-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtabRX.uc.js
259 lines (236 loc) · 8.66 KB
/
tabRX.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// ==UserScript==
// @name tabRX.uc.js
// @namespace https://github.com/zephyrer
// @description Remove/Refresh tabs with the same domain/host as current
// @include main
// @exclude about:*
// @author Zephyrer
// @compatibility 72
// @version 2022/02/04 14:20 1.21 匿名命名空间隔离
// @version 2022/02/01 17:54 1.2
// @version 2020/06/07 16:54 1.0
// @version 2020/06/07 15:58 initial
// ==/UserScript==
(function(){
var tabRX = {
get tabContext() {
return document.getElementById("tabContextMenu");
},
init: function(){
console.log("init");
this.tabContextMenu();
window.addEventListener('unload', this, false);
},
get locale() {
let locale = "";
//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;
}
},
test: function(aTab){
let ps = Services.prompt;
window.focus();
//let domain = tabRX.getDomain(aTab.linkedBrowser.currentURI.spec);
//let msg = "The domain of current tab is " + domain + ".";
let msg = "Current language is " + this.locale;
ps.alert(null, "TabRX", msg);
},
tabContextMenu: function(){
//tab context menu
var tabContext = this.tabContext;
tabContext = tabContext.appendChild(
document.createXULElement("menu"));
tabContext.id = "context_tabRX";
tabContext.setAttribute("label", (this.locale == "zh-CN") ? "\u540C\u6E90\u6807\u7B7E\u9875" : "Tabs From Same Source");//同源标签页
tabContext.setAttribute("accesskey", "Y");
tabContext = tabContext.appendChild(
document.createXULElement("menupopup"));
tabContext.id = "tabRX";
var menuitem = tabContext.appendChild(
document.createXULElement("menuitem"));
/*
menuitem.id = "tabRX-test";
menuitem.setAttribute("type", "command");
menuitem.setAttribute("accesskey", "T");
menuitem.setAttribute("oncommand","tabRX.test(TabContextMenu.contextTab);");
menuitem.setAttribute("label", "Test");
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;
}
menuitem = tabContext.appendChild(
document.createXULElement("menuitem"));
menuitem.id = "tabRefreshSameDomain";
menuitem.setAttribute("type", "command");
menuitem.setAttribute("accesskey", "H");
menuitem.setAttribute("oncommand","gBrowser.refreshTabsSameDomainAs(TabContextMenu.contextTab);");
switch(this.locale) {
case "zh-CN":
menuitem.setAttribute("label", "\u5237\u65B0\u540C\u4E00\u57DF\u540D\u6807\u7B7E\u9875");//刷新同一域名标签页
break;
default:
menuitem.setAttribute("label", "Refresh all tabs w/ the same domain");
break;
}
menuitem = tabContext.appendChild(
document.createXULElement("menuitem"));
menuitem.id = "tabRefreshSameHost";
menuitem.setAttribute("type", "command");
menuitem.setAttribute("accesskey", "H");
menuitem.setAttribute("oncommand","gBrowser.refreshTabsSameHostAs(TabContextMenu.contextTab);");
switch(this.locale) {
case "zh-CN":
menuitem.setAttribute("label", "\u5237\u65B0\u540C\u4E00\u4E3B\u673A\u540D\u6807\u7B7E\u9875");//刷新同一主机名标签页
break;
default:
menuitem.setAttribute("label", "Refresh all tabs w/ the same host");
break;
}
this.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 = tabRX.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 (tabRX.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);
};
gBrowser.refreshTabsSameDomainAs = function(aTab){
let domain = tabRX.getDomain(aTab.linkedBrowser.currentURI.spec);
let tabsToRefresh = [];
for (let tab of this.tabs) {
if (tabRX.getDomain(tab.linkedBrowser.currentURI.spec) == domain) {
tabsToRefresh.push(tab);
}
}
this.reloadTabs(tabsToRefresh);
};
gBrowser.refreshTabsSameHostAs = function(aTab){
let host = aTab.linkedBrowser.currentURI.host;
let tabsToRefresh = [];
for (let tab of this.tabs) {
if (tab.linkedBrowser.currentURI.host == host) {
tabsToRefresh.push(tab);
}
}
this.reloadTabs(tabsToRefresh);
};
// 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) {
tabRX.init();
} else {
let delayedStartupFinished = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" &&
subject == window) {
Services.obs.removeObserver(delayedStartupFinished, topic);
tabRX.init();
}
};
Services.obs.addObserver(delayedStartupFinished,
"browser-delayed-startup-finished");
}
})();