-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab-killer.js
executable file
·133 lines (117 loc) · 3.91 KB
/
tab-killer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var bg = {
contextMenuText: "Close tabs",
contexts : ["all"],
parentContextMenuId : undefined,
menus : [],
init: function() {
bg.parentContextMenuId = chrome.contextMenus.create({
"title": "Close tabs",
"contexts": bg.contexts
});
chrome.tabs.onActivated.addListener(
function(activeInfo) {
chrome.tabs.get(activeInfo.tabId, bg.resetSubMenu);
}
);
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
chrome.tabs.getCurrent(
function(currentTab) {
if (currentTab !== undefined && tabId === currentTab.id)
chrome.tabs.get(abId, bg.resetSubMenu);
}
);
}
);
},
resetSubMenu: function(tab) {
"use strict";
// See @urlParts example. First 4 items are protocol-specific.
let kUrlPartsDomain = 4;
function urlParts(url) {
// Split by '/' and '?'.
// > 'https://a.com/?/b'.split(/([\/\?])/g)
// [ 'https:', '/', '', '/', 'a.com', '/', '', '?', '', '/', 'b' ]
return url.split(/([\/\?])/g);
}
function getDomain(url) {
return urlParts(url)[kUrlPartsDomain];
};
var domain = "";
if (tab !== undefined) {
domain = getDomain(tab.url);
}
// reset old menus
for (var i = 0; i < bg.menus.length; i++) {
chrome.contextMenus.remove(bg.menus[i]);
}
bg.menus = [];
// Filter in reverse order so the last tab (in chrome order) remains open.
function filterTabs(closeIf) {
chrome.tabs.getAllInWindow(null, function(tabs) {
let toRemove = [];
for (var i = tabs.length - 1; i != 0; i--) {
if (closeIf(tabs, i)) {
toRemove.push(tabs[i].id);
}
}
chrome.tabs.remove(toRemove, null);
});
};
function addMenu(title, closeIf) {
bg.menus.push(chrome.contextMenus.create({
"title": title,
"contexts": bg.contexts,
"parentId": bg.parentContextMenuId,
"onclick": function(info, curTab) {
filterTabs(function(tabs, i) { return closeIf(tabs, i, curTab); });
},
}));
}
addMenu("Duplicate tabs (same URL)",
function(tabs, i, curTab) {
for (var j = i + 1; j < tabs.length; j++) {
if (tabs[i].url === tabs[j].url) {
return true;
}
}
return false;
}
);
addMenu("Left tabs",
function(tabs, i, curTab) { return tabs[i].index < curTab.index; });
addMenu("Right tabs",
function(tabs, i, curTab) { return tabs[i].index > curTab.index; });
addMenu("This tab",
function(tabs, i, curTab) { return tabs[i].index === curTab.index; });
addMenu("Other tabs",
function(tabs, i, curTab) { return tabs[i].index !== curTab.index; });
addMenu("From domain: " + domain,
function(tabs, i, curTab) { return getDomain(tabs[i].url) === getDomain(curTab.url); });
addMenu("Not from domain: " + domain,
function(tabs, i, curTab) { return getDomain(tabs[i].url) !== getDomain(curTab.url); });
addMenu("This URL",
function(tabs, i, curTab) { return tabs[i].url === curTab.url; });
let parts = urlParts(tab.url);
for (i = kUrlPartsDomain; i < parts.length - 1; i++) {
// NOTE: iterate to len-1 to avoid creating an entry for the full URL
// (that's already covered by "This URL" above).
if (parts[i] === '' || parts[i] === '/' || parts[i] === '?') {
// Avoid creating enries that don't differ or differ by a '/' or '?'
continue;
}
function subUrl(tab, lastPart) {
return urlParts(tab.url).slice(kUrlPartsDomain, lastPart + 1).join('');
}
let index = i;
addMenu(
"URL path: " + subUrl(tab, index),
function(tabs, i, curTab) {
return (getDomain(tabs[i].url) === getDomain(tab.url) &&
subUrl(tabs[i], index) === subUrl(tab, index));
}
);
}
}
};
bg.init();