-
Notifications
You must be signed in to change notification settings - Fork 29
/
background.js
95 lines (84 loc) · 3.39 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
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
"use strict";
const onCreatedTab = (tab) => {
tabsInfo.setNewTab(tab.id);
if (tab.status === "complete" && !isBlankURL(tab.url)) {
options.autoCloseTab ? searchForDuplicateTabsToClose(tab, true) : refreshDuplicateTabsInfo(tab.windowId);
}
};
const onBeforeNavigate = async (details) => {
if (options.autoCloseTab && (details.frameId == 0) && (details.tabId !== -1) && !isBlankURL(details.url)) {
if (tabsInfo.isIgnoredTab(details.tabId)) return;
const tab = await getTab(details.tabId);
if (tab) {
tabsInfo.resetTab(tab.id);
searchForDuplicateTabsToClose(tab, true, details.url);
}
}
};
const onCompletedTab = async (details) => {
if ((details.frameId == 0) && (details.tabId !== -1)) {
if (tabsInfo.isIgnoredTab(details.tabId)) return;
const tab = await getTab(details.tabId);
if (tab) {
tabsInfo.updateTab(tab);
options.autoCloseTab ? searchForDuplicateTabsToClose(tab) : refreshDuplicateTabsInfo(tab.windowId);
}
}
};
const onUpdatedTab = (tabId, changeInfo, tab) => {
if (tabsInfo.isIgnoredTab(tabId)) return;
if (Object.prototype.hasOwnProperty.call(changeInfo, "status") && changeInfo.status === "complete") {
if (Object.prototype.hasOwnProperty.call(changeInfo, "url") && (changeInfo.url !== tab.url)) {
if (isBlankURL(tab.url) || !tab.favIconUrl || !tabsInfo.hasUrlChanged(tab)) return;
tabsInfo.updateTab(tab);
options.autoCloseTab ? searchForDuplicateTabsToClose(tab) : refreshDuplicateTabsInfo(tab.windowId);
}
else if (isChromeURL(tab.url)) {
tabsInfo.updateTab(tab);
options.autoCloseTab ? searchForDuplicateTabsToClose(tab) : refreshDuplicateTabsInfo(tab.windowId);
}
}
};
const onAttached = async (tabId) => {
const tab = await getTab(tabId);
if (tab) {
options.autoCloseTab ? searchForDuplicateTabsToClose(tab) : refreshDuplicateTabsInfo(tab.windowId);
}
};
const onRemovedTab = (removedTabId, removeInfo) => {
tabsInfo.removeTab(removedTabId);
if (removeInfo.isWindowClosing) {
if (options.searchInAllWindows && tabsInfo.hasDuplicateTabs(removeInfo.windowId)) refreshDuplicateTabsInfo();
tabsInfo.clearDuplicateTabsInfo(removeInfo.windowId);
}
else if (tabsInfo.hasDuplicateTabs(removeInfo.windowId)) {
refreshDuplicateTabsInfo(removeInfo.windowId);
}
};
const onDetachedTab = (detachedTabId, detachInfo) => {
if (tabsInfo.hasDuplicateTabs(detachInfo.oldWindowId)) refreshDuplicateTabsInfo(detachInfo.oldWindowId);
};
const onActivatedTab = (activeInfo) => {
// for Chrome only
if (tabsInfo.isIgnoredTab(activeInfo.tabId)) return;
setBadge(activeInfo.windowId, activeInfo.tabId);
};
const onCommand = (command) => {
if (command == "close-duplicate-tabs") closeDuplicateTabs();
};
const start = async () => {
// eslint-disable-next-line no-unused-vars
await initializeOptions();
setBadgeIcon();
await refreshGlobalDuplicateTabsInfo();
chrome.tabs.onCreated.addListener(onCreatedTab);
chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigate);
chrome.tabs.onAttached.addListener(onAttached);
chrome.tabs.onDetached.addListener(onDetachedTab);
chrome.tabs.onUpdated.addListener(onUpdatedTab);
chrome.webNavigation.onCompleted.addListener(onCompletedTab);
chrome.tabs.onRemoved.addListener(onRemovedTab);
if (!environment.isFirefox) chrome.tabs.onActivated.addListener(onActivatedTab);
chrome.commands.onCommand.addListener(onCommand);
};
start();