blob: 239fa7330371bceb58567186d18095d04e4eee5b (
plain)
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
|
<script>
function with_iframe(url) {
return new Promise(resolve => {
let frame = document.createElement('iframe');
frame.src = url;
frame.onload = () => { resolve(frame); };
document.body.appendChild(frame);
});
}
function with_sandboxed_iframe(url, sandbox) {
return new Promise(resolve => {
let frame = document.createElement('iframe');
frame.sandbox = sandbox;
frame.src = url;
frame.onload = () => { resolve(frame); };
document.body.appendChild(frame);
});
}
function fetch_from_worker(url) {
return new Promise(resolve => {
let blob = new Blob([
`fetch('${url}', {mode: 'no-cors'})` +
" .then(() => { self.postMessage('OK'); });"]);
let worker_url = URL.createObjectURL(blob);
let worker = new Worker(worker_url);
worker.onmessage = resolve;
});
}
function run_test(type) {
const base_path = location.href;
switch (type) {
case 'fetch':
return fetch(`${base_path}&test=fetch`, {mode: 'no-cors'});
case 'fetch-from-worker':
return fetch_from_worker(`${base_path}&test=fetch-from-worker`);
case 'iframe':
return with_iframe(`${base_path}&test=iframe`);
case 'sandboxed-iframe':
return with_sandboxed_iframe(`${base_path}&test=sandboxed-iframe`,
"allow-scripts");
case 'sandboxed-iframe-same-origin':
return with_sandboxed_iframe(
`${base_path}&test=sandboxed-iframe-same-origin`,
"allow-scripts allow-same-origin");
default:
return Promise.reject(`Unknown type: ${type}`);
}
}
window.onmessage = event => {
let id = event.data['id'];
run_test(event.data['type'])
.then(() => {
window.top.postMessage({id: id, result: 'done'}, '*');
})
.catch(e => {
window.top.postMessage({id: id, result: 'error: ' + e.toString()}, '*');
});
};
</script>
|