|
1 | 1 | <title>Proxy Sandbox Controller</title> |
2 | 2 | <script> |
3 | | - function notifyReady(controller) { |
4 | | - parent.postMessage({ $sandboxsw$type: "ready" }, "*"); |
5 | | - } |
6 | | - |
7 | | - function checkAndNotifyIfReady(registration) { |
8 | | - const controller = navigator.serviceWorker.controller; |
9 | | - if (!controller) return; |
10 | | - |
11 | | - if (controller.state === "activated") { |
12 | | - notifyReady(controller); |
13 | | - } else { |
14 | | - controller.addEventListener("statechange", function onStateChange() { |
15 | | - if (controller.state === "activated") { |
16 | | - console.log("state activated"); |
17 | | - controller.removeEventListener("statechange", onStateChange); |
18 | | - notifyReady(controller); |
19 | | - } |
20 | | - }); |
21 | | - } |
22 | | - } |
23 | | - |
24 | 3 | navigator.serviceWorker |
25 | 4 | .register("/sw.js", { |
26 | 5 | scope: "./", |
27 | 6 | }) |
28 | | - .then((registration) => { |
29 | | - if (navigator.serviceWorker.controller) { |
30 | | - // there's probably already a service worker |
31 | | - checkAndNotifyIfReady(registration); |
32 | | - } else { |
33 | | - // there's no service worker, wait until we're controlled |
34 | | - navigator.serviceWorker.addEventListener("controllerchange", () => { |
35 | | - checkAndNotifyIfReady(registration); |
36 | | - }); |
37 | | - } |
38 | | - }) |
39 | 7 | .catch((error) => { |
40 | 8 | console.error("Service Worker registration failed:", error); |
41 | 9 | window.parent.postMessage( |
|
54 | 22 | // forward messageport |
55 | 23 | navigator.serviceWorker.controller.postMessage(event.data, event.ports); |
56 | 24 | }); |
| 25 | + navigator.serviceWorker.addEventListener("message", (event) => { |
| 26 | + if (event.data === "ready") { |
| 27 | + window.parent.postMessage({ $sandboxsw$type: "ready" }, "*"); |
| 28 | + } |
| 29 | + }); |
57 | 30 | </script> |
0 commit comments