forked from yoko/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskip_redirector.user.js
135 lines (124 loc) · 3.54 KB
/
skip_redirector.user.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
// ==UserScript==
// @name Skip Redirector
// @namespace http://codefairy.org/ns/userscripts
// @include *
// @version 1.1.2
// @license MIT License
// @work Greasemonkey
// @work GreaseKit
// ==/UserScript==
new function() {
const API = 'http://wedata.net/databases/Redirector/items.json';
const EXPIRES = 7;
var greasemonkey = (typeof unsafeWindow != 'undefined');
var now = +new Date;
if (greasemonkey)
GM_registerMenuCommand('Skip Redirector Clear SITEINFO Cache', save);
var timer, complete = false;
var stash = load();
if (stash && stash.expires >= now)
handler(stash.data);
else {
if (greasemonkey)
GM_xmlhttpRequest({
method: 'GET',
url : API,
onload: function(r) {
clearTimeout(timer);
var data = JSON.parse(r.responseText);
save(data);
if (!complete) handler(data);
}
});
else if (document.body) {
window.userscript_skip_redirector_jsonp = function(data) {
clearTimeout(timer);
save(data);
if (!complete) handler(data);
};
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = API+'?callback=userscript_skip_redirector_jsonp';
s.charset = 'utf-8';
document.body.appendChild(s);
}
if (stash)
timer = setTimeout(function() {
complete = true;
handler(save(stash.data));
}, 1000 * 30);
}
function handler(data) {
var i = data.length;
while (i--) {
var item = data[i].data;
if (item.fixme) continue;
var url = item.url;
var link = item.link;
var replace_url = item.replace_url;
if (new RegExp(url).test(location.href)) {
if (replace_url) {
var reditrect_url = decodeURIComponent(location.href.replace(new RegExp(url), replace_url));
if (/^https?:\/\//.test(reditrect_url)) {
location.href = reditrect_url;
return;
}
}
else if (link) {
var a = $X(link)[0];
if (a) {
var e = document.createEvent('MouseEvent');
e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
return;
}
}
}
}
}
function load() {
var stash = greasemonkey ? GM_getValue('stash') : localStorage.userscript_skip_redirector_stash;
// compatibility
try {
return (stash && JSON.parse(stash));
}
catch (e) {
return undefined;
}
}
function save(data) {
var stash = data ?
JSON.stringify({
data : data,
expires: +new Date + 1000 * 60 * 60 * 24 * EXPIRES
}) :
'';
if (greasemonkey)
GM_setValue('stash', stash);
else {
if (stash) localStorage.userscript_skip_redirector_stash = stash;
else delete localStorage.userscript_skip_redirector_stash;
}
return data;
}
// http://gist.github.com/3242
function $X (exp, context) {
context || (context = document);
var expr = (context.ownerDocument || context).createExpression(exp, function (prefix) {
return document.createNSResolver(context.documentElement || context).lookupNamespaceURI(prefix) ||
context.namespaceURI || document.documentElement.namespaceURI || "";
});
var result = expr.evaluate(context, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.STRING_TYPE : return result.stringValue;
case XPathResult.NUMBER_TYPE : return result.numberValue;
case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
// not ensure the order.
var ret = [], i = null;
while (i = result.iterateNext()) ret.push(i);
return ret;
}
return null;
}
};