From ffa8683dbe05127881b691c9d8d1d830e3263f6e Mon Sep 17 00:00:00 2001 From: Xmader Date: Sun, 6 Dec 2020 01:28:56 -0500 Subject: [PATCH] refactor: GM APIs --- src/btn.ts | 7 ++++--- src/gm.ts | 12 ++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/btn.ts b/src/btn.ts index 0bd6dc9..1047f8f 100644 --- a/src/btn.ts +++ b/src/btn.ts @@ -2,7 +2,7 @@ import { ScoreInfo } from './scoreinfo' import { loadMscore, WebMscore } from './mscore' import { useTimeout, windowOpenAsync, console, attachShadow } from './utils' -import { isGmAvailable, registerMenuCommand } from './gm' +import { isGmAvailable, _GM } from './gm' import i18n from './i18n' // @ts-ignore import btnListCss from './btn.css' @@ -88,8 +88,9 @@ export class BtnList { } // add buttons to the userscript manager menu - if (isGmAvailable()) { - registerMenuCommand(options.name, () => { + if (isGmAvailable('registerMenuCommand')) { + // eslint-disable-next-line no-void + void _GM.registerMenuCommand(options.name, () => { options.action(options.name, btnTpl, () => undefined) }) } diff --git a/src/gm.ts b/src/gm.ts index e02e1c0..fd76ebe 100644 --- a/src/gm.ts +++ b/src/gm.ts @@ -3,17 +3,17 @@ * UserScript APIs */ declare const GM: { + /** https://www.tampermonkey.net/documentation.php#GM_info */ + info: Record; + /** https://www.tampermonkey.net/documentation.php#GM_registerMenuCommand */ registerMenuCommand (name: string, fn: () => any, accessKey?: string): Promise; } +export const _GM = GM type GM = typeof GM -export const isGmAvailable = (): boolean => { +export const isGmAvailable = (requiredMethod: keyof GM = 'info'): boolean => { return typeof GM !== 'undefined' && - typeof GM.registerMenuCommand === 'function' -} - -export const registerMenuCommand: GM['registerMenuCommand'] = (...args) => { - return GM.registerMenuCommand(...args) + typeof GM[requiredMethod] !== 'undefined' }