-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
55 lines (45 loc) · 1.57 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
// close-similar-tabs-copy (https://github.com/paulwarwicker/close-similar-tabs-copy)
// modified copy of https://github.com/webdkt/ff-close-similar-tabs
// excludes any removal of pinned tabs
var id="https://github.com/paulwarwicker/close-similar-tabs-copy";
browser.menus.create({
id: id,
title: "Close similar tabs",
contexts: ["tab"]
});
browser.menus.onClicked.addListener((info, tab) => {
console.log("Item %o clicked in tab %d (%s)", info.menuItemId, tab.id, tab.url);
if (info.menuItemId == id) {
var url = extractHostnameWithProtocol(tab.url);
console.log(url);
var querying = browser.tabs.query({ url: url + "/*", currentWindow: true });
querying.then(function (tabs) {
for (tab of tabs) {
if (!tab.active && !tab.pinned) {
browser.tabs.remove(tab.id);
console.log("Tab %d closed (%s)", tab.id, tab.url);
}
}
})
}
});
browser.menus.onShown.addListener(async function (info, tab) {
console.log("Item %o shown in tab %d (%s)", info.menuItemId, tab.id, tab.url);
var url = extractHostnameWithProtocol(tab.url);
console.log(url);
var querying = browser.tabs.query({ url: url + "/*", currentWindow: true });
querying.then(function (tabs) {
var count = tabs.length;
for (tab of tabs) {
if (tab.active || tab.pinned) {
count--;
}
}
browser.menus.update(id, { title: "Close similar tabs (" + count + ")" });
browser.menus.refresh();
})
})
function extractHostnameWithProtocol(url) {
var hostname = url.match(/(([a-z]*:\/\/)?[^\/]+)/i)[1];;
return hostname;
}