mock-violentmonkey
Version:
Mock violentmonkey's globals for testing userscripts
98 lines • 2.42 kB
JavaScript
import crypto from 'node:crypto';
import process from 'node:process';
import { VMStorage } from '../vm-storage.js';
const generateInfo = () => ({
uuid: crypto.randomUUID(),
scriptMetaStr: '',
scriptWillUpdate: true,
scriptHandler: 'Violentmonkey',
version: '2.30.0',
platform: {
arch: 'x86-64',
browserName: 'firefox',
browserVersion: '135.0',
os: 'linux',
},
script: {
description: '',
excludeMatches: [],
excludes: [],
grant: [],
includes: [],
matches: [],
name: '',
namespace: '',
resources: [],
require: [],
runAt: 'document-start',
version: '1.0',
},
injectInto: 'page',
isIncognito: false,
userAgent: `mock-violentmonkey (Node.js ${process.version})`,
});
const cachedInfos = new VMStorage(generateInfo);
/**
* Returns with information about the userscript
*
* The object is modifiable like with Violentmonkey
*
* If this function is imported, it is a function, if it used from global, it is a getter.
*
* @defaultValue
* ```
* {
* uuid: {randomly generated},
* scriptMetaStr: '',
* scriptWillUpdate: true,
* scriptHandler: 'Violentmonkey',
* version: '2.13.0',
* platform: {
* arch: 'x86-64',
* browserName: 'firefox',
* browserVersion: '93',
* os: 'linux',
* },
* script: {
* description: '',
* excludes: [],
* includes: [],
* matches: [],
* name: '',
* namespace: '',
* resources: [],
* runAt: 'document-start',
* version: '1.0',
* },
* injectInto: 'page',
* }
* ```
*/
const getInfo = () => cachedInfos.get(true);
/**
* Update the GM_info object by passing a DeepPartial of GM_info
*/
const updateInfo = (newInfo) => {
const info = getInfo();
for (const [key, value] of Object.entries(newInfo)) {
switch (key) {
case 'script':
case 'platform': {
Object.assign(info[key], value);
break;
}
default: {
// @ts-expect-error It will always have the right type, since key and value are connected
info[key] = value;
}
}
}
return info;
};
export { getInfo as GM_info, updateInfo as update_GM_info, };
Object.defineProperties(globalThis, {
GM_info: {
get: getInfo,
},
});
//# sourceMappingURL=info.js.map