@@ -10,6 +10,7 @@ import EventEmitter from "eventemitter3";
1010import { getStorageName } from "@App/pkg/utils/utils" ;
1111import { MessageRequest } from "../service_worker/gm_api" ;
1212import { ScriptLoadInfo } from "../service_worker/runtime" ;
13+ import { ScriptMenuItem } from "../service_worker/popup" ;
1314
1415interface ApiParam {
1516 depend ?: string [ ] ;
@@ -299,20 +300,26 @@ export default class GMApi {
299300 }
300301
301302 @GMContext . API ( )
302- GM_registerMenuCommand ( name : string , listener : ( ) => void , accessKey ?: string | { id ?: number } ) : number {
303+ GM_registerMenuCommand (
304+ name : string ,
305+ listener : ( inputValue ?: any ) => void ,
306+ options_or_accessKey ?: ScriptMenuItem [ "options" ] | string
307+ ) : number {
303308 if ( ! this . menuMap ) {
304309 this . menuMap = new Map ( ) ;
305310 }
306- if ( typeof accessKey === "object" ) {
311+ if ( typeof options_or_accessKey === "object" ) {
312+ const option : ScriptMenuItem [ "options" ] = options_or_accessKey ;
307313 // 如果是对象,并且有id属性,则直接使用id
308- if ( accessKey . id && this . menuMap . has ( accessKey . id ) ) {
314+ if ( option . id && this . menuMap . has ( option . id ) ) {
309315 // 如果id存在,则直接使用
310- this . EE . removeAllListeners ( "menuClick:" + accessKey . id ) ;
311- this . EE . addListener ( "menuClick:" + accessKey . id , listener ) ;
312- this . sendMessage ( "GM_registerMenuCommand" , [ accessKey . id , name , accessKey ] ) ;
313- return accessKey . id ;
316+ this . EE . removeAllListeners ( "menuClick:" + option . id ) ;
317+ this . EE . addListener ( "menuClick:" + option . id , listener ) ;
318+ this . sendMessage ( "GM_registerMenuCommand" , [ option . id , name , option ] ) ;
319+ return option . id ;
314320 }
315321 } else {
322+ options_or_accessKey = { accessKey : options_or_accessKey } ;
316323 let flag = 0 ;
317324 this . menuMap . forEach ( ( val , menuId ) => {
318325 if ( val === name ) {
@@ -325,12 +332,20 @@ export default class GMApi {
325332 }
326333 this . eventId += 1 ;
327334 const id = this . eventId ;
335+ options_or_accessKey . id = id ;
328336 this . menuMap . set ( id , name ) ;
329337 this . EE . addListener ( "menuClick:" + id , listener ) ;
330- this . sendMessage ( "GM_registerMenuCommand" , [ id , name , accessKey ] ) ;
338+ this . sendMessage ( "GM_registerMenuCommand" , [ id , name , options_or_accessKey ] ) ;
331339 return id ;
332340 }
333341
342+ @GMContext . API ( {
343+ depend : [ "GM_registerMenuCommand" ] ,
344+ } )
345+ CAT_registerMenuInput ( ...args : Parameters < GMApi [ "GM_registerMenuCommand" ] > ) : number {
346+ return this . GM_registerMenuCommand ( ...args ) ;
347+ }
348+
334349 @GMContext . API ( )
335350 GM_addStyle ( css : string ) {
336351 // 与content页的消息通讯实际是同步,此方法不需要经过background
@@ -394,6 +409,13 @@ export default class GMApi {
394409 this . sendMessage ( "GM_unregisterMenuCommand" , [ id ] ) ;
395410 }
396411
412+ @GMContext . API ( {
413+ depend : [ "GM_unregisterMenuCommand" ] ,
414+ } )
415+ CAT_unregisterMenuInput ( ...args : Parameters < GMApi [ "GM_unregisterMenuCommand" ] > ) : void {
416+ this . GM_unregisterMenuCommand ( ...args ) ;
417+ }
418+
397419 @GMContext . API ( )
398420 CAT_userConfig ( ) {
399421 return this . sendMessage ( "CAT_userConfig" , [ ] ) ;
0 commit comments