This repository has been archived by the owner on Dec 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
serviceWorker.ts
95 lines (89 loc) · 3.38 KB
/
serviceWorker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { getCurrentTabId, isFirefox } from "./core/common/common";
import { JSONToFormData } from "./core/common/dom";
import { fetchSafe } from "./core/common/fetch";
import { alarmNotifications, notificationClicked, setInitialNotificationsEventId } from "./core/notifications";
import { migrateSettings } from "./core/settings";
const requiredOrigin = "https://www.shacknews.com/chatty*";
// use non-async response here due to: https://bugs.chromium.org/p/chromium/issues/detail?id=1185241
chrome.runtime.onMessage.addListener(
(request: OnMessageRequest, _: chrome.runtime.MessageSender, sendResponse: any) => {
try {
if (request.name === "scrollByKeyFix" && isFirefox()) {
// scroll-by-key fix for Chatty
getCurrentTabId().then((tabId) => {
chrome.scripting
.executeScript({
target: { tabId },
files: ["patches/nuScrollByKeyFix.js"],
})
.then((r) => {
sendResponse(r);
});
});
} else if (request.name === "corbFetch") {
const fetchArgs: FetchArgs = {
url: request.url as string,
fetchOpts: request.fetchOpts,
parseType: request.parseType,
};
fetchSafe(fetchArgs).then((response) => {
sendResponse(response);
});
} else if (request.name === "corbPost") {
const _fd = request.data ? JSONToFormData(request.data) : null;
const fetchArgs: FetchArgs = {
url: request.url as string,
fetchOpts: {
...request.fetchOpts,
method: request.fetchOpts?.method ? request.fetchOpts.method : "POST",
body: _fd,
},
parseType: request.parseType,
};
fetchSafe(fetchArgs).then((response) => {
sendResponse(response);
});
}
return true;
} catch (e) {
console.error(e);
}
}
);
// spin up the notification polling service
chrome.notifications.onClicked.addListener(notificationClicked);
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === "notification-alarm") {
await alarmNotifications();
}
});
(async () => {
// attempt to migrate legacy settings on startup
await migrateSettings();
await setInitialNotificationsEventId();
chrome.alarms.create("notification-alarm", {
delayInMinutes: 1,
periodInMinutes: 1,
});
})();
chrome.runtime.onInstalled.addListener((details) => {
// only show the Preferences->Permissions panel if we're in Firefox
if (isFirefox && details.reason === "install") {
console.log("serviceWorker caught an permissions.onInstalled message!");
chrome.runtime.openOptionsPage();
} else if (isFirefox && details.reason === "update") {
console.log("serviceWorker caught a permissions.onRemoved message!");
chrome.runtime.openOptionsPage();
}
});
chrome.permissions.onAdded.addListener(async (permissions) => {
const isGranted = permissions.origins.includes(requiredOrigin);
console.log("permission.onAdded:", isGranted);
chrome.runtime.sendMessage({ type: "permissions_granted", permissions: permissions });
});
chrome.permissions.onRemoved.addListener(async (permissions) => {
if (!isFirefox) return;
const isGranted = permissions.origins.includes(requiredOrigin);
console.log("permission.onRemoved:", isGranted);
chrome.runtime.sendMessage({ type: "permissions_removed", permissions: permissions });
});