|
13 | 13 | });
|
14 | 14 | }
|
15 | 15 |
|
16 |
| -var serverAddress; |
| 16 | +navigator.serviceWorker.ready |
| 17 | +.then(function(reg) { |
| 18 | + var channel = new MessageChannel(); |
| 19 | + channel.port1.onmessage = function(e) { |
| 20 | + window.document.title = e.data; |
| 21 | + } |
| 22 | + reg.active.postMessage('setup', [channel.port2]); |
17 | 23 |
|
18 |
| -function go() { |
19 |
| - navigator.serviceWorker.ready |
20 |
| - .then(function(reg) { |
21 |
| - var channel = new MessageChannel(); |
22 |
| - channel.port1.onmessage = function(e) { |
23 |
| - window.document.title = e.data; |
24 |
| - } |
25 |
| - reg.active.postMessage('setup', [channel.port2]); |
26 |
| - |
27 |
| - return reg.pushManager.getSubscription() |
28 |
| - .then(function(subscription) { |
29 |
| - if (!subscription) { |
30 |
| - return reg.pushManager.subscribe({ userVisibleOnly: true }) |
31 |
| - .then(function(subscription) { |
32 |
| - return subscription; |
33 |
| - }); |
34 |
| - } else { |
35 |
| - return subscription; |
36 |
| - } |
37 |
| - }); |
38 |
| - }) |
| 24 | + return reg.pushManager.getSubscription() |
39 | 25 | .then(function(subscription) {
|
40 |
| - var key = subscription.getKey ? subscription.getKey('p256dh') : ''; |
41 |
| - var auth = subscription.getKey ? subscription.getKey('auth') : ''; |
| 26 | + if (!subscription) { |
| 27 | + return reg.pushManager.subscribe({ userVisibleOnly: true }) |
| 28 | + .then(function(subscription) { |
| 29 | + return subscription; |
| 30 | + }); |
| 31 | + } else { |
| 32 | + return subscription; |
| 33 | + } |
| 34 | + }); |
| 35 | +}) |
| 36 | +.then(function(subscription) { |
| 37 | + var key = subscription.getKey ? subscription.getKey('p256dh') : ''; |
| 38 | + var auth = subscription.getKey ? subscription.getKey('auth') : ''; |
42 | 39 |
|
43 |
| - return fetch(serverAddress, { |
44 |
| - method: 'post', |
45 |
| - headers: { |
46 |
| - 'Content-type': 'application/json' |
47 |
| - }, |
48 |
| - body: JSON.stringify({ |
49 |
| - endpoint: subscription.endpoint, |
50 |
| - key: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : '', |
51 |
| - auth: auth ? btoa(String.fromCharCode.apply(null, new Uint8Array(auth))) : '', |
52 |
| - }), |
53 |
| - }); |
| 40 | + return fetch('/', { |
| 41 | + method: 'post', |
| 42 | + headers: { |
| 43 | + 'Content-type': 'application/json' |
| 44 | + }, |
| 45 | + body: JSON.stringify({ |
| 46 | + endpoint: subscription.endpoint, |
| 47 | + key: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : '', |
| 48 | + auth: auth ? btoa(String.fromCharCode.apply(null, new Uint8Array(auth))) : '', |
| 49 | + }), |
54 | 50 | });
|
55 |
| -} |
| 51 | +}); |
56 | 52 | </script>
|
57 | 53 | </body>
|
58 | 54 | </html>
|
0 commit comments