-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
58 lines (49 loc) · 2.47 KB
/
background.js
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
import unloader from './lib/unloader.js';
// On installed
chrome.runtime.onInstalled.addListener(async (details) => {
// traditional quick access (set status on extenxion install, update, reload...)
chrome.action.setPopup({ 'popup': (await chrome.storage.sync.get(['traditional-quick-access']))['traditional-quick-access'] ? 'popup.html' : '' });
switch (details.reason) {
case "install":
case "update": {
chrome.tabs.create({ 'url': 'https://sakkyoi.github.io/Tabs-Unloader/welcome.html' });
break;
}
}
});
// On startup
chrome.runtime.onStartup.addListener(async () => {
// traditional quick access (set status on startup)
chrome.action.setPopup({ 'popup': (await chrome.storage.sync.get(['traditional-quick-access']))['traditional-quick-access'] ? 'popup.html' : '' });
// Startup unload
if (!(await chrome.storage.sync.get(['startup-unload']))['startup-unload']) return;
const tabs = await chrome.tabs.query({ highlighted: false });
for (let tab of tabs) {
chrome.tabs.discard(tab.id);
}
});
// On storage changed
chrome.storage.onChanged.addListener(async () => {
// traditional quick access (set status on preference change)
chrome.action.setPopup({ 'popup': (await chrome.storage.sync.get(['traditional-quick-access']))['traditional-quick-access'] ? 'popup.html' : '' });
});
// On contextmenu clicked
chrome.contextMenus.onClicked.addListener(async (info, _) => {
unloader(info.menuItemId);
if (info.menuItemId === '_preference') chrome.tabs.create({ 'url': chrome.runtime.getURL('preference/preference.html') });
});
// on action clicked
chrome.action.onClicked.addListener(async () => {
// anti mistouch
if (!(await chrome.storage.sync.get(['anti-mistouch']))['anti-mistouch']) unloader('unloadSelected');
});
// Initialization Contextmenu
(async () => {
await chrome.contextMenus.removeAll(); // avoid error
const contextMenuItems = ['unloadSelected', 'unloadAllExceptSelected', 'unloadAll', 'unloadAllFromThisWindowExceptSelected', 'unloadAllFromThisWindow'];
for (let contextMenuItem of contextMenuItems) {
chrome.contextMenus.create({ 'id': contextMenuItem, 'title': chrome.i18n.getMessage(contextMenuItem), 'contexts': [ 'all' ] });
}
chrome.contextMenus.create({ 'id': '_', 'type': 'separator', 'contexts': [ 'all' ] });
chrome.contextMenus.create({ 'id': '_preference', 'title': chrome.i18n.getMessage('preference'), 'contexts': [ 'all' ] });
})()