OLD | NEW |
| 1 importScripts('/resources/testharness-helpers.js'); |
| 2 |
1 // Allows a document to exercise the Notifications API within a service worker b
y sending commands. | 3 // Allows a document to exercise the Notifications API within a service worker b
y sending commands. |
2 var messagePort = null; | 4 var messagePort = null; |
3 | 5 |
4 addEventListener('message', function(workerEvent) { | 6 addEventListener('message', function(workerEvent) { |
5 messagePort = workerEvent.data; | 7 messagePort = workerEvent.data; |
6 | 8 |
7 // Listen to incoming commands on the message port. | 9 // Listen to incoming commands on the message port. |
8 messagePort.onmessage = function(event) { | 10 messagePort.onmessage = function(event) { |
9 if (typeof event.data != 'object' || !event.data.command) | 11 if (typeof event.data != 'object' || !event.data.command) |
10 return; | 12 return; |
(...skipping 20 matching lines...) Expand all Loading... |
31 break; | 33 break; |
32 } | 34 } |
33 }; | 35 }; |
34 | 36 |
35 // Notify the controller that the worker is now available. | 37 // Notify the controller that the worker is now available. |
36 messagePort.postMessage('ready'); | 38 messagePort.postMessage('ready'); |
37 }); | 39 }); |
38 | 40 |
39 addEventListener('notificationclick', function(event) { | 41 addEventListener('notificationclick', function(event) { |
40 // Copies the serializable attributes of the Notification instance on |event
|. | 42 // Copies the serializable attributes of the Notification instance on |event
|. |
41 var notificationCopy = JSON.parse(JSON.stringify(event.notification)); | 43 var notificationCopy = JSON.parse(stringifyDOMObject(event.notification)); |
42 | 44 |
43 // Notifications containing "ACTION:CLOSE" in their message will be closed | 45 // Notifications containing "ACTION:CLOSE" in their message will be closed |
44 // immediately by the Service Worker. | 46 // immediately by the Service Worker. |
45 if (event.notification.body.indexOf('ACTION:CLOSE') != -1) | 47 if (event.notification.body.indexOf('ACTION:CLOSE') != -1) |
46 event.notification.close(); | 48 event.notification.close(); |
47 | 49 |
48 // Notifications containing "ACTION:OPENWINDOW" in their message will attemp
t | 50 // Notifications containing "ACTION:OPENWINDOW" in their message will attemp
t |
49 // to open a new window for an example URL. | 51 // to open a new window for an example URL. |
50 if (event.notification.body.indexOf('ACTION:OPENWINDOW') != -1) | 52 if (event.notification.body.indexOf('ACTION:OPENWINDOW') != -1) |
51 event.waitUntil(clients.openWindow('https://example.com/')); | 53 event.waitUntil(clients.openWindow('https://example.com/')); |
52 | 54 |
53 messagePort.postMessage({ command: 'click', | 55 messagePort.postMessage({ command: 'click', |
54 notification: notificationCopy }); | 56 notification: notificationCopy }); |
55 }); | 57 }); |
OLD | NEW |