feat: add buttons to the userscript manager menu
This commit is contained in:
parent
95ec4725e1
commit
63ac2030a9
4 changed files with 29 additions and 1 deletions
|
@ -83,7 +83,7 @@ export default [
|
||||||
format: "iife",
|
format: "iife",
|
||||||
sourcemap: false,
|
sourcemap: false,
|
||||||
banner: getBannerText,
|
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)})"
|
outro: "}.toString() + ')()');document.body.prepend(d)})"
|
||||||
},
|
},
|
||||||
plugins,
|
plugins,
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
import { ScoreInfo } from './scoreinfo'
|
import { ScoreInfo } from './scoreinfo'
|
||||||
import { loadMscore, WebMscore } from './mscore'
|
import { loadMscore, WebMscore } from './mscore'
|
||||||
import { useTimeout, windowOpenAsync, console, attachShadow } from './utils'
|
import { useTimeout, windowOpenAsync, console, attachShadow } from './utils'
|
||||||
|
import { isGmAvailable, registerMenuCommand } from './gm'
|
||||||
import i18n from './i18n'
|
import i18n from './i18n'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import btnListCss from './btn.css'
|
import btnListCss from './btn.css'
|
||||||
|
@ -86,6 +87,13 @@ export class BtnList {
|
||||||
btnTpl.title = options.tooltip
|
btnTpl.title = options.tooltip
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add buttons to the userscript manager menu
|
||||||
|
if (isGmAvailable()) {
|
||||||
|
registerMenuCommand(options.name, () => {
|
||||||
|
options.action(options.name, btnTpl, () => undefined)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return btnTpl
|
return btnTpl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
src/gm.ts
Normal file
19
src/gm.ts
Normal 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)
|
||||||
|
}
|
|
@ -13,5 +13,6 @@
|
||||||
// @license MIT
|
// @license MIT
|
||||||
// @copyright Copyright (c) 2019-2020 Xmader
|
// @copyright Copyright (c) 2019-2020 Xmader
|
||||||
// @grant unsafeWindow
|
// @grant unsafeWindow
|
||||||
|
// @grant GM.registerMenuCommand
|
||||||
// @run-at document-start
|
// @run-at document-start
|
||||||
// ==/UserScript==
|
// ==/UserScript==
|
||||||
|
|
Loading…
Reference in a new issue