OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <title>Push API Test</title> | 4 <title>Push API Test</title> |
5 <script> | 5 <script> |
6 var lastMessageFromServiceWorker = null; | |
7 | |
6 function sendResultToTest(result) { | 8 function sendResultToTest(result) { |
7 console.log(result); | 9 console.log('sendResultToTest: ' + result); |
8 if (window.domAutomationController) { | 10 if (window.domAutomationController) { |
9 domAutomationController.send('' + result); | 11 domAutomationController.send('' + result); |
10 } | 12 } |
11 } | 13 } |
12 | 14 |
13 function sendErrorToTest(error) { | 15 function sendErrorToTest(error) { |
14 sendResultToTest(error.name + ' - ' + error.message); | 16 sendResultToTest(error.name + ' - ' + error.message); |
15 } | 17 } |
16 | 18 |
17 function registerServiceWorker() { | 19 function registerServiceWorker() { |
18 navigator.serviceWorker.register('service_worker.js').then(function(swRegi stration) { | 20 navigator.serviceWorker.register('service_worker.js').then( |
19 console.log(swRegistration); | 21 function(swRegistration) { |
20 sendResultToTest('ok'); | 22 sendResultToTest('ok - service worker registered'); |
21 }, sendErrorToTest); | 23 }, sendErrorToTest); |
22 } | 24 } |
23 | 25 |
24 function registerPush(senderId) { | 26 function registerPush(senderId) { |
25 navigator.serviceWorker.ready.then(function() { | 27 navigator.serviceWorker.ready.then(function() { |
26 navigator.push.register(senderId).then(function(pushRegistration) { | 28 navigator.push.register(senderId).then(function(pushRegistration) { |
27 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + pushRegistrat ion.pushRegistrationId); | 29 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + |
30 pushRegistration.pushRegistrationId); | |
28 }, sendErrorToTest); | 31 }, sendErrorToTest); |
29 }, sendErrorToTest); | 32 }, sendErrorToTest); |
30 } | 33 } |
34 | |
35 function isControlled() { | |
36 if (navigator.serviceWorker.controller) { | |
37 sendResultToTest('true - is controlled'); | |
38 } else { | |
39 sendResultToTest('false - is not controlled'); | |
40 } | |
41 } | |
42 | |
43 function doPostMessage() { | |
44 navigator.serviceWorker.controller.postMessage('Message from page.'); | |
45 } | |
46 | |
47 addEventListener('message', function(event) { | |
48 var message = JSON.parse(event.data); | |
49 asyncData.setData(message.type, message.data); | |
50 }, false); | |
51 | |
52 // For getting and setting data that might not be available yet at the time | |
53 // it is requested by the test. | |
54 function AsyncData() { | |
55 this.received = {}; | |
56 this.requested = {}; | |
Peter Beverloo
2014/10/24 14:54:58
I'd like to get rid of the maps here, and change A
Michael van Ouwerkerk
2014/10/27 15:14:30
Done.
| |
57 } | |
58 | |
59 // Sends the data to the test if it is available. If not, marks it as | |
60 // requested. | |
61 AsyncData.prototype.getData = function(field) { | |
62 console.log('getData: ' + field); | |
63 if (this.received[field]) { | |
64 sendResultToTest(this.received[field]); | |
65 this.received[field] = null; | |
66 this.requested[field] = false; | |
67 } else { | |
68 this.requested[field] = true; | |
69 } | |
70 }; | |
71 | |
72 // Stores the data if it was not requested yet. If it was requested already, | |
73 // sends the data to the test. | |
74 AsyncData.prototype.setData = function(field, data) { | |
75 console.log('setData ' + field + ', ' + data); | |
76 if (this.requested[field]) { | |
77 sendResultToTest(data); | |
78 this.received[field] = null; | |
79 this.requested[field] = false; | |
80 } else { | |
81 this.received[field] = data; | |
82 } | |
83 } | |
84 | |
85 var asyncData = new AsyncData(); | |
31 </script> | 86 </script> |
32 </head> | 87 </head> |
33 <body>Push API Test</body> | 88 <body> |
89 <h1>Push API Test</h1> | |
90 <div> | |
91 <button onclick="registerServiceWorker();">registerServiceWorker()</button> | |
92 <button onclick="registerPush();">registerPush()</button> | |
93 <button onclick="isControlled();">isControlled()</button> | |
94 <button onclick="doPostMessage();">doPostMessage()</button> | |
95 <button onclick="getLastMessageFromServiceWorker();"> | |
96 getLastMessageFromServiceWorker()</button> | |
Peter Beverloo
2014/10/24 14:54:58
While I'm not necessarily against adding some manu
fgorski
2014/10/24 17:09:24
+1. However, I think if you document it well, they
Michael van Ouwerkerk
2014/10/27 15:14:30
Done.
Michael van Ouwerkerk
2014/10/27 15:14:30
Deleted.
| |
97 </div> | |
98 </body> | |
34 </html> | 99 </html> |
OLD | NEW |