forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.ts
119 lines (102 loc) · 3.29 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import 'webext-dynamic-content-scripts';
import {globalCache} from 'webext-storage-cache'; // Also needed to regularly clear the cache
import {addOptionsContextMenu} from 'webext-tools';
import addPermissionToggle from 'webext-permission-toggle';
import webextAlert from 'webext-alert';
import {StorageItem} from 'webext-storage';
import {handleMessages} from 'webext-msg';
import optionsStorage, {hasToken} from './options-storage.js';
import isDevelopmentVersion from './helpers/is-development-version.js';
import {doesBrowserActionOpenOptions} from './helpers/feature-utils.js';
import {styleHotfixes} from './helpers/hotfix.js';
const {version} = chrome.runtime.getManifest();
const welcomeShown = new StorageItem('welcomed', {defaultValue: false});
// GHE support
addPermissionToggle();
// Firefox/Safari polyfill
addOptionsContextMenu();
handleMessages({
async openUrls(urls: string[], {tab}: chrome.runtime.MessageSender) {
for (const [index, url] of urls.entries()) {
void chrome.tabs.create({
url,
index: tab!.index + index + 1,
active: false,
});
}
},
async closeTab(_: any, {tab}: chrome.runtime.MessageSender) {
void chrome.tabs.remove(tab!.id!);
},
async fetchJSON(url: string) {
const response = await fetch(url);
return response.json();
},
async openOptionsPage() {
return chrome.runtime.openOptionsPage();
},
async getStyleHotfixes() {
return styleHotfixes.get(version);
},
});
chrome.action.onClicked.addListener(async tab => {
if (doesBrowserActionOpenOptions) {
void chrome.runtime.openOptionsPage();
return;
}
const {actionUrl} = await optionsStorage.getAll();
if (!actionUrl) {
// Default to options page if unset
void chrome.runtime.openOptionsPage();
return;
}
await chrome.tabs.create({
openerTabId: tab.id,
url: actionUrl,
});
});
async function showWelcomePage(): Promise<void> {
if (await welcomeShown.get()) {
return;
}
const [token, permissions] = await Promise.all([
hasToken(), // We can't handle an invalid token on a "Welcome" page, so just check whether the user has ever set one
chrome.permissions.contains({origins: ['https://github.com/*']}),
]);
try {
if (token && permissions) {
// Mark as welcomed
return;
}
const url = chrome.runtime.getURL('assets/welcome.html');
await chrome.tabs.create({url});
} finally {
// Make sure it's always set to true even in case of errors
await welcomeShown.set(true);
}
}
chrome.runtime.onInstalled.addListener(async () => {
if (isDevelopmentVersion()) {
await globalCache.clear();
}
if (await chrome.permissions.contains({origins: ['*://*/*']})) {
console.warn('Refined GitHub was granted access to all websites by the user and it’s now been removed. https://github.com/refined-github/refined-github/pull/7407');
await chrome.permissions.remove({
origins: [
'*://*/*',
],
});
}
// Call after the reset above just in case we nuked Safari's base permissions
await showWelcomePage();
});
chrome.permissions.onAdded.addListener(async permissions => {
if (permissions.origins?.includes('*://*/*')) {
await chrome.permissions.remove({
origins: [
'*://*/*',
],
});
await webextAlert('Refined GitHub is not meant to run on every website. If you’re looking to enable it on GitHub Enterprise, follow the instructions in the Options page.');
}
});