forked from ywzhaiqi/userChromeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openLinkTabThirdPart.uc.js
92 lines (74 loc) · 2.79 KB
/
openLinkTabThirdPart.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
// ==UserScript==
// @name openLinkTabThirdPart.uc.js
// @description 点击链接如果是第三方域名则在新标签页打开,否则当前标签页。有黑名单
// @author ywzhaiqi
// @namespace [email protected]
// @include main
// @charset UTF-8
// ==/UserScript==
/**
* 例: www.163.com,域名是 163.com 在当前标签页打开,其它域名新标签页打开
*/
(function() {
// 排除列表
var EXCLUDE = [
"http://booklink.me/*",
"http://www.baidu.com/*",
];
let { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
if (!window.Services) Cu.import("resource://gre/modules/Services.jsm");
var EXCLUDE_REGEXP = new RegExp(EXCLUDE.map(wildcardToRegExpStr).join("|"));
gBrowser.mPanelContainer.addEventListener('click', function(e){
if (e.button == 0 && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) {
if(EXCLUDE_REGEXP.test(gBrowser.currentURI.href)){
return;
}
var link = findLink(e.target);
if(!link) return;
var href = link.href;
if (href && href.match(/^(https?|ftp):\/\//)) {
e.preventDefault();
e.stopPropagation();
// if(link.host == gBrowser.currentURI.host){
if(isThirdPart(link, gBrowser.currentURI)){
gBrowser.loadOneTab(href, {
referrerURI: document.documentURIObject,
charset: target.charset,
postData: null,
inBackground: false
});
}else{
gBrowser.loadURI(href);
}
}
}
}, false);
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 isThirdPart(aLink, bLink){
try{
var aTLD = Services.eTLD.getBaseDomainFromHost(aLink.host);
var bTLD = Services.eTLD.getBaseDomainFromHost(bLink.host);
return aTLD != bTLD;
}catch(e){
return aLink.host != bLink.host;
}
}
function wildcardToRegExpStr(urlstr) {
if (urlstr.source) return urlstr.source;
let reg = urlstr.replace(/[()\[\]{}|+.,^$?\\]/g, "\\$&").replace(/\*+/g, function(str){
return str === "*" ? ".*" : "[^/]*";
});
return "^" + reg + "$";
}
})();