-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathindex.html
87 lines (74 loc) · 2.1 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>
<head>
<title>Background Sync Demonstration</title>
<meta name=viewport content="width=device-width, initial-scale=1">
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
Network status:
<div id="network" style="display:inline">
</div>
<p>
One-off background sync doesn't require permissions, but notifications do,
and that's how we're going to tell you it worked.
</p>
<button class="register">Register background sync</button>
<div class="log"></div>
<script>
var logEl = document.querySelector('.log');
function log(msg) {
var p = document.createElement('p');
p.textContent = msg;
logEl.appendChild(p);
console.log(msg);
}
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js', { scope: '/BackgroundSync/demo/' })
.catch(function(err) {
log('ServiceWorker failed to register. Are you visiting the HTTPS site?');
log(err.message);
});
}
function updateOnlineStatus(event) {
var online = navigator.onLine;
document.getElementById("network").innerHTML = online ? 'Online' : 'Offline';
}
window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);
updateOnlineStatus();
function displayErrorFromWorker(message) {
log("Error: " + message);
}
window.addEventListener('message', displayErrorFromWorker);
document.querySelector('.register').addEventListener('click', function(event) {
event.preventDefault();
new Promise(function(resolve, reject) {
Notification.requestPermission(function(result) {
if (result !== 'granted') return reject(Error("Denied notification permission"));
resolve();
})
}).then(function() {
return navigator.serviceWorker.ready;
}).then(function(reg) {
return reg.sync.register('syncTest');
}).then(function() {
log('Sync registered');
}).catch(function(err) {
log('It broke');
log(err.message);
});
});
</script>
<br><br>
<p>
<small>
To use this site, run Chrome version >= 49
</small>
</p>
</body>
</html>