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 { 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)
|
||||
})
|
||||
}
|
||||
|
|
12
src/gm.ts
12
src/gm.ts
|
@ -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'
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue