-
Notifications
You must be signed in to change notification settings - Fork 158
/
helper.js
64 lines (56 loc) · 1.87 KB
/
helper.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
scroll(0, 0);
var slowSrc = 'http://1.cuzillion.com/bin/resource.cgi?type=gif&sleep=5&n=1&'
var localSrc = 'imgs/%i.jpg?'
var mainTpl = [
'<div class="sep"></div>',
'<img width=200 height=200',
'data-src="%src"',
'src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== onload=lzld(this) onerror=lzld(this)%class />'
].join(' ');
var errorFlag = false;
var insertSlowImage = makeInsert(slowSrc);
var insertImage = makeInsert(localSrc);
var error = makeRunOnce(function error(msg) {
errorFlag = true;
document.getElementById('debug').innerHTML = msg || 'unknown';
document.getElementById('result').className = 'error';
});
var success = makeRunOnce(function success() {
if (errorFlag) return;
successFlag = true;
document.getElementById('debug').className = 'hide';
document.getElementById('result').className = 'success';
});
function makeInsert(src) {
var tpl = mainTpl.replace('%src', src + new Date().getTime());
return function insertImage(n, className, fn) {
var newtpl = tpl;
n = n || 1;
if (typeof className === 'function') fn = className;
if (className !== undefined) newtpl = newtpl.replace('%class', ' class="'+className+'"');
else newtpl = newtpl.replace('%class', '');
var content = document.getElementById('content');
var ret = '';
for (var i = 1; i <= n; i ++) {
ret += newtpl.replace('%i', i);
}
if (typeof fn === 'function') return fn(ret)
content.innerHTML = content.innerHTML + ret;
}
}
function insertSlowScript() {
document.write('<scr' + 'ipt src="http://1.cuzillion.com/bin/resource.cgi?type=js&sleep=2&jsdelay=1&n=1&t=1340197119"></scr' + 'ipt>');
}
function makeRunOnce(fn) {
var calls = 0;
return function() {
if (calls > 0) return;
calls++;
return fn.apply(this, arguments);
}
}
window.onerror = function() {
window.onload = function() {
error();
}
}