Repository navigation
Expand file tree
/
Copy pathwindow_message_test.js
More file actions
155 lines (143 loc) · 4.75 KB
/
Copy pathwindow_message_test.js
File metadata and controls
155 lines (143 loc) · 4.75 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
// ==UserScript==
// @name WindowMessage Transport Test
// @namespace https://docs.scriptcat.org/
// @version 0.1.0
// @description Verifies the WindowMessage paths used by ScriptCat sandbox and offscreen pages.
// @author ScriptCat
// @match *://*/*?WINDOW_MESSAGE_TEST_SC
// @grant GM.xmlHttpRequest
// @grant GM_xmlhttpRequest
// @grant GM.setClipboard
// @grant unsafeWindow
// @require https://cdn.jsdelivr.net/gh/scriptscat/scriptcat@36ab4ce5ff23c820a32cd13ac5a04d8834ab4d82/example/tests/lib/sctest.js
// @connect httpbingo.org
// @run-at document-end
// @noframes
// ==/UserScript==
(async function () {
"use strict";
const { describe, check, expect, run } = SCTest.create({ name: "WindowMessage 传输测试" });
function withTimeout(promise, label, ms = 10000) {
let timer = null;
const timeout = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);
});
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}
describe("Sandbox endpoint", () => {
check(
"自动断言",
"default userscript runs in the sandbox window",
() => {
expect(typeof unsafeWindow).toBe("object");
expect(window !== unsafeWindow).toBeTruthy();
expect(self).toBe(window);
expect(globalThis).toBe(window);
},
null,
null,
null
);
});
describe("One-shot sendMessage path", () => {
check(
"自动断言",
"GM.setClipboard resolves through the offscreen sendMessage bridge",
async () => {
const text = `ScriptCat WindowMessage ${Date.now()} ${Math.random().toString(36).slice(2)}`;
await withTimeout(GM.setClipboard(text, { type: "text", mimetype: "text/plain" }), "GM.setClipboard");
},
null,
null,
null
);
});
describe("Long-lived connect path", () => {
check(
"自动断言",
"GM.xmlHttpRequest receives offscreen response data over a connect channel",
async () => {
const marker = `window-message-${Date.now()}-${Math.random().toString(36).slice(2)}`;
const response = await withTimeout(
GM.xmlHttpRequest({
method: "GET",
url: `https://httpbingo.org/get?marker=${encodeURIComponent(marker)}`,
responseType: "json",
}),
"GM.xmlHttpRequest"
);
expect(response.status).toBe(200);
expect(response.finalUrl.includes("httpbingo.org/get")).toBeTruthy();
expect(typeof response.responseHeaders === "string").toBeTruthy();
expect(response.response && typeof response.response === "object").toBeTruthy();
const args = response.response.args || response.response.query || response.response.params || {};
expect(args.marker?.[0] ?? args.marker).toBe(marker);
},
null,
null,
null
);
check(
"自动断言",
"GM_xmlhttpRequest forwards readyState events over the connect channel",
async () => {
const states = [];
const response = await withTimeout(
new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "GET",
url: "https://httpbingo.org/bytes/64",
onreadystatechange: (res) => {
states.push(res.readyState);
},
onload: resolve,
onerror: reject,
ontimeout: reject,
timeout: 10000,
});
}),
"GM_xmlhttpRequest readyState"
);
expect(response.status).toBe(200);
expect(states.includes(4)).toBeTruthy();
expect(response.responseText.length > 0).toBeTruthy();
},
null,
null,
null
);
check(
"自动断言",
"GM_xmlhttpRequest abort disconnects a pending connect channel",
async () => {
await withTimeout(
new Promise((resolve, reject) => {
const request = GM_xmlhttpRequest({
method: "GET",
url: "https://httpbingo.org/delay/5",
onload: () => reject(new Error("request loaded before abort")),
onerror: reject,
ontimeout: reject,
onabort: (res) => {
try {
expect(res.readyState).toBe(0);
expect(res.status).toBe(0);
resolve();
} catch (error) {
reject(error);
}
},
timeout: 10000,
});
setTimeout(() => request.abort(), 100);
}),
"GM_xmlhttpRequest abort"
);
},
null,
null,
null
);
});
await run();
})();