Repository navigation
Expand file tree
/
Copy pathearly_inject_page_test.js
More file actions
280 lines (256 loc) · 7.91 KB
/
Copy pathearly_inject_page_test.js
File metadata and controls
280 lines (256 loc) · 7.91 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// ==UserScript==
// @name Early-start Test (page 环境)
// @namespace https://docs.scriptcat.org/
// @version 0.1.0
// @description early-start 可以比 document-start 更早执行
// @match https://content-security-policy.com/?early_inject_page
// @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 测试(page 环境)", 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
);
check(
"自动断言",
"检查页面CSP",
async () => {
console.log("开始CSP检测...");
console.log("当前页面URL:", window.location.href);
// 尝试插入外部script来测试CSP
console.log("\n%c测试外部script插入", "color: #3b82f6;");
const testScript = document.createElement("script");
testScript.src = "data:application/javascript,window.__cspTestExternal=true;";
testScript.id = "csp-test-external";
// 使用Promise等待加载结果
const loadResult = await new Promise((resolve, reject) => {
testScript.onload = () => {
console.log("%c✓ 外部script加载成功 - 无CSP限制或已允许", "color: #ef4444;");
resolve({ success: true, blocked: false });
};
testScript.onerror = (error) => {
console.log("%c✓ 外部script加载失败 - 被CSP阻止(符合预期)", "color: #10b981;");
console.log("CSP错误详情:", error);
resolve({ success: false, blocked: true, error });
};
// 设置超时(1秒)
setTimeout(() => {
reject(new Error("Script加载超时"));
}, 1000);
// 插入元素到DOM
console.log("正在插入script元素到DOM...");
document.documentElement.appendChild(testScript);
console.log("script元素已同步插入DOM,等待异步加载结果...");
});
// 验证检测结果
if (loadResult.blocked) {
console.log("%c✓ 页面存在CSP策略限制(符合预期)", "color: #10b981; font-weight: bold;");
expect(true).toBeTruthy();
} else if (loadResult.success) {
console.log("%c✗ 页面无CSP限制或已允许该资源(不符合预期)", "color: #ef4444; font-weight: bold;");
expect(false).toBeTruthy();
}
},
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", "早期脚本测试值");
const value = GM_getValue("test_key");
expect(value).toBe("早期脚本测试值");
},
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: "early" };
GM_setValue("test_object", obj);
const value = GM_getValue("test_object", {});
expect(value.name).toBe("ScriptCat");
expect(value.type).toBe("early");
},
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();
})();