forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.ts
65 lines (58 loc) · 1.86 KB
/
background.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
import 'webext-dynamic-content-scripts';
import cache from 'webext-storage-cache'; // Also needed to regularly clear the cache
import addDomainPermissionToggle from 'webext-domain-permission-toggle';
import optionsStorage from './options-storage';
import {getRghIssueUrl} from './helpers/rgh-issue-link';
import isDevelopmentVersion from './helpers/is-development-version';
// GHE support
addDomainPermissionToggle();
const messageHandlers = {
openUrls(urls: string[], {tab}: browser.runtime.MessageSender) {
for (const [i, url] of urls.entries()) {
void browser.tabs.create({
url,
index: tab!.index + i + 1,
active: false,
});
}
},
closeTab(_: any, {tab}: browser.runtime.MessageSender) {
void browser.tabs.remove(tab!.id!);
},
async fetch(url: string) {
const response = await fetch(url);
return response.text();
},
async fetchJSON(url: string) {
const response = await fetch(url);
return response.json();
},
openOptionsPage() {
void browser.runtime.openOptionsPage();
},
};
browser.runtime.onMessage.addListener((message: typeof messageHandlers, sender) => {
for (const id of Object.keys(message) as Array<keyof typeof messageHandlers>) {
if (id in messageHandlers) {
return messageHandlers[id](message[id], sender);
}
}
});
// Give the browserAction a reason to exist other than "Enable RGH on this domain"
browser.browserAction.onClicked.addListener(async () => {
const {actionUrl} = await optionsStorage.getAll();
void browser.tabs.create({
url: actionUrl || 'https://github.com',
});
});
browser.runtime.onInstalled.addListener(async ({reason}) => {
// Only notify on install
if (reason === 'install' && !isDevelopmentVersion()) {
await browser.tabs.create({
url: getRghIssueUrl(3543),
});
}
// Hope that the feature was fixed in this version
await cache.delete('hotfixes');
await cache.delete('style-hotfixes');
});