-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtabRemoveX.uc.js
194 lines (178 loc) · 6.03 KB
/
tabRemoveX.uc.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// ==UserScript==
// @name tabRemoveX.uc.js
// @namespace https://github.com/zephyrer
// @description Remove tabs with the same domain/host as current
// @include main
// @exclude about:*
// @author Zephyrer
// @compatibility 72
// @version 2020/05/22 21:51 changing locale detection logic to using navigator.language
// @version 2020/05/22 08:28 locale detection logic enhanced: support "intl.accept_languages" pref
// @version 2020/05/21 00:45 1.0
// @version 2020/05/20 19:28 initial
// ==/UserScript==
var tabRemoveX = {
get tabContext() {
return document.getElementById("tabContextMenu");
},
init: function(){
console.log("init");
this.tabContextMenu();
window.addEventListener('unload', this, false);
},
get locale() {
let locale = "";
/*
let prefs = ["general.useragent.locale",
"intl.locale.requested",
"intl.accept_languages",
"font.language.group"
];
let i = 0;
for (i=0; i<prefs.length; i++) {
locale = Services.prefs.getCharPref(prefs[i], "");
if (locale !== "") break;
}
if (/^chrome:\/\/.+\/locale\/.+\.properties/.test(locale))
locale = Services.prefs.getComplexValue(prefs[i], Components.interfaces.nsIPrefLocalizedString).data;
if (locale.includes(',')) {
locale = locale.substring(0, locale.indexOf(','));
}
*/
//Get the currently active Browser Window
let activeWindow = Services.wm.getMostRecentWindow("navigator:browser");
//Get the window.navigator from current window/tab
//activeWindow.document.defaultView is the <window> you want to use
let defaultViewNavigator = activeWindow.document.defaultView.navigator;
locale = defaultViewNavigator.language;
return locale;
},
uninit: function(){
window.removeEventListener('unload', this, false)
this.tabContext.removeEventListener('popupshowing', this, false);
},
handleEvent: function(event) {
switch(event.type) {
case "unload":
this.uninit(event);
break;
case "popupshowing":
this.popupshowing(event);
break;
}
},
tabContextMenu: function(){
//tab context menu
var tabContext = this.tabContext;
var menuitem = tabContext.appendChild(
document.createXULElement("menuitem"));
menuitem.id = "tabRemoveSameDomain";
menuitem.setAttribute("type", "command");
menuitem.setAttribute("accesskey", "D");
menuitem.setAttribute("oncommand","gBrowser.removeTabsSameDomainAs(TabContextMenu.contextTab);");
switch(this.locale) {
case "zh-CN":
menuitem.setAttribute("label", "\u5173\u95ED\u540C\u4E00\u57DF\u540D\u6807\u7B7E\u9875");//关闭同一域名标签页
break;
default:
menuitem.setAttribute("label", "Close all tabs w/ the same domain");
break;
}
menuitem = tabContext.appendChild(
document.createXULElement("menuitem"));
menuitem.id = "tabRemoveSameHost";
menuitem.setAttribute("type", "command");
menuitem.setAttribute("accesskey", "H");
menuitem.setAttribute("oncommand","gBrowser.removeTabsSameHostAs(TabContextMenu.contextTab);");
switch(this.locale) {
case "zh-CN":
menuitem.setAttribute("label", "\u5173\u95ED\u540C\u4E00\u4E3B\u673A\u540D\u6807\u7B7E\u9875");//关闭同一主机名标签页
break;
default:
menuitem.setAttribute("label", "Close all tabs w/ the same host");
break;
}
tabContext.addEventListener('popupshowing', this, false);
},
popupshowing: function(event) {
;
},
getHostName: function(url) {
let match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i);
if (match != null && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0) {
return match[2];
}
else {
return null;
}
},
getDomain: function(url) {
let hostName = this.getHostName(url);
let domain = hostName;
if (hostName != null) {
let parts = hostName.split('.').reverse();
if (parts != null && parts.length > 1) {
domain = parts[1] + '.' + parts[0];
if (/co|com|edu|gov|mil|net|org/i.test(parts[1]) && parts.length > 2) {
domain = parts[2] + '.' + domain;
}
}
}
return domain;
}
}
gBrowser.removeTabsSameDomainAs = function(aTab){
let domain = tabRemoveX.getDomain(aTab.linkedBrowser.currentURI.spec);
let tabsToRemove = [];
for (let tab of this.tabs) {
if (tab.pinned) continue;
if ("isLockTab" in gBrowser && this.isLockTab(tab)) continue;
if (tabRemoveX.getDomain(tab.linkedBrowser.currentURI.spec) == domain) {
tabsToRemove.push(tab);
}
}
if (
!this.warnAboutClosingTabs(
tabsToRemove.length,
this.closingTabsEnum.MULTI_SELECTED
)
) {
return;
}
this.removeTabs(tabsToRemove);
};
gBrowser.removeTabsSameHostAs = function(aTab){
let host = aTab.linkedBrowser.currentURI.host;
let tabsToRemove = [];
for (let tab of this.tabs) {
if (tab.pinned) continue;
if ("isLockTab" in gBrowser && this.isLockTab(tab)) continue;
if (tab.linkedBrowser.currentURI.host == host) {
tabsToRemove.push(tab);
}
}
if (
!this.warnAboutClosingTabs(
tabsToRemove.length,
this.closingTabsEnum.MULTI_SELECTED
)
) {
return;
}
this.removeTabs(tabsToRemove);
};
// We should only start the redirection if the browser window has finished
// starting up. Otherwise, we should wait until the startup is done.
if (gBrowserInit.delayedStartupFinished) {
tabRemoveX.init();
} else {
let delayedStartupFinished = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" &&
subject == window) {
Services.obs.removeObserver(delayedStartupFinished, topic);
tabRemoveX.init();
}
};
Services.obs.addObserver(delayedStartupFinished,
"browser-delayed-startup-finished");
}