-
Notifications
You must be signed in to change notification settings - Fork 5
/
celery.js
97 lines (81 loc) · 2.45 KB
/
celery.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
(function celery(doc) {
// uglify 优化
var parentNode = 'parentNode';
var replaceChild = 'replaceChild';
var innerHTML = 'innerHTML';
var documentElement = 'documentElement';
var createElement = 'createElement';
var forEach = 'forEach';
var _script = 'script';
var events = 'contextmenu dragstart selectstart select copy beforecopy mouseup mousedown'.split(' ');
function noop() {}
function remove(element) {
element[parentNode].removeChild(element);
}
function query(selecter) {
return [].slice.call(doc.querySelectorAll(selecter));
}
function strip(element) {
events[forEach](function (event) {
// 移除 DOM 0 事件
element['on' + event] = null;
});
var done;
function deep() {
_setTimeout(function () {
if (!done) {
try {
var contentDocument = element.contentDocument;
if (contentDocument.body.childNodes.length) {
done = 1;
celery(contentDocument);
}
} catch (e) {
done = 0;
};
}
}, 100);
}
deep();
element.onload = deep;
}
// 清空所有定时器
for (var i = setTimeout(noop, 0); i >= 0; i--) {
clearTimeout(i);
};
for (i = setInterval(noop, 1e8); i >= 0; i--) {
clearInterval(i);
};
var _setTimeout = setTimeout;
// 覆盖掉延时方法
setTimeout = setInterval = noop;
// 通过替换 DOM 的方式移除节点上的监听事件
query(_script)[forEach](remove);
// frame 不能通过 innerHTML 完全重建,缓存起来
var frames = [];
query('iframe,frame')[forEach](function (frame) {
frames.push(frame);
frame[parentNode][replaceChild](doc[createElement](_script), frame);
})
var old = doc[documentElement][innerHTML];
doc.open();
doc.write('<!DOCTYPE html>');
doc.close();
doc[documentElement][innerHTML] = old;
// 替换回 frame
query(_script)[forEach](function (script) {
frames.length && script[parentNode][replaceChild](frames.shift(), script);
})
query('*')[forEach](strip);
strip(doc);
events[forEach](function (event) {
// 阻止事件传播
doc.addEventListener(event, function (e) {
e.stopPropagation();
}, true);
});
// 移除通过样式禁用选择
var style = doc[createElement]('style');
style[innerHTML] = '*{-webkit-user-select:text!important;-moz-user-select:text!important;user-select:text!important;}';
doc.body.appendChild(style);
}(document));