-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
66 lines (51 loc) · 1.72 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<html>
<head>
<link rel="manifest" href="./manifest.json">
</head>
<body>
<h1>Test gitbhub page, hoping it's https</h1>
<button onclick="sendMessageToSw()">Click me</button>
<br/>
<input id="txtInput" type="text"/>
<button onclick="add()">Add</button>
</body>
<script>
function add()
{
var input = document.getElementById("txtInput");
console.log(`will be adding input with text ${input.value}`);
}
if(self.IndexedDB){
console.log('IndexedDB is supported');
}
function sendMessageToSw()
{
console.log(`sending message with data...${new Date().toLocaleString()}`);
sw.postMessage(`Hi worker, here is data: ${new Date().toLocaleString()}`);
}
var sw;// = serviceWorkerRegistration.active;
async function registerSW() {
if ('serviceWorker' in navigator) {
try
{
console.log("registering service worker.")
sw = await navigator.serviceWorker.register('./sw.js').active;
console.log("registered service worker.")
navigator.serviceWorker.addEventListener('message', event => {
// event is a MessageEvent object
console.log(`The service worker sent me a message: ${event.data}`);
});
navigator.serviceWorker.ready.then( registration => {
sw = registration.active;
registration.active.postMessage("Hi service worker");
});
} catch (e) {
alert('ServiceWorker registration failed. Sorry about that.');
}
} else {
//document.querySelector('.alert').removeAttribute('hidden');
}
}
registerSW();
</script>
</html>