-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
126 lines (97 loc) · 3.24 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
let removingTabInProgress = false, debug = false;
const wait = () =>
new Promise(resolve =>
setTimeout(() => resolve(true), 50)
);
async function getCurrentTab() {
let queryOptions = {active: true, currentWindow: true};
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
async function handleTabActivated(tabInfo) {
if (debug) {
console.log('Tab activated: ' + tabInfo.tabId);
}
while (removingTabInProgress) {
await wait();
}
let tabsHistory;
await chrome.storage.local.get('tabsHistory').then(result => tabsHistory = result.tabsHistory);
const foundIndex = tabsHistory.findIndex(historyTabInfo => historyTabInfo.tabId === tabInfo.tabId);
if (foundIndex !== -1) {
tabsHistory.splice(foundIndex, 1);
}
tabsHistory.unshift(tabInfo);
if (debug) {
console.log('handleTabActivated', tabsHistory);
}
await chrome.storage.local.set({tabsHistory});
}
chrome.runtime.onInstalled.addListener(async () => {
if (debug) {
console.log('Extension installed');
}
let tabsHistory = [];
await getCurrentTab().then(tabInfo => tabsHistory.push({tabId: tabInfo.id, windowId: tabInfo.windowId}));
if (debug) {
console.log('onInstalled', tabsHistory);
}
await chrome.storage.local.set({tabsHistory});
});
chrome.tabs.onActivated.addListener(async function (tabInfo) {
await handleTabActivated(tabInfo);
});
chrome.tabs.onRemoved.addListener(async function (tabId) {
if (debug) {
console.log('Tab removed: ' + tabId);
}
while (removingTabInProgress) {
await wait();
}
removingTabInProgress = true;
let tabsHistory;
await chrome.storage.local.get('tabsHistory').then(result => tabsHistory = result.tabsHistory);
tabsHistory = tabsHistory.filter(historyTabInfo => historyTabInfo.tabId !== tabId);
if (debug) {
console.log('tabs.onRemoved', tabsHistory);
}
await chrome.storage.local.set({tabsHistory});
removingTabInProgress = false;
});
chrome.commands.onCommand.addListener(async () => {
if (debug) {
console.log('Hotkey pressed');
}
let tabsHistory;
await chrome.storage.local.get('tabsHistory').then(result => tabsHistory = result.tabsHistory);
if (tabsHistory.length < 2) {
return;
}
const currentWindow = await chrome.windows.getCurrent();
if (tabsHistory[1].windowId !== currentWindow.id) {
if (debug) {
console.log('Switching window to ' + tabsHistory[1].windowId);
}
await chrome.windows.update(tabsHistory[1].windowId, {focused: true});
}
await chrome.tabs.update(tabsHistory[1].tabId, {highlighted: true, active: true});
});
chrome.windows.onFocusChanged.addListener(async function (windowId) {
if (windowId === -1) {
if (debug) {
console.log('Window is -1, ignoring');
}
return;
}
if (debug) {
console.log('Window focused: ' + windowId);
}
let tabInfo = await getCurrentTab();
if (!tabInfo) {
if (debug) {
console.log('No tab found');
}
return;
}
await handleTabActivated({tabId: tabInfo.id, windowId: tabInfo.windowId});
}, {windowTypes: ['normal']});