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
|
<!DOCTYPE html>
<script src="/common/get-host-info.sub.js"></script>
<script src="test-helpers.sub.js"></script>
<script>
var host_info = get_host_info();
var SCOPE = 'navigation-redirect-scope1.py';
var SCRIPT = 'redirect-worker.js';
var registration;
var worker;
var wait_for_worker_promise = navigator.serviceWorker.getRegistration(SCOPE)
.then(function(reg) {
if (reg)
return reg.unregister();
})
.then(function() {
return navigator.serviceWorker.register(SCRIPT, {scope: SCOPE});
})
.then(function(reg) {
registration = reg;
worker = reg.installing;
return new Promise(function(resolve) {
worker.addEventListener('statechange', function() {
if (worker.state == 'activated')
resolve();
});
});
});
function send_result(message_id, result) {
window.parent.postMessage(
{id: message_id, result: result},
host_info['HTTPS_ORIGIN']);
}
function get_request_infos(worker) {
return new Promise(function(resolve) {
var channel = new MessageChannel();
channel.port1.onmessage = (msg) => {
resolve(msg.data.requestInfos);
};
worker.postMessage({command: 'getRequestInfos', port: channel.port2},
[channel.port2]);
});
}
function get_clients(worker, actual_ids) {
return new Promise(function(resolve) {
var channel = new MessageChannel();
channel.port1.onmessage = (msg) => {
resolve(msg.data.clients);
};
worker.postMessage({
command: 'getClients',
actual_ids,
port: channel.port2
}, [channel.port2]);
});
}
window.addEventListener('message', on_message, false);
function on_message(e) {
if (e.origin != host_info['HTTPS_ORIGIN']) {
console.error('invalid origin: ' + e.origin);
return;
}
const command = e.data.message.command;
if (command == 'wait_for_worker') {
wait_for_worker_promise.then(function() { send_result(e.data.id, 'ok'); });
} else if (command == 'get_request_infos') {
get_request_infos(worker)
.then(function(data) {
send_result(e.data.id, data);
});
} else if (command == 'get_clients') {
get_clients(worker, e.data.message.actual_ids)
.then(function(data) {
send_result(e.data.id, data);
});
} else if (command == 'unregister') {
registration.unregister()
.then(function() {
send_result(e.data.id, 'ok');
});
}
}
</script>
|