refactor: GM APIs
This commit is contained in:
parent
9491f6c848
commit
ffa8683dbe
2 changed files with 10 additions and 9 deletions
|
@ -2,7 +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 { isGmAvailable, _GM } from './gm'
|
||||||
import i18n from './i18n'
|
import i18n from './i18n'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import btnListCss from './btn.css'
|
import btnListCss from './btn.css'
|
||||||
|
@ -88,8 +88,9 @@ export class BtnList {
|
||||||
}
|
}
|
||||||
|
|
||||||
// add buttons to the userscript manager menu
|
// add buttons to the userscript manager menu
|
||||||
if (isGmAvailable()) {
|
if (isGmAvailable('registerMenuCommand')) {
|
||||||
registerMenuCommand(options.name, () => {
|
// eslint-disable-next-line no-void
|
||||||
|
void _GM.registerMenuCommand(options.name, () => {
|
||||||
options.action(options.name, btnTpl, () => undefined)
|
options.action(options.name, btnTpl, () => undefined)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
12
src/gm.ts
12
src/gm.ts
|
@ -3,17 +3,17 @@
|
||||||
* UserScript APIs
|
* UserScript APIs
|
||||||
*/
|
*/
|
||||||
declare const GM: {
|
declare const GM: {
|
||||||
|
/** https://www.tampermonkey.net/documentation.php#GM_info */
|
||||||
|
info: Record<string, any>;
|
||||||
|
|
||||||
/** https://www.tampermonkey.net/documentation.php#GM_registerMenuCommand */
|
/** https://www.tampermonkey.net/documentation.php#GM_registerMenuCommand */
|
||||||
registerMenuCommand (name: string, fn: () => any, accessKey?: string): Promise<number>;
|
registerMenuCommand (name: string, fn: () => any, accessKey?: string): Promise<number>;
|
||||||
}
|
}
|
||||||
|
export const _GM = GM
|
||||||
|
|
||||||
type GM = typeof GM
|
type GM = typeof GM
|
||||||
|
|
||||||
export const isGmAvailable = (): boolean => {
|
export const isGmAvailable = (requiredMethod: keyof GM = 'info'): boolean => {
|
||||||
return typeof GM !== 'undefined' &&
|
return typeof GM !== 'undefined' &&
|
||||||
typeof GM.registerMenuCommand === 'function'
|
typeof GM[requiredMethod] !== 'undefined'
|
||||||
}
|
|
||||||
|
|
||||||
export const registerMenuCommand: GM['registerMenuCommand'] = (...args) => {
|
|
||||||
return GM.registerMenuCommand(...args)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue