1// Copyright 2015 Peter Beverloo. All rights reserved. 2// Use of this source code is governed by the MIT license, a copy of which can 3// be found in the LICENSE file. 4 5// Pulled from: https://github.com/beverloo/peter.sh/blob/71f60130396051a62e7958a191ffb0fa0bf22137/tests/cases/notification-generator-sw.js 6 7// IMPORTANT: 8// When changing this file you have to stop and unregister the original ServiceWorker. 9 10function getWindowClient(desktopServiceId) 11{ 12 var tmp_clients = {}; 13 return new Promise( 14 function (resolve, reject) 15 { 16 clients.matchAll({ type: 'window' }).then( 17 function (windowClients) 18 { 19 // Loop through all the clients and request their IDs. 20 for (var client of windowClients) 21 { 22 var messageChannel = new MessageChannel(); 23 tmp_clients[client.id] = { 24 client: client, 25 channel: messageChannel, 26 }; 27 messageChannel.port1.onmessage = function (event) 28 { 29 var client = tmp_clients[event.data.windowId].client; 30 if (event.data.desktopServiceId == desktopServiceId) 31 { 32 //console.log('Service Worker: getWindowClient FOUND, ' + desktopServiceId); 33 resolve(client); 34 } 35 delete tmp_clients[event.data.windowId].channel; 36 }; 37 client.postMessage({ action: 'guid', windowId: client.id }, [messageChannel.port2]); 38 } 39 } 40 ); 41 } 42 ); 43} 44 45self.addEventListener('install', 46 function (event) 47 { 48 event.waitUntil(skipWaiting()); 49 } 50); 51 52self.addEventListener('activate', 53 function (event) 54 { 55 event.waitUntil(clients.claim()); 56 } 57); 58 59self.addEventListener('notificationclick', function (event) 60{ 61 var notification = event.notification; 62 63 //console.log('Service Worker: notificationclick, ' + event.action + ', ' + notification.data.desktopServiceId); 64 65 if (['first', 'second'].indexOf(event.action) != -1) 66 { 67 notification.close(); 68 } 69 70 // Send action back to window. 71 var promise = Promise.resolve(); 72 promise = 73 promise.then(function () { return getWindowClient(notification.data.desktopServiceId); }) 74 .then( 75 function (client) 76 { 77 if (event.action) 78 { 79 console.log('event received: ', event); 80 console.log('client: ', client); 81 //console.log('Service Worker: sending ' + event.action + ' to found window'); 82 83 client.postMessage({ 84 action: event.action, 85 notifyData: notification.data, 86 }); 87 } 88 89 // Focus the tab on click 90 if (notification.data.options.focus) 91 { 92 return client.focus(); 93 } 94 return true; 95 } 96 ); 97 event.waitUntil(promise); 98}); 99 100/* NOTE: 101** There appears to be a bug in Chrome where the notificationclose event is not raised when the user closes the dialog using the "x" in the upper right corner (in a certain circumstance). 102** After the service worker is loaded, if the first time the dialog is shown you close it with the "X", everything will be fine and you will recieve the notificationclose event when it is closed that way in the future 103** however, if the first time the dialog is shown you select one of the actions - then in the future, when the dialog is closed with the "X" no event will be received. 104** From logging, we determined the notificationclose event is not even called
104in this scenerio. The notificationclick event appears to always fire properly when an item is clicked. 105*/ 106self.addEventListener('notificationclose', function (event) 107{ 108 var notification = event.notification; 109 //console.log('Service Worker: noticiationclose, ' + notification.data.desktopServiceId); 110 111 // Send action back to window. 112 var promise = Promise.resolve(); 113 promise = 114 promise.then(function () { return getWindowClient(notification.data.desktopServiceId); }) 115 .then( 116 function (client) 117 { 118 //console.log('Service Worker: sending close to found window'); 119 120 client.postMessage({ 121 action: 'close', 122 notifyData: notification.data, 123 }); 124 125 return true; 126 } 127 ); 128 event.waitUntil(promise); 129});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.