mirror of
https://github.com/hanzoai/workbox.git
synced 2026-09-24 17:04:35 +00:00
28 lines
850 B
JavaScript
28 lines
850 B
JavaScript
/*
|
|
Copyright 2019 Google LLC
|
|
|
|
Use of this source code is governed by an MIT-style
|
|
license that can be found in the LICENSE file or at
|
|
https://opensource.org/licenses/MIT.
|
|
*/
|
|
|
|
addEventListener('install', () => skipWaiting());
|
|
|
|
addEventListener('message', async (event) => {
|
|
if (event.data.type === 'RESPOND_TO_MESSAGE') {
|
|
event.ports[0].postMessage('Reply from SW!');
|
|
} else if (event.data.type === 'POST_MESSAGE_BACK') {
|
|
const windows = await clients.matchAll({
|
|
type: 'window',
|
|
includeUncontrolled: true,
|
|
});
|
|
for (const win of windows) {
|
|
const channel = new MessageChannel();
|
|
win.postMessage('postMessage from SW!', [channel.port1]);
|
|
}
|
|
} else if (event.data.type === 'BROADCAST_BACK') {
|
|
const bc = new BroadcastChannel('workbox');
|
|
bc.postMessage('BroadcastChannel from SW!');
|
|
}
|
|
});
|