forked from protamail/quickjs-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
49 lines (46 loc) · 1.99 KB
/
index.html
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
<html>
<head>
<title>qjs-http</title>
<script type="module" src="testHtm.js"></script>
<script type="module">
if ("serviceWorker" in navigator) {
window.addEventListener("load", async function () {
const script = "/sw.js";
const { serviceWorker } = navigator;
window.sw = serviceWorker;
console.log("register service worker", serviceWorker);
let result = await serviceWorker.register(script);
console.log("Service Worker Registered", serviceWorker);
const { controller } = serviceWorker;
console.log("Service Worker controller", controller);
window.swc = controller;
controller.addEventListener("message", (event) => {
console.debug("index.html MESSAGE:", event.data);
});
const messageChannel = new MessageChannel();
controller.postMessage(
{
type: "INIT_PORT"
},
[messageChannel.port2]
);
messageChannel.port1.onmessage = (event) => {
const { data } = event;
if (data.type == "LOG") console.log("WORKER", ...data.args);
else console.log("Message from serviceWorker:", event.data);
};
window.mc = messageChannel;
window.swp = messageChannel.port2;
controller.postMessage({
type: "REQUESTED"
});
let registration = await serviceWorker.ready;
console.log("Service Worker ready", registration);
});
}
</script>
</head>
<body>
<main id="main"></main>
</body>
</html>