OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <script src="../../resources/get-host-info.js?pipe=sub"></script> | |
3 <script src="test-helpers.js"></script> | |
4 <script> | |
5 var host_info = get_host_info(); | |
6 var SCOPE = 'navigation-redirect-scope1.php'; | |
7 var SCRIPT = 'navigation-redirect-worker.js'; | |
8 | |
9 var registration; | |
10 var worker; | |
11 var wait_for_worker_promise = navigator.serviceWorker.getRegistration(SCOPE) | |
12 .then(function(reg) { | |
13 if (reg) | |
14 return reg.unregister(); | |
15 }) | |
16 .then(function() { | |
17 return navigator.serviceWorker.register(SCRIPT, {scope: SCOPE}); | |
18 }) | |
19 .then(function(reg) { | |
20 registration = reg; | |
21 worker = reg.installing; | |
22 return new Promise(function(resolve) { | |
23 worker.addEventListener('statechange', function() { | |
24 if (worker.state == 'activated') | |
25 resolve(); | |
26 }); | |
27 }); | |
28 }); | |
29 | |
30 function send_result(message_id, result) { | |
31 window.parent.postMessage( | |
32 {id: message_id, result: result}, | |
33 host_info['HTTP_ORIGIN']); | |
34 } | |
35 | |
36 function get_intercepted_urls(worker) { | |
37 return new Promise(function(resolve) { | |
38 var channel = new MessageChannel(); | |
39 channel.port1.onmessage = function(msg) { resolve(msg.data.urls); }; | |
40 worker.postMessage({port: channel.port2}, [channel.port2]); | |
41 }); | |
42 } | |
43 | |
44 window.addEventListener('message', on_message, false); | |
45 | |
46 function on_message(e) { | |
47 if (e.origin != host_info['HTTP_ORIGIN']) { | |
48 console.error('invalid origin: ' + e.origin); | |
49 return; | |
50 } | |
51 if (e.data.message == 'wait_for_worker') { | |
52 wait_for_worker_promise.then(function() { send_result(e.data.id, 'ok'); }); | |
53 } else if (e.data.message == 'get_intercepted_urls') { | |
54 get_intercepted_urls(worker) | |
55 .then(function(urls) { | |
56 send_result(e.data.id, urls); | |
57 }); | |
58 } else if (e.data.message == 'unregister') { | |
59 registration.unregister() | |
60 .then(function() { | |
61 send_result(e.data.id, 'ok'); | |
62 }); | |
63 } | |
64 } | |
65 | |
66 </script> | |
OLD | NEW |