@@ -37,26 +37,94 @@ self.addEventListener('push', (event) => {
3737
3838} ) ;
3939
40- // https://stackoverflow.com/a/63917373/1872200
41- // async function is a function that synchronously returns a promise
42- async function showNotificationAsync ( payload , options ) {
43- await self . registration . showNotification ( payload . title , options ) ;
44- }
45-
4640// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/notificationclick_event#examples
41+ // self.addEventListener('notificationclick', (event) => {
42+ // const { action, notification } = event;
43+ // notification.close();
44+
45+ // if (action === 'open-a-link') {
46+ // // https://developer.mozilla.org/en-US/docs/Web/API/Clients/openWindow#return_value
47+ // // https://web-push-book.gauntface.com/common-notification-patterns/
48+ // //clients.openWindow(notification.data.url);
49+ // event.waitUntil(openLink(notification.data));
50+ // }
51+ // });
52+
53+
4754self . addEventListener ( 'notificationclick' , ( event ) => {
4855 const { action, notification } = event ;
4956 notification . close ( ) ;
5057
51- if ( action === 'open-a-link' ) {
52- // https://developer.mozilla.org/en-US/docs/Web/API/Clients/openWindow#return_value
58+ const { url } = notification . data ;
59+ event . waitUntil ( sendMessage ( event , url ) ) ;
60+ } ) ;
61+
62+
63+ // https://developer.mozilla.org/en-US/docs/Web/API/Clients
64+ async function sendMessage ( event , content ) {
65+ // If we didn't find an existing chat window,
66+ // open a new one:
67+ // https://stackoverflow.com/questions/61862872/how-to-copy-web-notification-content-to-clipboard
68+ // https://stackoverflow.com/a/34250261/1872200
69+ //var client = await clients.openWindow('/');
70+ //client.focus();
71+
72+ // // Exit early if we don't have access to the client.
73+ // // Eg, if it's cross-origin.
74+ // if (!event.clientId) return;
75+
76+ // // Get the client.
77+ // const client = await clients.get(event.clientId);
78+ // // Exit early if we don't get the client.
79+ // // Eg, if it closed.
80+ // if (!client) return;
81+
82+
83+ const allClients = await clients . matchAll ( {
84+ includeUncontrolled : true ,
85+ type : 'window' ,
86+ } ) ;
87+
88+ let chatClient ;
89+
90+ // Let's see if we already have a chat window open:
91+ for ( const client of allClients ) {
92+ const url = new URL ( client . url ) ;
93+
5394 // https://web-push-book.gauntface.com/common-notification-patterns/
54- //clients.openWindow(notification.data.url);
55- event . waitUntil ( openLink ( notification . data ) ) ;
95+ if ( url . pathname === '/' ) {
96+ // Excellent, let's use it!
97+ await client . focus ( ) ;
98+ chatClient = client ;
99+ break ;
100+ }
56101 }
57- } ) ;
102+
103+ // If we didn't find an existing chat window,
104+ // open a new one:
105+ if ( ! chatClient ) {
106+ chatClient = await clients . openWindow ( '/' ) ;
107+ //await chatClient.focus();
108+ }
109+
110+
111+ // Send a message to the client.
112+ chatClient . postMessage ( {
113+ type : 'clipboard' ,
114+ content : content ,
115+ } ) ;
116+
117+ }
58118
59119async function openLink ( data ) {
60120 // https://developer.mozilla.org/en-US/docs/Web/API/Clients/openWindow#return_value
61121 await clients . openWindow ( data . url ) ;
62122}
123+
124+
125+ // https://stackoverflow.com/a/63917373/1872200
126+ // async function is a function that synchronously returns a promise
127+ async function showNotificationAsync ( payload , options ) {
128+ await self . registration . showNotification ( payload . title , options ) ;
129+ }
130+
0 commit comments