|
63 | 63 | // ============ 早期脚本环境检查 ============ |
64 | 64 | console.log("\n%c--- 早期脚本环境检查 ---", "color: orange; font-weight: bold;"); |
65 | 65 |
|
66 | | - test("检查 document.head 不存在", () => { |
| 66 | + await test("检查 document.head 不存在", () => { |
67 | 67 | console.log("document.head 存在:", !!document.head); |
68 | 68 | console.log("document.head 值:", document.head); |
69 | 69 | // 早期脚本运行时 document.head 应该不存在 |
70 | 70 | assertTrue(document.head === null || document.head === undefined, "早期脚本运行时 document.head 应该不存在"); |
71 | 71 | }); |
72 | 72 |
|
73 | | - test("检查 document.body 不存在", () => { |
| 73 | + await test("检查 document.body 不存在", () => { |
74 | 74 | console.log("document.body 存在:", !!document.body); |
75 | 75 | console.log("document.body 值:", document.body); |
76 | 76 | // 早期脚本运行时 document.body 应该不存在 |
77 | 77 | assertTrue(document.body === null || document.body === undefined, "早期脚本运行时 document.body 应该不存在"); |
78 | 78 | }); |
79 | 79 |
|
80 | | - test("检查可用的DOM节点应该是HTML元素", () => { |
| 80 | + await test("检查可用的DOM节点应该是HTML元素", () => { |
81 | 81 | const firstElement = document.querySelector("*"); |
82 | 82 | console.log("querySelector('*') 找到的第一个元素:", firstElement?.tagName); |
83 | 83 | assertTrue(firstElement !== null, "应该能找到第一个DOM节点"); |
|
94 | 94 | // ============ CSP绕过测试 ============ |
95 | 95 | console.log("\n%c--- CSP绕过测试 ---", "color: orange; font-weight: bold;"); |
96 | 96 |
|
97 | | - test("CSP绕过 - 内联脚本", () => { |
| 97 | + await test("CSP绕过 - 内联脚本", () => { |
98 | 98 | const script = document.createElement("script"); |
99 | 99 | script.textContent = 'console.log("Content环境绕过CSP测试");'; |
100 | 100 | // 早期脚本运行时 document.head 和 document.body 不存在 |
|
110 | 110 | // ============ GM_addElement/GM_addStyle 测试 ============ |
111 | 111 | console.log("\n%c--- DOM操作 API 测试 ---", "color: orange; font-weight: bold;"); |
112 | 112 |
|
113 | | - test("GM_addElement", () => { |
| 113 | + await test("GM_addElement", () => { |
114 | 114 | const element = GM_addElement("div", { |
115 | 115 | textContent: "GM_addElement测试元素", |
116 | 116 | style: "display:none;", |
|
122 | 122 | console.log("返回元素:", element); |
123 | 123 | }); |
124 | 124 |
|
125 | | - test("GM_addStyle", () => { |
| 125 | + await test("GM_addStyle", () => { |
126 | 126 | const styleElement = GM_addStyle(` |
127 | 127 | .gm-style-test { |
128 | 128 | color: #10b981 !important; |
|
136 | 136 | // ============ GM_log 测试 ============ |
137 | 137 | console.log("\n%c--- GM_log 测试 ---", "color: orange; font-weight: bold;"); |
138 | 138 |
|
139 | | - test("GM_log", () => { |
| 139 | + await test("GM_log", () => { |
140 | 140 | GM_log("测试日志输出", { type: "test", value: 123 }); |
141 | 141 | // GM_log本身不返回值,只要不抛出异常就算成功 |
142 | 142 | assertTrue(true, "GM_log应该能正常输出"); |
|
145 | 145 | // ============ GM_info 测试 ============ |
146 | 146 | console.log("\n%c--- GM_info 测试 ---", "color: orange; font-weight: bold;"); |
147 | 147 |
|
148 | | - test("GM_info", () => { |
| 148 | + await test("GM_info", () => { |
149 | 149 | assertTrue(typeof GM_info === "object", "GM_info应该是对象"); |
150 | 150 | assertTrue(!!GM_info.script, "GM_info.script应该存在"); |
151 | 151 | assertTrue(!!GM_info.script.name, "GM_info.script.name应该存在"); |
|
161 | 161 | assert("content环境测试值", value, "应该正确保存和读取字符串"); |
162 | 162 | }); |
163 | 163 |
|
164 | | - test("GM_setValue - 数字", () => { |
| 164 | + await test("GM_setValue - 数字", () => { |
165 | 165 | GM_setValue("test_number", 12345); |
166 | 166 | const value = GM_getValue("test_number"); |
167 | 167 | assert(12345, value, "应该正确保存和读取数字"); |
168 | 168 | }); |
169 | 169 |
|
170 | | - test("GM_setValue - 对象", () => { |
| 170 | + await test("GM_setValue - 对象", () => { |
171 | 171 | const obj = { name: "ScriptCat", type: "content" }; |
172 | 172 | GM_setValue("test_object", obj); |
173 | 173 | const value = GM_getValue("test_object", {}); |
174 | 174 | assert("ScriptCat", value.name, "对象的name属性应该正确"); |
175 | 175 | assert("content", value.type, "对象的type属性应该正确"); |
176 | 176 | }); |
177 | 177 |
|
178 | | - test("GM_getValue - 默认值", () => { |
| 178 | + await test("GM_getValue - 默认值", () => { |
179 | 179 | const value = GM_getValue("non_existent_key", "默认值"); |
180 | 180 | assert("默认值", value, "不存在的键应该返回默认值"); |
181 | 181 | }); |
182 | 182 |
|
183 | | - test("GM_listValues", () => { |
| 183 | + await test("GM_listValues", () => { |
184 | 184 | const keys = GM_listValues(); |
185 | 185 | assertTrue(Array.isArray(keys), "GM_listValues应该返回数组"); |
186 | 186 | assertTrue(keys.length >= 3, "应该至少有3个存储键"); |
187 | 187 | console.log("存储的键:", keys); |
188 | 188 | }); |
189 | 189 |
|
190 | | - test("GM_deleteValue", () => { |
| 190 | + await test("GM_deleteValue", () => { |
191 | 191 | GM_setValue("test_delete", "to_be_deleted"); |
192 | 192 | assert("to_be_deleted", GM_getValue("test_delete"), "值应该存在"); |
193 | 193 | GM_deleteValue("test_delete"); |
|
0 commit comments