Repository navigation
Expand file tree
/
Copy pathearly_inject_content_test.js
More file actions
251 lines (234 loc) · 6.72 KB
/
Copy pathearly_inject_content_test.js
File metadata and controls
251 lines (234 loc) · 6.72 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
// ==UserScript==
// @name Early-start Test (content 环境)
// @namespace https://docs.scriptcat.org/
// @version 0.1.0
// @description early-start 可以比 document-start 更早执行
// @match https://content-security-policy.com/?early_inject_content
// @inject-into content
// @early-start
// @grant GM_addElement
// @grant GM_addStyle
// @grant GM_log
// @grant GM_info
// @grant GM_setValue
// @grant GM.setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_listValues
// @require https://cdn.jsdelivr.net/gh/scriptscat/scriptcat@36ab4ce5ff23c820a32cd13ac5a04d8834ab4d82/example/tests/lib/sctest.js
// @run-at document-start
// ==/UserScript==
// reporter: "console" — 用例本身在断言 document-start 时 DOM 应保持原始态(head/body 均不存在、
// 唯一节点 innerHTML 为空);Panel reporter 会在 run() 开始时把浮层面板挂到 document.documentElement
// 下,抢在该断言之前弄脏这个待验证的原始态,所以这里必须关闭 Panel,只留 Console 通道。
const { describe, check, expect, run } = SCTest.create({ name: "Early-start 测试(content 环境)", reporter: "console" });
// 同步测试
describe("DOM操作 API 测试", () => {
check(
"自动断言",
"GM_addElement",
() => {
const element = GM_addElement("div", {
textContent: "GM_addElement测试元素",
style: "display:none;",
id: "gm-test-element",
});
expect(element !== null && element !== undefined).toBeTruthy();
expect(element.id).toBe("gm-test-element");
expect(element.tagName).toBe("DIV");
// 清理测试元素
element.parentNode.removeChild(element);
},
null,
null,
null
);
check(
"自动断言",
"GM_addStyle",
() => {
const styleElement = GM_addStyle(`
.gm-style-test {
color: #10b981 !important;
}
`);
expect(styleElement !== null && styleElement !== undefined).toBeTruthy();
expect(styleElement.tagName === "STYLE" || styleElement.sheet).toBeTruthy();
// 清理测试样式
styleElement.parentNode.removeChild(styleElement);
},
null,
null,
null
);
});
(async function () {
"use strict";
describe("早期脚本环境检查", () => {
check(
"自动断言",
"检查 document.head 不存在",
() => {
console.log("document.head 存在:", !!document.head);
console.log("document.head 值:", document.head);
// 早期脚本运行时 document.head 应该不存在
expect(document.head === null || document.head === undefined).toBeTruthy();
},
null,
null,
null
);
check(
"自动断言",
"检查 document.body 不存在",
() => {
console.log("document.body 存在:", !!document.body);
console.log("document.body 值:", document.body);
// 早期脚本运行时 document.body 应该不存在
expect(document.body === null || document.body === undefined).toBeTruthy();
},
null,
null,
null
);
check(
"自动断言",
"检查可用的DOM节点应该是HTML元素",
() => {
const firstElement = document.querySelector("*");
console.log("querySelector('*') 找到的第一个元素:", firstElement?.tagName);
expect(firstElement !== null).toBeTruthy();
expect(firstElement.tagName).toBe("HTML");
expect(firstElement.innerHTML).toBe("");
},
null,
null,
null
);
});
describe("CSP绕过测试", () => {
check(
"自动断言",
"CSP绕过 - 内联脚本",
() => {
const script = document.createElement("script");
script.textContent = 'console.log("Content环境绕过CSP测试");';
// 早期脚本运行时 document.head 和 document.body 不存在
// 使用 querySelector("*") 查找第一个可用的元素(应该是HTML元素)进行注入
let node = document.querySelector("*");
expect(node !== null).toBeTruthy();
node.appendChild(script);
expect(script.parentNode === node).toBeTruthy();
expect(node.tagName).toBe("HTML");
},
null,
null,
null
);
});
describe("GM_log 测试", () => {
check(
"自动断言",
"GM_log",
() => {
GM_log("测试日志输出", "info", { type: "test", value: 123 });
// GM_log本身不返回值,只要不抛出异常就算成功
expect(true).toBeTruthy();
},
null,
null,
null
);
});
describe("GM_info 测试", () => {
check(
"自动断言",
"GM_info",
() => {
expect(typeof GM_info === "object").toBeTruthy();
expect(!!GM_info.script).toBeTruthy();
expect(!!GM_info.script.name).toBeTruthy();
},
null,
null,
null
);
});
describe("GM 存储 API 测试", () => {
check(
"自动断言",
"GM_setValue - 字符串",
async () => {
await GM.setValue("test_key", "content环境测试值");
const value = GM_getValue("test_key");
expect(value).toBe("content环境测试值");
},
null,
null,
null
);
check(
"自动断言",
"GM_setValue - 数字",
() => {
GM_setValue("test_number", 12345);
const value = GM_getValue("test_number");
expect(value).toBe(12345);
},
null,
null,
null
);
check(
"自动断言",
"GM_setValue - 对象",
() => {
const obj = { name: "ScriptCat", type: "content" };
GM_setValue("test_object", obj);
const value = GM_getValue("test_object", {});
expect(value.name).toBe("ScriptCat");
expect(value.type).toBe("content");
},
null,
null,
null
);
check(
"自动断言",
"GM_getValue - 默认值",
() => {
const value = GM_getValue("non_existent_key", "默认值");
expect(value).toBe("默认值");
},
null,
null,
null
);
check(
"自动断言",
"GM_listValues",
() => {
const keys = GM_listValues();
expect(Array.isArray(keys)).toBeTruthy();
expect(keys.length >= 3).toBeTruthy();
},
null,
null,
null
);
check(
"自动断言",
"GM_deleteValue",
() => {
GM_setValue("test_delete", "to_be_deleted");
expect(GM_getValue("test_delete")).toBe("to_be_deleted");
GM_deleteValue("test_delete");
expect(GM_getValue("test_delete", null)).toBe(null);
},
null,
null,
null
);
});
await run();
})();