refactor: GM APIs

This commit is contained in:
Xmader 2020-12-06 01:28:56 -05:00
parent 9491f6c848
commit ffa8683dbe
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
2 changed files with 10 additions and 9 deletions

View File

@ -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)
})
}

View File

@ -3,17 +3,17 @@
* UserScript APIs
*/
declare const GM: {
/** https://www.tampermonkey.net/documentation.php#GM_info */
info: Record<string, any>;
/** https://www.tampermonkey.net/documentation.php#GM_registerMenuCommand */
registerMenuCommand (name: string, fn: () => any, accessKey?: string): Promise<number>;
}
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'
}