File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // ==UserScript==
2+ // @name 异步GM函数
3+ // @namespace https://bbs.tampermonkey.net.cn/
4+ // @version 0.1.0
5+ // @description try to take over the world!
6+ // @author You
7+ // @match https://bbs.tampermonkey.net.cn/
8+ // @grant GM.getValue
9+ // @grant GM.setValue
10+ // @resource test.html https://bbs.tampermonkey.net.cn/
11+ // @grant GM.getResourceUrl
12+ // ==/UserScript==
13+
14+ ( async function ( ) {
15+ 'use strict' ;
16+ GM . setValue ( "test-key" , 1 ) . then ( ( ) => {
17+ GM . getValue ( "test-key" ) . then ( value => {
18+ console . log ( "get test-key value: " , value ) ;
19+ } )
20+ } ) ;
21+ const resourceUrl = await GM . getResourceUrl ( "test.html" ) ;
22+ console . log ( resourceUrl ) ;
23+ } ) ( ) ;
Original file line number Diff line number Diff line change @@ -170,6 +170,15 @@ export default class GMApi {
170170 return defaultValue ;
171171 }
172172
173+ @GMContext . API ( { depend : [ "GM_getValue" ] } )
174+ public GMDotGetValue ( key : string , defaultValue ?: any ) : Promise < any > {
175+ // 兼容GM.getValue
176+ return new Promise ( ( resolve ) => {
177+ const ret = this . GM_getValue ( key , defaultValue ) ;
178+ resolve ( ret ) ;
179+ } ) ;
180+ }
181+
173182 @GMContext . API ( )
174183 public GM_setValue ( key : string , value : any ) {
175184 // 对object的value进行一次转化
@@ -978,9 +987,8 @@ export default class GMApi {
978987 }
979988
980989 // GM_getResourceURL的异步版本,用来兼容GM.getResourceUrl
981- @GMContext . API ( )
990+ @GMContext . API ( { depend : [ "GM_getResourceURL" ] } )
982991 async GMDotGetResourceUrl ( name : string , isBlobUrl ?: boolean ) : Promise < string | undefined > {
983- console . log ( "GMDotGetResourceUrl" , name , isBlobUrl ) ;
984992 if ( this . scriptRes . resource ) {
985993 const r = this . scriptRes . resource [ name ] ;
986994 if ( r ) {
You can’t perform that action at this time.
0 commit comments