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