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-electronimportUserscriptConverterfrom'userscript-to-electron';importfsfrom'fs';constconverter=newUserscriptConverter({dataDir: './userscript-data'});constscriptContent=fs.readFileSync('my-script.user.js','utf-8');constresult=converter.convert(scriptContent);console.log(result.metadata.name);converter.save(result,'output.js');const{ app, BrowserWindow, ipcMain }=require('electron');constpath=require('path');constUserscriptConverter=require('userscript-to-electron');constconverter=newUserscriptConverter({dataDir: path.join(app.getPath('userData'),'userscripts')});ipcMain.handle('userscript:run',async(_event,scriptPath)=>{constresult=converter.convertFile(scriptPath);constwin=BrowserWindow.getFocusedWindow();if(win){awaitwin.webContents.executeJavaScript(result.code);}returnresult.metadata;});newUserscriptConverter(options?: ConvertOptions)Methods:
convert(scriptContent: string): ConvertResultconvertFile(filePath: string): ConvertResultsave(result: ConvertResult, outputPath: string): void
ConvertResult:
interfaceConvertResult{code: string;metadata: UserscriptMetadata;sourceMap?: string;}MetadataParser.parse(scriptContent: string): UserscriptMetadatanewGMPolyfill(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.