-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
43 lines (38 loc) · 1.46 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
var tabIndex = {};
chrome.tabs.onCreated.addListener((newTab) => {
// Use -1 to represent the last position
chrome.tabs.move(newTab.id, { index: -1 });
});
// Store the position of the selected tab in the tab index.
// Use the active Chrome window's id as the index key property.
chrome.tabs.onActivated.addListener((activeInfo) => {
// Use chrome.tabs.get with a callback to handle potential errors.
chrome.tabs.get(activeInfo.tabId, (selectedTab) => {
// Check if the tab exists before accessing its properties.
if (chrome.runtime.lastError) {
console.error("Error getting tab:", chrome.runtime.lastError);
return;
}
if (selectedTab) {
tabIndex[activeInfo.windowId] = selectedTab.index;
} else {
// Handle cases where the tab might have been closed.
console.warn("Tab with ID", activeInfo.tabId, "not found.");
delete tabIndex[activeInfo.windowId];
}
});
});
chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
// If a window is closed, removeInfo.windowId will be available
if (removeInfo.windowId && tabIndex.hasOwnProperty(removeInfo.windowId)) {
//If the whole window is being closed, delete the entry
if (removeInfo.isWindowClosing) {
delete tabIndex[removeInfo.windowId];
}
}
});
chrome.windows.onRemoved.addListener((windowId) => {
if (tabIndex.hasOwnProperty(windowId)) {
delete tabIndex[windowId];
}
});