Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs executeScript Test</title>
5
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
6
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
7
<script type="text/javascript" src="head.js"></script>
8
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
9
</head>
10
<body>
11
12
<script type="text/javascript">
13
"use strict";
14
15
add_task(async function testExecuteScript() {
17
const URL = BASE + "file_iframe_document.html";
18
19
let win = window.open(URL);
20
await new Promise(resolve => win.addEventListener("load", resolve, {once: true}));
21
22
async function background() {
23
try {
24
let [tab] = await browser.tabs.query({active: true, currentWindow: true});
25
// TODO bug 1565536: tab.active is broken in GeckoView.
26
browser.test.assertEq(undefined, tab, "currentWindow's tab is not active (bug 1565536)");
27
[tab] = await browser.tabs.query({currentWindow: true});
28
29
let frames = await browser.webNavigation.getAllFrames({tabId: tab.id});
30
31
browser.test.log(`FRAMES: ${frames[1].frameId} ${JSON.stringify(frames)}\n`);
32
await Promise.all([
33
browser.tabs.executeScript({
34
code: "42",
35
}).then(result => {
36
browser.test.assertEq(1, result.length, "Expected one callback result");
37
browser.test.assertEq(42, result[0], "Expected callback result");
38
}),
39
40
browser.tabs.executeScript({
41
file: "script.js",
42
code: "42",
43
}).then(result => {
44
browser.test.fail("Expected not to be able to execute a script with both file and code");
45
}, error => {
46
browser.test.assertTrue(/a 'code' or a 'file' property, but not both/.test(error.message),
47
"Got expected error");
48
}),
49
50
browser.tabs.executeScript({
51
file: "script.js",
52
}).then(result => {
53
browser.test.assertEq(1, result.length, "Expected one callback result");
54
browser.test.assertEq(undefined, result[0], "Expected callback result");
55
}),
56
57
browser.tabs.executeScript({
58
file: "script2.js",
59
}).then(result => {
60
browser.test.assertEq(1, result.length, "Expected one callback result");
61
browser.test.assertEq(27, result[0], "Expected callback result");
62
}),
63
64
browser.tabs.executeScript({
65
code: "location.href;",
66
allFrames: true,
67
}).then(result => {
68
browser.test.assertTrue(Array.isArray(result), "Result is an array");
69
70
browser.test.assertEq(2, result.length, "Result has correct length");
71
72
browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "First result is correct");
73
browser.test.assertEq("http://mochi.test:8888/", result[1], "Second result is correct");
74
}),
75
76
browser.tabs.executeScript({
77
code: "location.href;",
78
allFrames: true,
79
matchAboutBlank: true,
80
}).then(result => {
81
browser.test.assertTrue(Array.isArray(result), "Result is an array");
82
83
browser.test.assertEq(3, result.length, "Result has correct length");
84
85
browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "First result is correct");
86
browser.test.assertEq("http://mochi.test:8888/", result[1], "Second result is correct");
87
browser.test.assertEq("about:blank", result[2], "Thirds result is correct");
88
}),
89
90
browser.tabs.executeScript({
91
code: "location.href;",
92
runAt: "document_end",
93
}).then(result => {
94
browser.test.assertEq(1, result.length, "Expected callback result");
95
browser.test.assertEq("string", typeof result[0], "Result is a string");
96
97
browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "Result is correct");
98
}),
99
100
browser.tabs.executeScript({
101
code: "window",
102
}).then(result => {
103
browser.test.fail("Expected error when returning non-structured-clonable object");
104
}, error => {
105
browser.test.assertEq("<anonymous code>", error.fileName, "Got expected fileName");
106
browser.test.assertEq("Script '<anonymous code>' result is non-structured-clonable data",
107
error.message, "Got expected error");
108
}),
109
110
browser.tabs.executeScript({
111
code: "Promise.resolve(window)",
112
}).then(result => {
113
browser.test.fail("Expected error when returning non-structured-clonable object");
114
}, error => {
115
browser.test.assertEq("<anonymous code>", error.fileName, "Got expected fileName");
116
browser.test.assertEq("Script '<anonymous code>' result is non-structured-clonable data",
117
error.message, "Got expected error");
118
}),
119
120
browser.tabs.executeScript({
121
file: "script3.js",
122
}).then(result => {
123
browser.test.fail("Expected error when returning non-structured-clonable object");
124
}, error => {
125
const expected = /Script '.*script3.js' result is non-structured-clonable data/;
126
browser.test.assertTrue(expected.test(error.message), "Got expected error");
127
browser.test.assertTrue(error.fileName.endsWith("script3.js"), "Got expected fileName");
128
}),
129
130
browser.tabs.executeScript({
131
frameId: Number.MAX_SAFE_INTEGER,
132
code: "42",
133
}).then(result => {
134
browser.test.fail("Expected error when specifying invalid frame ID");
135
}, error => {
136
browser.test.assertEq(`Frame not found, or missing host permission`,
137
error.message, "Got expected error");
138
}),
139
140
browser.tabs.create({url: "http://example.net/", active: false}).then(async tab => {
141
await browser.tabs.executeScript(tab.id, {
142
code: "42",
143
}).then(result => {
144
browser.test.fail("Expected error when trying to execute on invalid domain");
145
}, error => {
146
browser.test.assertEq(`Missing host permission for the tab`,
147
error.message, "Got expected error");
148
});
149
150
await browser.tabs.remove(tab.id);
151
}),
152
153
browser.tabs.executeScript({
154
code: "Promise.resolve(42)",
155
}).then(result => {
156
browser.test.assertEq(42, result[0], "Got expected promise resolution value as result");
157
}),
158
159
browser.tabs.executeScript({
160
code: "location.href;",
161
runAt: "document_end",
162
allFrames: true,
163
}).then(result => {
164
browser.test.assertTrue(Array.isArray(result), "Result is an array");
165
166
browser.test.assertEq(2, result.length, "Result has correct length");
167
168
browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "First result is correct");
169
browser.test.assertEq("http://mochi.test:8888/", result[1], "Second result is correct");
170
}),
171
172
browser.tabs.executeScript({
173
code: "location.href;",
174
frameId: frames[0].frameId,
175
}).then(result => {
176
browser.test.assertEq(1, result.length, "Expected one result");
177
browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), `Result for frameId[0] is correct: ${result[0]}`);
178
}),
179
180
browser.tabs.executeScript({
181
code: "location.href;",
182
frameId: frames[1].frameId,
183
}).then(result => {
184
browser.test.assertEq(1, result.length, "Expected one result");
185
browser.test.assertEq("http://mochi.test:8888/", result[0], "Result for frameId[1] is correct");
186
}),
187
188
browser.tabs.create({url: "http://example.com/"}).then(async tab => {
189
let result = await browser.tabs.executeScript(tab.id, {code: "location.href"});
190
191
browser.test.assertEq("http://example.com/", result[0], "Script executed correctly in new tab");
192
193
await browser.tabs.remove(tab.id);
194
}),
195
196
// This currently does not work on Android.
197
/*
198
browser.tabs.create({url: "about:blank"}).then(async tab => {
199
const result = await browser.tabs.executeScript(tab.id, {code: "location.href", matchAboutBlank: true});
200
browser.test.assertEq("about:blank", result[0], "Script executed correctly in new tab");
201
await browser.tabs.remove(tab.id);
202
}),
203
*/
204
205
new Promise(resolve => {
206
browser.runtime.onMessage.addListener(message => {
207
browser.test.assertEq("script ran", message, "Expected runtime message");
208
resolve();
209
});
210
}),
211
]);
212
213
browser.test.notifyPass("executeScript");
214
} catch (e) {
215
browser.test.fail(`Error: ${e} :: ${e.stack}`);
216
browser.test.notifyFail("executeScript");
217
}
218
}
219
220
let extension = ExtensionTestUtils.loadExtension({
221
manifest: {
222
"permissions": ["http://mochi.test/", "http://example.com/", "webNavigation"],
223
},
224
225
background,
226
227
files: {
228
"script.js": function() {
229
browser.runtime.sendMessage("script ran");
230
},
231
232
"script2.js": "27",
233
234
"script3.js": "window",
235
},
236
});
237
238
await extension.startup();
239
240
await extension.awaitFinish("executeScript");
241
242
await extension.unload();
243
244
win.close();
245
});
246
</script>
247
248
</body>
249
</html>