@@ -5,7 +5,7 @@ import { ExtVersion } from "@App/app/const";
5
5
import { initTestEnv } from "@Tests/utils" ;
6
6
import { describe , expect , it } from "vitest" ;
7
7
import type { GMInfoEnv } from "./types" ;
8
- import { ScriptLoadInfo } from "../service_worker/types" ;
8
+ import type { ScriptLoadInfo } from "../service_worker/types" ;
9
9
10
10
initTestEnv ( ) ;
11
11
@@ -62,7 +62,7 @@ describe("GM_info", () => {
62
62
const ret = await sandboxExec . exec ( ) ;
63
63
expect ( ret . GM_info . version ) . toEqual ( ExtVersion ) ;
64
64
expect ( ret . GM_info . script . version ) . toEqual ( "1.0.0" ) ;
65
- expect ( ret . _this ) . toEqual ( sandboxExec . proxyContent ) ;
65
+ expect ( ret . _this ) . toEqual ( sandboxExec . proxyContext ) ;
66
66
} ) ;
67
67
} ) ;
68
68
@@ -188,7 +188,7 @@ describe("none this", () => {
188
188
describe ( "@grant GM" , ( ) => {
189
189
it ( "GM_" , async ( ) => {
190
190
const script = Object . assign ( { } , scriptRes2 ) as ScriptLoadInfo ;
191
- script . metadata . grant = [ "GM_getValue" , "GM_getTab" , "GM_saveTab" ] ;
191
+ script . metadata . grant = [ "GM_getValue" , "GM_getTab" , "GM_saveTab" , "GM_cookie" ] ;
192
192
// @ts -ignore
193
193
const exec = new ExecScript ( script , undefined , undefined , undefined , envInfo ) ;
194
194
script . code = `return {
@@ -198,21 +198,25 @@ describe("@grant GM", () => {
198
198
GM_getValue: this.GM_getValue,
199
199
GM_getTab: this.GM_getTab,
200
200
GM_saveTab: this.GM_saveTab,
201
+ GM_cookie: this.GM_cookie,
202
+ ["GM_cookie.list"]: this.GM_cookie.list,
203
+ ["GM.cookie"]: this.GM.cookie,
201
204
}` ;
202
205
exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
203
206
const ret = await exec . exec ( ) ;
204
- expect ( ret ) . toEqual ( {
205
- "GM.getValue" : undefined ,
206
- "GM.getTab" : undefined ,
207
- "GM.setTab" : undefined ,
208
- GM_getValue : expect . any ( Function ) ,
209
- GM_getTab : expect . any ( Function ) ,
210
- GM_saveTab : expect . any ( Function ) ,
211
- } ) ;
207
+ expect ( ret [ "GM.getValue" ] ) . toBeUndefined ( ) ;
208
+ expect ( ret [ "GM.getTab" ] ) . toBeUndefined ( ) ;
209
+ expect ( ret [ "GM.setTab" ] ) . toBeUndefined ( ) ;
210
+ expect ( ret . GM_getValue . name ) . toEqual ( "bound GM_getValue" ) ;
211
+ expect ( ret . GM_getTab . name ) . toEqual ( "bound GM_getTab" ) ;
212
+ expect ( ret . GM_saveTab . name ) . toEqual ( "bound GM_saveTab" ) ;
213
+ expect ( ret . GM_cookie . name ) . toEqual ( "bound GM_cookie" ) ;
214
+ expect ( ret [ "GM_cookie.list" ] . name ) . toEqual ( "bound GM_cookie.list" ) ;
215
+ expect ( ret [ "GM.cookie" ] ) . toBeUndefined ( ) ;
212
216
} ) ;
213
217
it ( "GM.*" , async ( ) => {
214
218
const script = Object . assign ( { } , scriptRes2 ) as ScriptLoadInfo ;
215
- script . metadata . grant = [ "GM.getValue" , "GM.getTab" , "GM.saveTab" ] ;
219
+ script . metadata . grant = [ "GM.getValue" , "GM.getTab" , "GM.saveTab" , "GM.cookie" ] ;
216
220
// @ts -ignore
217
221
const exec = new ExecScript ( script , undefined , undefined , undefined , envInfo ) ;
218
222
script . code = `return {
@@ -222,16 +226,110 @@ describe("@grant GM", () => {
222
226
GM_getValue: this.GM_getValue,
223
227
GM_getTab: this.GM_getTab,
224
228
GM_saveTab: this.GM_saveTab,
229
+ GM_cookie: this.GM_cookie,
230
+ ["GM.cookie"]: this.GM.cookie,
225
231
}` ;
226
232
exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
227
233
const ret = await exec . exec ( ) ;
228
- expect ( ret ) . toEqual ( {
229
- "GM.getValue" : expect . any ( Function ) ,
230
- "GM.getTab" : expect . any ( Function ) ,
231
- "GM.setTab" : expect . any ( Function ) ,
232
- GM_getValue : undefined ,
233
- GM_getTab : undefined ,
234
- GM_setTab : undefined ,
235
- } ) ;
234
+ expect ( ret [ "GM.getValue" ] . name ) . toEqual ( "bound GM.getValue" ) ;
235
+ expect ( ret [ "GM.getTab" ] . name ) . toEqual ( "bound GM_getTab" ) ;
236
+ expect ( ret [ "GM.saveTab" ] . name ) . toEqual ( "bound GM_saveTab" ) ;
237
+ expect ( ret . GM_getValue ) . toBeUndefined ( ) ;
238
+ expect ( ret . GM_getTab ) . toBeUndefined ( ) ;
239
+ expect ( ret . GM_saveTab ) . toBeUndefined ( ) ;
240
+ expect ( ret . GM_cookie ) . toBeUndefined ( ) ;
241
+ expect ( ret [ "GM.cookie" ] . name ) . toEqual ( "bound GM.cookie" ) ;
242
+ expect ( ret [ "GM.cookie" ] . list . name ) . toEqual ( "bound GM.cookie.list" ) ;
243
+ } ) ;
244
+ } ) ;
245
+
246
+ describe ( "window.*" , ( ) => {
247
+ it ( "window.close" , async ( ) => {
248
+ const script = Object . assign ( { } , scriptRes2 ) as ScriptLoadInfo ;
249
+ script . metadata . grant = [ "window.close" ] ;
250
+ script . code = `return window.close;` ;
251
+ // @ts -ignore
252
+ const exec = new ExecScript ( script , undefined , undefined , undefined , envInfo ) ;
253
+ exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
254
+ const ret = await exec . exec ( ) ;
255
+ expect ( ret ) . toEqual ( expect . any ( Function ) ) ;
256
+ } ) ;
257
+ } ) ;
258
+
259
+ describe ( "GM Api" , ( ) => {
260
+ it ( "GM_getValue" , async ( ) => {
261
+ const script = Object . assign ( { } , scriptRes2 ) as ScriptLoadInfo ;
262
+ script . value = { test : "ok" } ;
263
+ script . metadata . grant = [ "GM_getValue" ] ;
264
+ script . code = `return GM_getValue("test");` ;
265
+ // @ts -ignore
266
+ const exec = new ExecScript ( script , undefined , undefined , undefined , envInfo ) ;
267
+ exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
268
+ const ret = await exec . exec ( ) ;
269
+ expect ( ret ) . toEqual ( "ok" ) ;
270
+ } ) ;
271
+ it ( "GM.getValue" , async ( ) => {
272
+ const script = Object . assign ( { } , scriptRes2 ) as ScriptLoadInfo ;
273
+ script . value = { test : "ok" } ;
274
+ script . metadata . grant = [ "GM.getValue" ] ;
275
+ script . code = `return GM.getValue("test").then(v=>v+"!");` ;
276
+ // @ts -ignore
277
+ const exec = new ExecScript ( script , undefined , undefined , undefined , envInfo ) ;
278
+ exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
279
+ const ret = await exec . exec ( ) ;
280
+ expect ( ret ) . toEqual ( "ok!" ) ;
281
+ } ) ;
282
+
283
+
284
+ it ( "GM_listValues" , async ( ) => {
285
+ const script = Object . assign ( { } , scriptRes2 ) as ScriptLoadInfo ;
286
+ script . value = { test1 : "23" , test2 : "45" , test3 : "67" } ;
287
+ script . metadata . grant = [ "GM_listValues" ] ;
288
+ script . code = `return GM_listValues().join("-");` ;
289
+ // @ts -ignore
290
+ const exec = new ExecScript ( script , undefined , undefined , undefined , envInfo ) ;
291
+ exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
292
+ const ret = await exec . exec ( ) ;
293
+ expect ( ret ) . toEqual ( "test1-test2-test3" ) ;
294
+ } ) ;
295
+
296
+ it ( "GM.listValues" , async ( ) => {
297
+ const script = Object . assign ( { } , scriptRes2 ) as ScriptLoadInfo ;
298
+ script . value = { test1 : "23" , test2 : "45" , test3 : "67" } ;
299
+ script . metadata . grant = [ "GM.listValues" ] ;
300
+ script . code = `return GM.listValues().then(v=>v.join("-"));` ;
301
+ // @ts -ignore
302
+ const exec = new ExecScript ( script , undefined , undefined , undefined , envInfo ) ;
303
+ exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
304
+ const ret = await exec . exec ( ) ;
305
+ expect ( ret ) . toEqual ( "test1-test2-test3" ) ;
306
+ } ) ;
307
+
308
+ it ( "GM_getValues" , async ( ) => {
309
+ const script = Object . assign ( { } , scriptRes2 ) as ScriptLoadInfo ;
310
+ script . value = { test1 : "23" , test2 : 45 , test3 : "67" } ;
311
+ script . metadata . grant = [ "GM_getValues" ] ;
312
+ script . code = `return GM_getValues(["test2", "test3", "test1"]);` ;
313
+ // @ts -ignore
314
+ const exec = new ExecScript ( script , undefined , undefined , undefined , envInfo ) ;
315
+ exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
316
+ const ret = await exec . exec ( ) ;
317
+ expect ( ret . test1 ) . toEqual ( "23" ) ;
318
+ expect ( ret . test2 ) . toEqual ( 45 ) ;
319
+ expect ( ret . test3 ) . toEqual ( "67" ) ;
320
+ } ) ;
321
+
322
+ it ( "GM.getValues" , async ( ) => {
323
+ const script = Object . assign ( { } , scriptRes2 ) as ScriptLoadInfo ;
324
+ script . value = { test1 : "23" , test2 : 45 , test3 : "67" } ;
325
+ script . metadata . grant = [ "GM.getValues" ] ;
326
+ script . code = `return GM.getValues(["test2", "test3", "test1"]).then(v=>v);` ;
327
+ // @ts -ignore
328
+ const exec = new ExecScript ( script , undefined , undefined , undefined , envInfo ) ;
329
+ exec . scriptFunc = compileScript ( compileScriptCode ( script ) ) ;
330
+ const ret = await exec . exec ( ) ;
331
+ expect ( ret . test1 ) . toEqual ( "23" ) ;
332
+ expect ( ret . test2 ) . toEqual ( 45 ) ;
333
+ expect ( ret . test3 ) . toEqual ( "67" ) ;
236
334
} ) ;
237
335
} ) ;
0 commit comments