|
18 | 18 | // @run-at document-start |
19 | 19 | // ==/UserScript== |
20 | 20 |
|
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; |
45 | 33 | } |
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; |
54 | 47 | } |
| 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 | +} |
55 | 58 |
|
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 || "断言失败: 期望条件为真"); |
61 | 63 | } |
| 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"; |
62 | 108 |
|
63 | 109 | // ============ 早期脚本环境检查 ============ |
64 | 110 | console.log("\n%c--- 早期脚本环境检查 ---", "color: orange; font-weight: bold;"); |
|
107 | 153 | console.log("脚本注入到:", node.tagName); |
108 | 154 | }); |
109 | 155 |
|
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 | | - |
136 | 156 | // ============ GM_log 测试 ============ |
137 | 157 | console.log("\n%c--- GM_log 测试 ---", "color: orange; font-weight: bold;"); |
138 | 158 |
|
139 | 159 | await test("GM_log", () => { |
140 | | - GM_log("测试日志输出", { type: "test", value: 123 }); |
| 160 | + GM_log("测试日志输出", "info", { type: "test", value: 123 }); |
141 | 161 | // GM_log本身不返回值,只要不抛出异常就算成功 |
142 | 162 | assertTrue(true, "GM_log应该能正常输出"); |
143 | 163 | }); |
|
0 commit comments