forked from ywzhaiqi/userChromeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenLinkinNewTabwithRightClick.uc.js
139 lines (118 loc) · 3.43 KB
/
OpenLinkinNewTabwithRightClick.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
// 修改自 openlinkinnewtabwithleftdoubleclick
(function() {
var gTimer = null;
function findLink(element) {
switch (element.tagName) {
case 'A': return element;
case 'B': case 'I': case 'SPAN': case 'SMALL':
case 'STRONG': case 'EM': case 'BIG': case 'SUB':
case 'SUP': case 'IMG': case 'S':
var parent = element.parentNode;
return parent && findLink(parent);
default:
return null;
}
}
function click(element, view) {
var e = document.createEvent('MouseEvents');
e.initMouseEvent('click', true, true, view, 0,
0, 0, 0, 0, false, false, false, false, 0, element);
return !element.dispatchEvent(e);
}
function openTab(href) {
if ('TreeStyleTabService' in window)
TreeStyleTabService.readyToOpenChildTab(gBrowser.selectedTab, false);
return gBrowser.addTab(href);
}
function findFrames(frame) {
var frames = frame.frames;
var fs = {};
for (var i = 0, len = frames.length; i < len; ++i) {
var f = frames[i];
fs[f.name] = f;
var children = findFrames(f);
for (k in children) {
var f = children[k];
fs[f.name] = f;
}
}
return fs;
}
function followLink(args) {
var link = args.link;
var newTab = args.newTab;
var window = args.window;
var activate = args.activate;
var href = link.href;
var target = link.target;
if (newTab) {
var tab = openTab(href);
if (activate) {
gBrowser.selectedTab = tab;
}
} else if (!target || target == '_self') {
window.location.href = href;
} else {
switch (target) {
case '_top':
window.top.location.href = href;
break;
case '_parent':
window.parent.location.href = href;
break;
case '_blank':
gBrowser.selectedTab = gBrowser.addTab(href);
break;
default:
var frames = findFrames(window.top);
var frame = frames[target];
if (frame) {
frame.location.href = href;
} else {
gBrowser.selectedTab = gBrowser.addTab(href);
}
}
}
}
gBrowser.mPanelContainer.addEventListener('click', function(e) {
if (e.button == 2 && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) {
var link = findLink(e.target);
if (link) {
var href = link.href;
if (href && href.match(/^(https?|ftp):\/\//)) {
e.preventDefault();
e.stopPropagation();
if (!gTimer) {
gTimer = setTimeout(function() {
try {
// if (click(link, e.view)) {
followLink({ link: link, window: e.view, newTab: true, activate: false });
// }
clearTimeout(gTimer);
} finally {
gTimer = null;
}
}, 200);
}
}
}
}
}, false);
// gBrowser.mPanelContainer.addEventListener('dblclick', function(e) {
// content.console.log(e.button)
// if (gTimer) {
// try {
// clearTimeout(gTimer);
// var link = findLink(e.target);
// if (link) {
// var href = link.href;
// if (href.match(/^(https?|ftp):\/\//) && click(link, e.view)) {
// followLink({ link: link, window: e.view, newTab: true, activate: false });
// }
// }
// } finally {
// gTimer = null;
// }
// }
// }, false);
})();