feat: add buttons to the userscript manager menu

This commit is contained in:
Xmader 2020-12-06 00:48:52 -05:00
parent 95ec4725e1
commit 63ac2030a9
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
4 changed files with 29 additions and 1 deletions

View File

@ -83,7 +83,7 @@ export default [
format: "iife",
sourcemap: false,
banner: getBannerText,
intro: "new Promise(resolve=>{const id=''+Math.random();(typeof unsafeWindow=='object'?unsafeWindow:window)[id]=resolve;setTimeout(`(function a(){window['${id}'](new Image())})()//# sourceURL=${location.href}`)}).then(d=>{d.style.display='none';d.src='data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';d.once=false;d.setAttribute('onload','if(this.once)return;this.once=true;this.remove();(' + function a () {",
intro: "const w=typeof unsafeWindow=='object'?unsafeWindow:window;const gmId=''+Math.random();w[gmId]=typeof GM=='object'?GM:undefined;new Promise(resolve=>{const id=''+Math.random();w[id]=resolve;setTimeout(`(function a(){window['${id}'](new Image())})()//# sourceURL=${location.href}`)}).then(d=>{d.style.display='none';d.src='data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';d.once=false;d.setAttribute('onload',`if(this.once)return;this.once=true;this.remove();const GM=window['${gmId}'];(` + function a () {",
outro: "}.toString() + ')()');document.body.prepend(d)})"
},
plugins,

View File

@ -2,6 +2,7 @@
import { ScoreInfo } from './scoreinfo'
import { loadMscore, WebMscore } from './mscore'
import { useTimeout, windowOpenAsync, console, attachShadow } from './utils'
import { isGmAvailable, registerMenuCommand } from './gm'
import i18n from './i18n'
// @ts-ignore
import btnListCss from './btn.css'
@ -86,6 +87,13 @@ export class BtnList {
btnTpl.title = options.tooltip
}
// add buttons to the userscript manager menu
if (isGmAvailable()) {
registerMenuCommand(options.name, () => {
options.action(options.name, btnTpl, () => undefined)
})
}
return btnTpl
}

19
src/gm.ts Normal file
View File

@ -0,0 +1,19 @@
/**
* UserScript APIs
*/
declare const GM: {
/** https://www.tampermonkey.net/documentation.php#GM_registerMenuCommand */
registerMenuCommand (name: string, fn: () => any, accessKey?: string): Promise<number>;
}
type GM = typeof GM
export const isGmAvailable = (): boolean => {
return typeof GM !== 'undefined' &&
typeof GM.registerMenuCommand === 'function'
}
export const registerMenuCommand: GM['registerMenuCommand'] = (...args) => {
return GM.registerMenuCommand(...args)
}

View File

@ -13,5 +13,6 @@
// @license MIT
// @copyright Copyright (c) 2019-2020 Xmader
// @grant unsafeWindow
// @grant GM.registerMenuCommand
// @run-at document-start
// ==/UserScript==