-
Notifications
You must be signed in to change notification settings - Fork 7
/
background.js
56 lines (50 loc) · 1.55 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
function onCreated(n) {
if (browser.runtime.lastError) {
console.log(`Error: ${browser.runtime.lastError}`);
} else {
console.log("Item created successfully");
}
}
const menuItemParams = {
id: "close_left",
title: browser.i18n.getMessage("contextItemTitle"),
contexts: ["tab"]
};
browser.contextMenus.create(menuItemParams, onCreated);
function closeTabs(sender, tabs) {
for (var tab of tabs) {
if (tab.id == sender.id) {
break;
}
if (!tab.pinned) {
browser.tabs.remove(tab.id);
}
}
}
browser.contextMenus.onClicked.addListener(function(info, sender) {
var querying = browser.tabs.query({currentWindow: true});
querying.then(closeTabs.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("[email protected]", {
type: "fake-contextMenu-create",
params: menuItemParams
}).then(onCreated, onCreated);
}
browser.runtime.onMessageExternal.addListener((message, sender) => {
switch (sender.id) {
case "[email protected]":
switch (message.type) {
case "ready":
registerToTST();
return;
case "fake-contextMenu-click":
browser.tabs.query({currentWindow: true})
.then(tabs => closeTabs(message.tab, tabs));
return;
}
}
});
registerToTST();