-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathgm_api_async_test.js
More file actions
533 lines (492 loc) · 13.1 KB
/
Copy pathgm_api_async_test.js
File metadata and controls
533 lines (492 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// ==UserScript==
// @name GM.* API 完整测试 (异步版本)
// @namespace https://docs.scriptcat.org/
// @version 1.0.1
// @description 全面测试ScriptCat的所有GM.* (异步Promise版本) API功能
// @author ScriptCat
// @match https://content-security-policy.com/?gm_api_async
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.deleteValue
// @grant GM.listValues
// @grant GM.getResourceText
// @grant GM.getResourceUrl
// @grant GM.addStyle
// @grant GM.addElement
// @grant GM.xmlHttpRequest
// @grant GM.notification
// @grant GM.setClipboard
// @grant GM.info
// @grant GM.openInTab
// @grant GM.registerMenuCommand
// @grant GM.unregisterMenuCommand
// @grant GM.cookie
// @grant unsafeWindow
// @require https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js#sha384-vtXRMe3mGCbOeY7l30aIg8H9p3GdeSe4IFlP6G8JMa7o7lXvnz3GFKzPxzJdPfGK
// @require https://cdn.jsdelivr.net/gh/scriptscat/scriptcat@36ab4ce5ff23c820a32cd13ac5a04d8834ab4d82/example/tests/lib/sctest.js
// @resource testCSS https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css#sha256=62f74b1cf824a89f03554c638e719594c309b4d8a627a758928c0516fa7890ab
// @connect httpbingo.org
// @connect example.com
// @run-at document-start
// ==/UserScript==
(async function () {
"use strict";
const { describe, check, expect, run } = SCTest.create({ name: "GM.* API 完整测试(异步)" });
describe("GM.info", () => {
check(
"自动断言",
"GM.info 存在",
async () => {
expect(GM.info).toBeTypeOf("object");
expect(GM.info.script).toBeTruthy();
expect(GM.info.scriptMetaStr).toBeTruthy();
},
null,
null,
null
);
});
describe("GM 存储 API", () => {
check(
"自动断言",
"GM.setValue - 字符串",
async () => {
await GM.setValue("test_string", "Hello ScriptCat Async");
const value = await GM.getValue("test_string");
expect(value).toBe("Hello ScriptCat Async");
},
null,
null,
null
);
check(
"自动断言",
"GM.setValue - 数字",
async () => {
await GM.setValue("test_number", 42);
const value = await GM.getValue("test_number");
expect(value).toBe(42);
},
null,
null,
null
);
check(
"自动断言",
"GM.setValue - 布尔值",
async () => {
await GM.setValue("test_boolean", true);
const value = await GM.getValue("test_boolean");
expect(value).toBe(true);
},
null,
null,
null
);
check(
"自动断言",
"GM.setValue - 对象",
async () => {
const obj = { name: "ScriptCat", version: "1.3.0", features: ["GM API", "Async"] };
await GM.setValue("test_object", obj);
const value = await GM.getValue("test_object");
expect(value).toBeTypeOf("object");
expect(value.name).toBe(obj.name);
expect(value.version).toBe(obj.version);
expect(JSON.stringify(value.features)).toBe(JSON.stringify(obj.features));
},
null,
null,
null
);
check(
"自动断言",
"GM.setValue - 数组",
async () => {
const arr = [1, 2, 3, "test", { key: "value" }];
await GM.setValue("test_array", arr);
const value = await GM.getValue("test_array");
expect(Array.isArray(value)).toBeTruthy();
expect(value.length).toBe(arr.length);
expect(value[0]).toBe(arr[0]);
expect(value[3]).toBe(arr[3]);
expect(value[4].key).toBe(arr[4].key);
},
null,
null,
null
);
check(
"自动断言",
"GM.getValue - 默认值",
async () => {
const value = await GM.getValue("non_existent_key", "default_value");
expect(value).toBe("default_value");
},
null,
null,
null
);
check(
"自动断言",
"GM.listValues",
async () => {
const values = await GM.listValues();
expect(Array.isArray(values)).toBeTruthy();
expect(values.includes("test_string")).toBeTruthy();
},
null,
null,
null
);
check(
"自动断言",
"GM.deleteValue",
async () => {
await GM.setValue("test_delete", "to be deleted");
expect(await GM.getValue("test_delete")).toBe("to be deleted");
await GM.deleteValue("test_delete");
expect(await GM.getValue("test_delete", "not_found")).toBe("not_found");
},
null,
null,
null
);
});
describe("GM 样式 API", () => {
check(
"自动断言",
"GM.addStyle - CSS字符串",
async () => {
const css = `
.scriptcat-test-async {
color: blue;
font-weight: bold;
}
`;
const element = await GM.addStyle(css);
expect(element && element.tagName === "STYLE").toBeTruthy();
},
null,
null,
null
);
});
describe("GM.addElement", () => {
check(
"自动断言",
"GM.addElement - 创建元素",
async () => {
expect(GM.addElement).toBeTypeOf("function");
const div = await GM.addElement("div", {
textContent: "ScriptCat GM.addElement 测试",
style: "position: fixed; top: 10px; right: 10px; background: lightblue; padding: 10px; z-index: 9999;",
});
expect(div && div.tagName === "DIV").toBeTruthy();
// 3秒后移除
setTimeout(() => div.remove(), 3000);
},
null,
null,
null
);
});
describe("GM 资源 API", () => {
check(
"自动断言",
"GM.getResourceText",
async () => {
expect(GM.getResourceText).toBeTypeOf("function");
const css = await GM.getResourceText("testCSS");
expect(css).toBeTypeOf("string");
expect(css.length).toBe(163870);
},
null,
null,
null
);
check(
"自动断言",
"GM.getResourceUrl",
async () => {
expect(GM.getResourceUrl).toBeTypeOf("function");
const url = await GM.getResourceUrl("testCSS");
expect(url).toBeTypeOf("string");
expect(url.startsWith("data:") || url.startsWith("blob:")).toBeTruthy();
},
null,
null,
null
);
});
describe("GM 网络请求 API", () => {
check(
"自动断言",
"GM.xmlHttpRequest - GET 请求",
async () => {
return new Promise((resolve, reject) => {
GM.xmlHttpRequest({
method: "GET",
url: "https://httpbingo.org/get",
timeout: 10000,
onload: (response) => {
try {
expect(response.status).toBe(200);
expect(response.responseText).toBeTruthy();
const data = JSON.parse(response.responseText);
expect(data).toBeTypeOf("object");
expect(data.url).toBe("https://httpbingo.org/get");
resolve();
} catch (error) {
reject(error);
}
},
onerror: (error) => {
reject(new Error("请求失败: " + error));
},
ontimeout: () => {
reject(new Error("请求超时"));
},
});
});
},
null,
null,
null
);
check(
"自动断言",
"GM.xmlHttpRequest - 返回控制对象",
async () => {
const controller = GM.xmlHttpRequest({
method: "GET",
url: "https://httpbingo.org/get",
timeout: 10000,
onload: () => {},
onerror: () => {},
});
expect(controller).toBeTypeOf("object");
expect(controller.abort).toBeTypeOf("function");
controller.abort();
},
null,
null,
null
);
});
describe("GM 通知 API", () => {
check(
"自动断言",
"GM.notification - Promise 版本",
async () => {
expect(GM.notification).toBeTypeOf("function");
const notificationPromise = GM.notification({
text: "ScriptCat GM.* API 测试通知",
title: "ScriptCat 异步测试",
image: "https://scriptcat.org/logo.png",
onclick: () => {
console.log("通知被点击");
},
});
// GM.notification 可能返回 Promise 或控制对象
if (notificationPromise && typeof notificationPromise.then === "function") {
await notificationPromise;
console.log("通知已发送(Promise 已完成)");
} else {
console.log("通知已发送(请检查系统通知)");
}
},
null,
null,
null
);
});
describe("GM 剪贴板 API", () => {
check(
"自动断言",
"GM.setClipboard",
async () => {
expect(GM.setClipboard).toBeTypeOf("function");
await GM.setClipboard("ScriptCat GM.* API 测试文本 - " + new Date().toLocaleString());
console.log("文本已复制到剪贴板(可以尝试粘贴验证)");
},
null,
null,
null
);
});
describe("GM 标签页 API", () => {
check(
"自动断言",
"GM.openInTab (不执行)",
async () => {
// 不实际打开标签页,只测试函数是否存在
expect(GM.openInTab).toBeTypeOf("function");
},
null,
null,
null
);
});
describe("GM 菜单 API", () => {
check(
"自动断言",
"GM.registerMenuCommand",
async () => {
const menuId = await GM.registerMenuCommand("ScriptCat 异步测试菜单", () => {
alert("异步测试菜单被点击!");
});
expect(menuId !== undefined).toBeTruthy();
},
null,
null,
null
);
});
describe("GM Cookie API", () => {
check(
"自动断言",
"GM.cookie 函数存在",
async () => {
expect(GM.cookie).toBeTypeOf("function");
},
null,
null,
null
);
check(
"自动断言",
"GM.cookie.set",
async () => {
await GM.cookie.set({
url: "http://example.com/cookie",
name: "scriptcat_async_test1",
value: "async_test_value_1",
});
},
null,
null,
null
);
check(
"自动断言",
"GM.cookie.set (带 domain 和 path)",
async () => {
await GM.cookie.set({
url: "http://www.example.com/",
domain: ".example.com",
path: "/path",
name: "scriptcat_async_test2",
value: "async_test_value_2",
});
},
null,
null,
null
);
check(
"自动断言",
"GM.cookie.list (by domain)",
async () => {
const cookies = await GM.cookie.list({
domain: "example.com",
});
expect(Array.isArray(cookies)).toBeTruthy();
expect(cookies.length >= 1).toBeTruthy();
},
null,
null,
null
);
check(
"自动断言",
"GM.cookie.list (by url)",
async () => {
const cookies = await GM.cookie.list({
url: "http://example.com/cookie",
});
expect(Array.isArray(cookies)).toBeTruthy();
},
null,
null,
null
);
check(
"自动断言",
"GM.cookie.delete",
async () => {
await GM.cookie.delete({
url: "http://www.example.com/path",
name: "scriptcat_async_test2",
});
},
null,
null,
null
);
check(
"自动断言",
"GM.cookie - 验证删除后",
async () => {
const cookies = await GM.cookie.list({
domain: "example.com",
});
const test2Cookie = cookies.find((c) => c.name === "scriptcat_async_test2");
expect(!test2Cookie).toBeTruthy();
},
null,
null,
null
);
// 清理所有测试 cookies
check(
"自动断言",
"清理测试 cookies",
async () => {
const cookies = await GM.cookie.list({ domain: "example.com" });
const testCookies = cookies.filter((c) => c.name.startsWith("scriptcat_async_test"));
if (testCookies.length === 0) {
return;
}
await Promise.all(
testCookies.map((cookie) =>
GM.cookie.delete({
url: `http://${cookie.domain}${cookie.path}`,
name: cookie.name,
})
)
);
},
null,
null,
null
);
});
describe("unsafeWindow", () => {
check(
"自动断言",
"unsafeWindow",
async () => {
expect(unsafeWindow).toBeTypeOf("object");
expect(unsafeWindow.document).toBe(document);
},
null,
null,
null
);
});
describe("@require", () => {
check(
"自动断言",
"jQuery 加载 (@require)",
async () => {
expect(jQuery).toBeTypeOf("function");
expect($).toBeTypeOf("function");
},
null,
null,
null
);
});
await run();
})();