Skip to content

Commit b455271

Browse files
committed
Merge branch 'release/v1.3' into pr-permission-check-and-better-page
2 parents c1cbbc0 + 75180b7 commit b455271

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2996
-972
lines changed

example/gm_add_element.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,36 @@
2222
* 2. 元素标签名
2323
* 3. 属性对象
2424
*/
25-
const el = GM_addElement(document.querySelector('.BorderGrid-cell'), "img", {
26-
src: "https://bbs.tampermonkey.net.cn/uc_server/avatar.php?uid=4&size=small&ts=1"
25+
26+
// ------------- 基础用法 ----------------
27+
28+
const el = GM_addElement(document.querySelector(".BorderGrid-cell"), "img", {
29+
src: "https://bbs.tampermonkey.net.cn/uc_server/avatar.php?uid=4&size=small&ts=1",
2730
});
2831

2932
// 打印创建出来的 DOM 元素
3033
console.log(el);
34+
35+
// ------------- 基础用法 - textContent ----------------
36+
37+
const span3 = GM_addElement("span", {
38+
textContent: "Hello",
39+
});
40+
41+
console.log(`span text: ${span3.textContent}`);
42+
43+
// ------------- 基础用法 - onload & onerror ----------------
44+
45+
new Promise((resolve, reject) => {
46+
img = GM_addElement(document.body, "img", {
47+
src: "https://www.tampermonkey.net/favicon.ico",
48+
onload: resolve,
49+
onerror: reject,
50+
});
51+
})
52+
.then(() => {
53+
console.log("img insert ok");
54+
})
55+
.catch(() => {
56+
console.log("img insert failed");
57+
});

example/gm_download.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* - 自定义 header
1919
* - 进度回调 / 完成回调
2020
*/
21+
22+
// 1. 下载网络资源
23+
2124
GM_download({
2225
url: "https://scriptcat.org/api/v2/open/crx-download/ndcooeababalnlpkfedmmbbbgkljhpjf",
2326
name: "scriptcat.crx",
@@ -35,3 +38,23 @@ GM_download({
3538
console.log("load", data);
3639
},
3740
});
41+
42+
// 2. 下载 Blob 资源
43+
// 参考: https://github.com/Tampermonkey/tampermonkey/issues/2591
44+
45+
const pngData = new Uint8Array([
46+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
47+
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
48+
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00,
49+
0x0a, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf, 0xc0, 0x00,
50+
0x00, 0x03, 0x01, 0x01, 0x00, 0xae, 0xb4, 0xfa, 0x77, 0x00, 0x00, 0x00,
51+
0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
52+
]);
53+
54+
const testImageUrl = URL.createObjectURL(new Blob([pngData], { type: 'image/png' }));
55+
56+
GM_download({
57+
url: testImageUrl,
58+
name: 'test/test.png', // 储存在 test 资料夹内
59+
conflictAction: 'overwrite', // 每次都使用固定的档案名
60+
});

example/tests/gm_api_test.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name GM API 完整测试
33
// @namespace https://docs.scriptcat.org/
4-
// @version 1.0.0
4+
// @version 1.1.0
55
// @description 全面测试ScriptCat的所有GM API功能
66
// @author ScriptCat
77
// @match https://content-security-policy.com/
@@ -234,7 +234,7 @@
234234
});
235235

236236
// ============ GM_addElement 测试 ============
237-
test("GM_addElement - 创建元素", () => {
237+
await testAsync("GM_addElement - 创建元素", async () => {
238238
assert("function", typeof GM_addElement, "GM_addElement 应该是函数");
239239

240240
const div = GM_addElement("div", {
@@ -244,8 +244,39 @@
244244
assert(true, div && div.tagName === "DIV", "应该返回 div 元素");
245245
console.log("添加的元素:", div);
246246

247+
// 创建脚本元素测试
248+
const script = GM_addElement("script", {
249+
textContent: 'window.foo = "bar";',
250+
});
251+
assert(true, script && script.tagName === "SCRIPT", "应该返回 script 元素");
252+
assert("bar", unsafeWindow.foo, "脚本内容应该执行,unsafeWindow.foo 应该是 'bar'");
253+
console.log("添加的脚本元素:", script);
254+
255+
document.querySelector(".container").insertBefore(script, document.querySelector(".masthead"));
256+
257+
// onload 和 onerror 测试 - 插入图片元素
258+
let img;
259+
await new Promise((resolve, reject) => {
260+
img = GM_addElement(document.body, "img", {
261+
src: "https://www.tampermonkey.net/favicon.ico",
262+
onload: () => {
263+
console.log("图片加载成功");
264+
resolve();
265+
},
266+
onerror: (error) => {
267+
reject(new Error("图片加载失败: " + error));
268+
},
269+
});
270+
});
271+
assert(true, img && img.tagName === "IMG", "应该返回 img 元素");
272+
console.log("添加的图片元素:", img);
273+
247274
// 3秒后移除
248-
setTimeout(() => div.remove(), 3000);
275+
setTimeout(() => {
276+
script.remove();
277+
div.remove();
278+
img.remove();
279+
}, 3000);
249280
});
250281

251282
// ============ GM_getResourceText/URL 测试 ============

0 commit comments

Comments
 (0)