-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtabPlus.uc.js
225 lines (219 loc) · 7.98 KB
/
tabPlus.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
// ==UserScript==
// @name TabPlus.uc.js
// @description 自用整合版标签增强
// @updateURL https://raw.githubusercontent.com/xinggsf/uc/master/tabPlus.uc.js
// @namespace [email protected]
// @include chrome://browser/content/browser.xul
// @include chrome://browser/content/bookmarks/bookmarksPanel.xul
// @include chrome://browser/content/history/history-panel.xul
// @include chrome://browser/content/places/places.xul
// @startup tabPlusManager.startup();
// @shutdown tabPlusManager.shutdown();
// @version 2017.5.27
// @Note xinggsf 2017.5.20 修正新建标签按钮右键新开网址后弹出菜单,修正“关闭当前标签页回到左边标签”失效
// @Note xinggsf 2016.5.15 用API判断是否为FX可用地址
// @Note xinggsf 2015.12.18 增加右击新建按钮新开剪贴板中的网址
// @Note xinggsf 2015.1.28 整合,并去掉经常产生BUG的地址栏输入新开功能
// @Note 2014.09.18 最后一次修正整合 by defpt
// ==/UserScript==
(function () {
Cu.import('resource://gre/modules/Services.jsm');
/* 下列代码firefox 47+失效,注释之
// 新标签打开:书签、历史、搜索栏
let s = openLinkIn.toString();
s = s.replace('w.gBrowser.selectedTab.pinned', '(!w.isTabEmpty(w.gBrowser.selectedTab) || $&)')
.replace(/&&\s+w\.gBrowser\.currentURI\.host != uriObj\.host/, '')
.replace(/loadInBackground = false;/g, 'loadInBackground = aInBackground;');
try {
(new Function('openLinkIn = ' + s)());
} catch (e) {}
//地址栏新标签打开
try {
location=="chrome://browser/content/browser.xul" &&
eval("gURLBar.handleCommand="+gURLBar.handleCommand.toString().replace(/^\s*(load.+);/gm,
"if(/^javascript:/.test(url)||isTabEmpty(gBrowser.selectedTab)){loadCurrent();}else{this.handleRevert();gBrowser.loadOneTab(url, {postData: postData, inBackground: false, allowThirdPartyFixup: true});}"));
}catch(e){}
//自动关闭下载产生的空白标签
eval("gBrowser.mTabProgressListener = " + gBrowser.mTabProgressListener.toString().replace(/(?=var location)/, '\
if (aWebProgress.DOMWindow.document.documentURI == "about:blank"\
&& aRequest.QueryInterface(nsIChannel).URI.spec != "about:blank") {\
aWebProgress.DOMWindow.setTimeout(function() {\
!aWebProgress.isLoadingDocument && aWebProgress.DOMWindow.close();\
}, 100);\
}\
'));
// 关闭当前标签页回到左边标签,firefox 47+失效,改用tabclose事件
try {
eval("gBrowser._blurTab = " + gBrowser._blurTab.toString()
.replace('this.selectedTab = tab;',
"this.selectedTab = aTab.previousSibling || tab;"));
} catch (e) {}
*/
//中键点击bookmark菜单不关闭
try {
eval('BookmarksEventHandler.onClick =' + BookmarksEventHandler.onClick
.toString().replace('node.hidePopup()', ''));
eval('checkForMiddleClick =' + checkForMiddleClick.toString()
.replace('closeMenus(event.target);', ''));
} catch (e) {}
if (location.href === "chrome://browser/content/browser.xul") {
//当地址栏失去焦点后恢复原来的地址
gURLBar.addEventListener("blur", function () {
this.handleRevert();
}, !1);
//中键点击地址栏自动复制网址
gURLBar.addEventListener("click", function (e) {
if (e.button === 1) goDoCommand('cmd_copy');
}, !1);
//当搜索栏失去焦点后清空
if (BrowserSearch.searchBar && !document.getElementById('omnibar-defaultEngine')) {
BrowserSearch.searchBar.addEventListener("blur", function () {
this.value = "";
}, !1);
}
}
class TabPlus{
constructor(wnd) {
this.window = wnd;
this.tab = null;
const c = wnd.gBrowser.tabContainer;
c.addEventListener('mouseover', this, !1);
c.addEventListener('mouseout', this, !1);
c.addEventListener('click', this, !1);
c.addEventListener('TabClose', this, !1);
c.addEventListener('contextmenu', this, !1);
const newTabBtn = this.window.document.getAnonymousElementByAttribute(c, "class", "tabs-newtab-button");
newTabBtn.setAttribute("tooltiptext","左键:新建标签页\n中键:恢复刚关闭的标签\n右键:新开剪贴板中的网址");
newTabBtn.removeAttribute("onclick");
newTabBtn.addEventListener('click', this.handleNewTabButton.bind(this), !0);
}
destroy(){
const c = this.window.gBrowser.tabContainer;
c.removeEventListener('mouseover', this);
c.removeEventListener('mouseout', this);
c.removeEventListener('click', this);
c.removeEventListener('TabClose', this);
c.removeEventListener('contextmenu', this);
const newTabBtn = this.window.document.getAnonymousElementByAttribute(c, "class", "tabs-newtab-button");
newTabBtn.removeEventListener('click', this.handleNewTabButton.bind(this));
}
handleNewTabButton(ev) {
//if (ev.button === 0) this.window.BrowserOpenTab(ev);
if (ev.button === 1) {
this.window.undoCloseTab();
}
//右键点击新建按钮打开剪贴板中的网址
else if (ev.button === 2) {
/*
openNewTabWith('about:blank');
gURLBar.select();
goDoCommand('cmd_paste');
gBrowser.loadOneTab(url, {inBackground:false});
*/
const reg = /^([\w\-]+\.)+[a-z]{2,3}(:\d+)?(\/\S*)?$/i;
let url = readFromClipboard();
if (reg.test(url))
url = 'http://' + url;
try {
switchToTabHavingURI(url, true);
} catch (ex) {
BrowserSearch.loadSearchFromContext(url);
}
}
}
handleEvent(ev) {
const t = ev.target;
switch (ev.type) {
case "mouseover":
if (this.tab !== t && t.tagName === 'tab') {
this.tab = t;
this.tid = setTimeout(() => {
if (this.tab) {
this.window.gBrowser.selectedTab = this.tab;
this.tab = null;
}
}, 300);//delay 300ms
}
break;
case "mouseout":
this.tab = null;
if (this.tid) {
clearTimeout(this.tid);
this.tid = null;
}
break;
case "TabClose"://关闭当前标签页回到左边标签
if (this.window.gBrowser.selectedTab === t) {
this.window.gBrowser.tabContainer.selectedIndex--;
ev.preventDefault();
ev.stopPropagation();
}
break;
case "click":
if (ev.button === 1) {
console.log(t);
if (t.matches("tab")) {//复制tab的网址
let url = t.linkedBrowser.currentURI.spec;
//Services.clipboard.setData(url, ,);
Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper).copyString(url, !1);
ev.preventDefault();
ev.stopPropagation();
}
}
//右键关闭标签页,ctrl+右键打开菜单
else if (ev.button === 2 && t.tagName === "tab" && !ev.ctrlKey) {
ev.preventDefault();
this.window.gBrowser.removeTab(t);
ev.stopPropagation();
}
break;
case "contextmenu":
if ((!ev.ctrlKey && t.matches('tab')) || ev.originalTarget.matches('.tabs-newtab-button')) {
ev.preventDefault();
ev.stopPropagation();
}
break;
}
}
}
const _winList = new Map();
const WindowListener = {
onOpenWindow(xulWindow) {
const window = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
function onWindowLoad() {
window.removeEventListener('load', onWindowLoad);
if (window.document.documentElement.getAttribute('windowtype') == 'navigator:browser') {
_winList.set(window, new TabPlus(window));
}
}
window.addEventListener('load', onWindowLoad);
},
onCloseWindow(xulWindow) {
const window = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
if (_winList.has(window)) {
_winList.get(window).destroy();
_winList.delete(window);
}
},
onWindowTitleChange(xulWindow, newTitle) {}
};
window.tabPlusManager = {
startup() {
const windows = Services.wm.getEnumerator('navigator:browser');
while (windows.hasMoreElements()) {
const domwindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
domwindow && _winList.set(domwindow, new TabPlus(domwindow));
}
Services.wm.addListener(WindowListener);
},
shutdown() {
for (let obj of _winList) {
obj.destroy();
}
//_winList.clear();
_winList = null;
Services.wm.removeListener(WindowListener);
}
};
})();
tabPlusManager.startup();