An npm package that converts Tampermonkey or Greasemonkey userscripts into Electron-executable JavaScript.
This project tracks Tampermonkey documentation and implements a broad GM API compatibility layer.
Chinese documentation: README.zh-CN.md
Compatibility notes:
- Supports both classic
GM_*and modernGM.*API styles. - Supports
@grant GMand expands it to the full GM namespace bridge. - Supports
GM.xmlHttpRequestandGM_xmlHttpRequestcompatibility aliases.
Storage APIs:
GM_getValue,GM_setValue,GM_deleteValue,GM_listValuesGM_setValues,GM_getValues,GM_deleteValuesGM_addValueChangeListener,GM_removeValueChangeListener
Network APIs:
GM_xmlhttpRequestGM_downloadGM_webRequest(experimental)
UI APIs:
GM_notificationGM_openInTabGM_registerMenuCommand,GM_unregisterMenuCommand
DOM and style APIs:
GM_addElementGM_addStyle
Resources and clipboard APIs:
GM_getResourceText,GM_getResourceURLGM_setClipboard
Tab and info APIs:
GM_getTab,GM_saveTab,GM_getTabsGM_infoGM_log
Cookie and audio APIs:
GM_cookie.list,GM_cookie.set,GM_cookie.deleteGM_audio.setMute,GM_audio.getStateGM_audio.addStateChangeListener,GM_audio.removeStateChangeListener
Window globals:
unsafeWindowwindow.close,window.focuswindow.onurlchange
Supports common Tampermonkey headers including:
@name,@namespace,@version,@description,@author@match,@include,@exclude,@connect@grant,@require,@resource@run-at,@run-in,@sandbox@updateURL,@downloadURL,@supportURL,@homepage@noframes,@unwrap,@tag,@antifeature- I18n variants like
@name:zh-CN,@description:en
npm install userscript-to-electronimport UserscriptConverter from 'userscript-to-electron';
import fs from 'fs';
const converter = new UserscriptConverter({
dataDir: './userscript-data'
});
const scriptContent = fs.readFileSync('my-script.user.js', 'utf-8');
const result = converter.convert(scriptContent);
console.log(result.metadata.name);
converter.save(result, 'output.js');const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const UserscriptConverter = require('userscript-to-electron');
const converter = new UserscriptConverter({
dataDir: path.join(app.getPath('userData'), 'userscripts')
});
ipcMain.handle('userscript:run', async (_event, scriptPath) => {
const result = converter.convertFile(scriptPath);
const win = BrowserWindow.getFocusedWindow();
if (win) {
await win.webContents.executeJavaScript(result.code);
}
return result.metadata;
});new UserscriptConverter(options?: ConvertOptions)Methods:
convert(scriptContent: string): ConvertResultconvertFile(filePath: string): ConvertResultsave(result: ConvertResult, outputPath: string): void
ConvertResult:
interface ConvertResult {
code: string;
metadata: UserscriptMetadata;
sourceMap?: string;
}MetadataParser.parse(scriptContent: string): UserscriptMetadatanew GMPolyfill(options?: GMPolyfillOptions)
generatePolyfill(grants: string[]): string- GM value storage is persisted to
${dataDir}/__gm_storage.json. - Tab state is persisted to
${dataDir}/__gm_tabs.json. - Downloads are saved to
${dataDir}/downloads/.
- DOM-dependent userscripts should run in an Electron renderer process or with jsdom.
- Some browser-specific cookie and tab behavior may differ in Electron.
GM_webRequestbehavior depends on integration context and is currently experimental.
npm run build
npm run dev
npm testMIT. See LICENSE.