-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
36 lines (31 loc) · 934 Bytes
/
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
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
title: chrome.i18n.getMessage("menu_title"),
id: "closeOtherTabs",
contexts: ["all"],
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "closeOtherTabs") {
const windowId = tab.windowId;
closeTabs(windowId);
// close other windows
chrome.windows.getAll({}, function (windows) {
const targets = windows.filter((w) => w.id !== windowId);
for (const t of targets) {
chrome.windows.remove(t.id);
}
});
}
});
chrome.action.onClicked.addListener(function (tab) {
const windowId = tab.windowId;
closeTabs(windowId);
});
// close same window inactive tabs
function closeTabs(windowId) {
chrome.tabs.query({ active: false, windowId: windowId }, function (tabs) {
const tabIds = tabs.map((tab) => tab.id);
chrome.tabs.remove(tabIds);
});
}