PageSourceSearch

https://electriclettuce.com/firebase-messaging-sw.js

js electriclettuce.com collected 2026-10-05 20:43:37 UTC 2,483 bytes, 76 lines download raw bytes

1/* eslint-disable no-undef */
2/* eslint-disable no-restricted-globals */
3
4// Scripts for firebase and firebase messaging
5importScripts("https://www.gstatic.com/firebasejs/9.6.11/firebase-app-compat.js");
6importScripts("https://www.gstatic.com/firebasejs/9.6.11/firebase-messaging-compat.js");
7
8self.addEventListener("push", event => {
9  const eventData = event.data.json();
10  event.waitUntil(
11    self.registration.showNotification(eventData.notification.title, {
12      body: eventData.notification.body,
13      data: eventData,
14    })
15  );
16});
17
18self.addEventListener("notificationclick", event => {
19  console.debug("SW notification click event", event);
20
21  const {click_action, url} = event.notification?.data?.data || {};
22  let urlClick = click_action || url;
23
24  if (!urlClick) {
25    const urlParams = new URLSearchParams(location.search);
26    urlClick = Object.fromEntries(urlParams).defaultUrlClick;
27  }
28
29  if (urlClick) urlClick = urlClick.endsWith("/") ? urlClick : urlClick + "/";
30
31  event.waitUntil(clients.claim());
32  event.waitUntil(
33    clients.matchAll({includeUncontrolled: true, type: "window"}).then(windowClients => {
34      // Check for target url coming from back end
35      // Check if there is already a window/tab open with the target URL
36      for (var i = 0; i < windowClients.length; i++) {
37        const windowClient = windowClients[i];
38        // If so, just focus it.
39        if ("focus" in windowClient && !urlClick) {
40          return windowClient.focus();
41        } else if (windowClient.url === urlClick) {
42          if (!windowClient.focused) windowClient.focus();
43          return windowClient.navigate(windowClient.url);
44        }
45      }
46      // If not, then open the target URL in a new window/tab.
47      if (clients.openWindow) {
48        return clients.openWindow(urlClick);
49      }
50    })
51  );
52});
53
54self.addEventListener("fetch", () => {
55  const urlParams = new URLSearchParams(location.search);
56  const firebaseCongifJson = Object.fromEntries(urlParams).firebaseConfig;
57  self.firebaseConfig = JSON.parse(firebaseCongifJson);
58});
59
60self.addEventListener("activate", async () => {
61  const tabs = await self.clients.matchAll({type: "window"});
62  tabs.forEach(tab => {
63    tab.navigate(tab.url);
64  });
65});
66
67// "Default" Firebase configuration (prevents errors)
68const defaultConfig = {
69  apiKey: true,
70  projectId: true,
71  messagingSenderId: true,
72  appId: true,
73};
74
75firebase.initializeApp(self.firebaseConfig || defaultConfig);
76firebase.messaging();

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.