forked from mkopec/close-tabs-to-the-right
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbackground.js
More file actions
72 lines (65 loc) · 2.02 KB
/
background.js
File metadata and controls
72 lines (65 loc) · 2.02 KB
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
function onCreated(n) {
if (browser.runtime.lastError) {
console.log(`Error: ${browser.runtime.lastError}`);
} else {
console.log("Item created successfully");
}
}
const menuItemParams = {
id: "close_other_tabs",
title: browser.i18n.getMessage("contextItemTitleCloseOtherTabs"),
contexts: ["tab"]
};
browser.contextMenus.create(menuItemParams, onCreated);
function closeTabs(sender, tabs) {
var senderFound = false;
for (var tab of tabs) {
if (tab.id == sender.id) {
senderFound = true;
continue;
}
if (!tab.pinned && senderFound) {
browser.tabs.remove(tab.id);
}
}
}
function closeOtherTabs(sender, tabs) {
for (var tab of tabs) {
if (tab.id == sender.id) {
continue;
} else if (!tab.pinned) {
browser.tabs.remove(tab.id);
}
}
}
browser.contextMenus.onClicked.addListener(function(info, sender) {
var querying = browser.tabs.query({currentWindow: true});
if (info.menuItemId == "close_right") {
querying.then(closeTabs.bind(null, sender));
} else if (info.menuItemId == "close_other_tabs") {
querying.then(closeOtherTabs.bind(null, sender));
}
});
// supports Tree Style Tab's fake context menu
// https://addons.mozilla.org/firefox/addon/tree-style-tab/
function registerToTST() {
browser.runtime.sendMessage("treestyletab@piro.sakura.ne.jp", {
type: "fake-contextMenu-create",
params: menuItemParams
}).then(onCreated, onCreated);
}
browser.runtime.onMessageExternal.addListener((message, sender) => {
switch (sender.id) {
case "treestyletab@piro.sakura.ne.jp":
switch (message.type) {
case "ready":
registerToTST();
return;
case "fake-contextMenu-click":
browser.tabs.query({currentWindow: true})
.then(tabs => closeTabs(message.tab, tabs));
return;
}
}
});
registerToTST();