Skip to content

Commit 9ef0322

Browse files
committed
Merge branch 'release/v1.3' into fix-xhr-impl-401
2 parents d1f4b2a + 1dff566 commit 9ef0322

193 files changed

Lines changed: 13136 additions & 6937 deletions

File tree

Some content is hidden

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

.github/workflows/test.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ jobs:
3838
eslint-
3939
4040
- name: Lint
41-
run: pnpm lint:ci
41+
run: |
42+
pnpm typecheck
43+
pnpm lint:ci
4244
4345
- name: Unit Test
4446
run: |

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ tailwind.config.js
3232

3333
package-lock.json
3434
yarn.lock
35+
36+
.claude/settings.local.json

example/tests/early_inject_content_test.js

Lines changed: 84 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,93 @@
1818
// @run-at document-start
1919
// ==/UserScript==
2020

21-
(async function () {
22-
"use strict";
23-
24-
console.log("%c=== Content环境 GM API 测试开始 ===", "color: blue; font-size: 16px; font-weight: bold;");
25-
26-
let testResults = {
27-
passed: 0,
28-
failed: 0,
29-
total: 0,
30-
};
31-
32-
// 测试辅助函数(支持同步和异步)
33-
async function test(name, fn) {
34-
testResults.total++;
35-
try {
36-
await fn();
37-
testResults.passed++;
38-
console.log(`%c✓ ${name}`, "color: green;");
39-
return true;
40-
} catch (error) {
41-
testResults.failed++;
42-
console.error(`%c✗ ${name}`, "color: red;", error);
43-
return false;
44-
}
21+
// 测试辅助函数(支持同步和异步)
22+
async function test(name, fn) {
23+
testResults.total++;
24+
try {
25+
await fn();
26+
testResults.passed++;
27+
console.log(`%c✓ ${name}`, "color: green;");
28+
return true;
29+
} catch (error) {
30+
testResults.failed++;
31+
console.error(`%c✗ ${name}`, "color: red;", error);
32+
return false;
4533
}
46-
47-
// assert(expected, actual, message) - 比较两个值是否相等
48-
function assert(expected, actual, message) {
49-
if (expected !== actual) {
50-
const valueInfo = `期望 ${JSON.stringify(expected)}, 实际 ${JSON.stringify(actual)}`;
51-
const error = message ? `${message} - ${valueInfo}` : `断言失败: ${valueInfo}`;
52-
throw new Error(error);
53-
}
34+
}
35+
36+
function testSync(name, fn) {
37+
testResults.total++;
38+
try {
39+
fn();
40+
testResults.passed++;
41+
console.log(`%c✓ ${name}`, "color: green;");
42+
return true;
43+
} catch (error) {
44+
testResults.failed++;
45+
console.error(`%c✗ ${name}`, "color: red;", error);
46+
return false;
5447
}
48+
}
49+
50+
// assert(expected, actual, message) - 比较两个值是否相等
51+
function assert(expected, actual, message) {
52+
if (expected !== actual) {
53+
const valueInfo = `期望 ${JSON.stringify(expected)}, 实际 ${JSON.stringify(actual)}`;
54+
const error = message ? `${message} - ${valueInfo}` : `断言失败: ${valueInfo}`;
55+
throw new Error(error);
56+
}
57+
}
5558

56-
// assertTrue(condition, message) - 断言条件为真
57-
function assertTrue(condition, message) {
58-
if (!condition) {
59-
throw new Error(message || "断言失败: 期望条件为真");
60-
}
59+
// assertTrue(condition, message) - 断言条件为真
60+
function assertTrue(condition, message) {
61+
if (!condition) {
62+
throw new Error(message || "断言失败: 期望条件为真");
6163
}
64+
}
65+
66+
console.log("%c=== Content环境 GM API 测试开始 ===", "color: blue; font-size: 16px; font-weight: bold;");
67+
68+
let testResults = {
69+
passed: 0,
70+
failed: 0,
71+
total: 0,
72+
};
73+
74+
// 同步测试
75+
76+
// ============ GM_addElement/GM_addStyle 测试 ============
77+
console.log("\n%c--- DOM操作 API 测试 ---", "color: orange; font-weight: bold;");
78+
79+
testSync("GM_addElement", () => {
80+
const element = GM_addElement("div", {
81+
textContent: "GM_addElement测试元素",
82+
style: "display:none;",
83+
id: "gm-test-element",
84+
});
85+
assertTrue(element !== null && element !== undefined, "GM_addElement应该返回元素");
86+
assert("gm-test-element", element.id, "元素ID应该正确");
87+
assert("DIV", element.tagName, "元素标签应该是DIV");
88+
console.log("返回元素:", element);
89+
// 清理测试元素
90+
element.parentNode.removeChild(element);
91+
});
92+
93+
testSync("GM_addStyle", () => {
94+
const styleElement = GM_addStyle(`
95+
.gm-style-test {
96+
color: #10b981 !important;
97+
}
98+
`);
99+
assertTrue(styleElement !== null && styleElement !== undefined, "GM_addStyle应该返回样式元素");
100+
assertTrue(styleElement.tagName === "STYLE" || styleElement.sheet, "应该返回STYLE元素或样式对象");
101+
console.log("返回样式元素:", styleElement);
102+
// 清理测试样式
103+
styleElement.parentNode.removeChild(styleElement);
104+
});
105+
106+
(async function () {
107+
"use strict";
62108

63109
// ============ 早期脚本环境检查 ============
64110
console.log("\n%c--- 早期脚本环境检查 ---", "color: orange; font-weight: bold;");
@@ -107,37 +153,11 @@
107153
console.log("脚本注入到:", node.tagName);
108154
});
109155

110-
// ============ GM_addElement/GM_addStyle 测试 ============
111-
console.log("\n%c--- DOM操作 API 测试 ---", "color: orange; font-weight: bold;");
112-
113-
await test("GM_addElement", () => {
114-
const element = GM_addElement("div", {
115-
textContent: "GM_addElement测试元素",
116-
style: "display:none;",
117-
id: "gm-test-element",
118-
});
119-
assertTrue(element !== null && element !== undefined, "GM_addElement应该返回元素");
120-
assert("gm-test-element", element.id, "元素ID应该正确");
121-
assert("DIV", element.tagName, "元素标签应该是DIV");
122-
console.log("返回元素:", element);
123-
});
124-
125-
await test("GM_addStyle", () => {
126-
const styleElement = GM_addStyle(`
127-
.gm-style-test {
128-
color: #10b981 !important;
129-
}
130-
`);
131-
assertTrue(styleElement !== null && styleElement !== undefined, "GM_addStyle应该返回样式元素");
132-
assertTrue(styleElement.tagName === "STYLE" || styleElement.sheet, "应该返回STYLE元素或样式对象");
133-
console.log("返回样式元素:", styleElement);
134-
});
135-
136156
// ============ GM_log 测试 ============
137157
console.log("\n%c--- GM_log 测试 ---", "color: orange; font-weight: bold;");
138158

139159
await test("GM_log", () => {
140-
GM_log("测试日志输出", { type: "test", value: 123 });
160+
GM_log("测试日志输出", "info", { type: "test", value: 123 });
141161
// GM_log本身不返回值,只要不抛出异常就算成功
142162
assertTrue(true, "GM_log应该能正常输出");
143163
});

example/tests/early_test.js

Lines changed: 82 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,91 @@
1717
// @run-at document-start
1818
// ==/UserScript==
1919

20-
(async function () {
21-
"use strict";
22-
23-
console.log("%c=== 早期脚本 GM API 测试开始 ===", "color: blue; font-size: 16px; font-weight: bold;");
24-
25-
let testResults = {
26-
passed: 0,
27-
failed: 0,
28-
total: 0,
29-
};
30-
31-
// 测试辅助函数(支持同步和异步)
32-
async function test(name, fn) {
33-
testResults.total++;
34-
try {
35-
await fn();
36-
testResults.passed++;
37-
console.log(`%c✓ ${name}`, "color: green;");
38-
return true;
39-
} catch (error) {
40-
testResults.failed++;
41-
console.error(`%c✗ ${name}`, "color: red;", error);
42-
return false;
43-
}
20+
// 测试辅助函数(支持同步和异步)
21+
async function test(name, fn) {
22+
testResults.total++;
23+
try {
24+
await fn();
25+
testResults.passed++;
26+
console.log(`%c✓ ${name}`, "color: green;");
27+
return true;
28+
} catch (error) {
29+
testResults.failed++;
30+
console.error(`%c✗ ${name}`, "color: red;", error);
31+
return false;
4432
}
45-
46-
// assert(expected, actual, message) - 比较两个值是否相等
47-
function assert(expected, actual, message) {
48-
if (expected !== actual) {
49-
const valueInfo = `期望 ${JSON.stringify(expected)}, 实际 ${JSON.stringify(actual)}`;
50-
const error = message ? `${message} - ${valueInfo}` : `断言失败: ${valueInfo}`;
51-
throw new Error(error);
52-
}
33+
}
34+
35+
function testSync(name, fn) {
36+
testResults.total++;
37+
try {
38+
fn();
39+
testResults.passed++;
40+
console.log(`%c✓ ${name}`, "color: green;");
41+
return true;
42+
} catch (error) {
43+
testResults.failed++;
44+
console.error(`%c✗ ${name}`, "color: red;", error);
45+
return false;
5346
}
47+
}
48+
49+
// assert(expected, actual, message) - 比较两个值是否相等
50+
function assert(expected, actual, message) {
51+
if (expected !== actual) {
52+
const valueInfo = `期望 ${JSON.stringify(expected)}, 实际 ${JSON.stringify(actual)}`;
53+
const error = message ? `${message} - ${valueInfo}` : `断言失败: ${valueInfo}`;
54+
throw new Error(error);
55+
}
56+
}
5457

55-
// assertTrue(condition, message) - 断言条件为真
56-
function assertTrue(condition, message) {
57-
if (!condition) {
58-
throw new Error(message || "断言失败: 期望条件为真");
59-
}
58+
// assertTrue(condition, message) - 断言条件为真
59+
function assertTrue(condition, message) {
60+
if (!condition) {
61+
throw new Error(message || "断言失败: 期望条件为真");
6062
}
63+
}
64+
65+
console.log("%c=== 早期脚本 GM API 测试开始 ===", "color: blue; font-size: 16px; font-weight: bold;");
66+
67+
let testResults = {
68+
passed: 0,
69+
failed: 0,
70+
total: 0,
71+
};
72+
73+
// ============ GM_addElement/GM_addStyle 测试 ============
74+
console.log("\n%c--- DOM操作 API 测试 ---", "color: orange; font-weight: bold;");
75+
76+
testSync("GM_addElement", () => {
77+
const element = GM_addElement("div", {
78+
textContent: "GM_addElement测试元素",
79+
style: "display:none;",
80+
id: "gm-test-element",
81+
});
82+
assertTrue(element !== null && element !== undefined, "GM_addElement应该返回元素");
83+
assert("gm-test-element", element.id, "元素ID应该正确");
84+
assert("DIV", element.tagName, "元素标签应该是DIV");
85+
console.log("返回元素:", element);
86+
// 清理测试元素
87+
element.parentNode.removeChild(element);
88+
});
89+
90+
testSync("GM_addStyle", () => {
91+
const styleElement = GM_addStyle(`
92+
.gm-style-test {
93+
color: #10b981 !important;
94+
}
95+
`);
96+
assertTrue(styleElement !== null && styleElement !== undefined, "GM_addStyle应该返回样式元素");
97+
assertTrue(styleElement.tagName === "STYLE" || styleElement.sheet, "应该返回STYLE元素或样式对象");
98+
console.log("返回样式元素:", styleElement);
99+
// 清理测试样式
100+
styleElement.parentNode.removeChild(styleElement);
101+
});
102+
103+
(async function () {
104+
"use strict";
61105

62106
// ============ 早期脚本环境检查 ============
63107
console.log("\n%c--- 早期脚本环境检查 ---", "color: orange; font-weight: bold;");
@@ -135,37 +179,11 @@
135179
}
136180
});
137181

138-
// ============ GM_addElement/GM_addStyle 测试 ============
139-
console.log("\n%c--- DOM操作 API 测试 ---", "color: orange; font-weight: bold;");
140-
141-
await test("GM_addElement", () => {
142-
const element = GM_addElement("div", {
143-
textContent: "GM_addElement测试元素",
144-
style: "display:none;",
145-
id: "gm-test-element",
146-
});
147-
assertTrue(element !== null && element !== undefined, "GM_addElement应该返回元素");
148-
assert("gm-test-element", element.id, "元素ID应该正确");
149-
assert("DIV", element.tagName, "元素标签应该是DIV");
150-
console.log("返回元素:", element);
151-
});
152-
153-
await test("GM_addStyle", () => {
154-
const styleElement = GM_addStyle(`
155-
.gm-style-test {
156-
color: #10b981 !important;
157-
}
158-
`);
159-
assertTrue(styleElement !== null && styleElement !== undefined, "GM_addStyle应该返回样式元素");
160-
assertTrue(styleElement.tagName === "STYLE" || styleElement.sheet, "应该返回STYLE元素或样式对象");
161-
console.log("返回样式元素:", styleElement);
162-
});
163-
164182
// ============ GM_log 测试 ============
165183
console.log("\n%c--- GM_log 测试 ---", "color: orange; font-weight: bold;");
166184

167185
await test("GM_log", () => {
168-
GM_log("测试日志输出", { type: "test", value: 123 });
186+
GM_log("测试日志输出", "info", { type: "test", value: 123 });
169187
// GM_log本身不返回值,只要不抛出异常就算成功
170188
assertTrue(true, "GM_log应该能正常输出");
171189
});

0 commit comments

Comments
 (0)