-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathflashBlocker.js
More file actions
83 lines (75 loc) · 3.18 KB
/
flashBlocker.js
File metadata and controls
83 lines (75 loc) · 3.18 KB
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
/*
* 用于屏蔽页面上的所有flash
*/
var flashText = '<div style="text-shadow:0 0 2px #eee;letter-spacing:-1px;background:#eee;font-weight:bold;padding:0;font-family:arial,sans-serif;font-size:30px;color:#ccc;width:152px;height:52px;border:4px solid #ccc;border-radius:12px;position:absolute;top:50%;left:50%;margin:-30px 0 0 -80px;text-align:center;line-height:52px;">Flash</div>';
var count = 0;
var flashBlocks = {};
//点击时间触发
var click2ShowFlash = function(e){
var index = this.getAttribute('data-flash-index');
var flash = flashBlocks[index];
flash.setAttribute('data-flash-show','isshow');
this.parentNode.insertBefore(flash, this);
this.parentNode.removeChild(this);
this.removeEventListener('click', click2ShowFlash, false);
};
var createAPlaceHolder = function(flash, width, height){
var index = count++;
var style = document.defaultView.getComputedStyle(flash, null);
var positionType = style.position;
positionType = positionType === 'static' ? 'relative' : positionType;
var margin = style['margin'];
var display = style['display'] == 'inline' ? 'inline-block' : style['display'];
var style = [
'',
'width:' + width +'px',
'height:' + height +'px',
'position:' + positionType,
'margin:' + margin,
'display:' + display,
'margin:0',
'padding:0',
'border:0',
'border-radius:1px',
'cursor:pointer',
'background:-webkit-linear-gradient(top, rgba(240,240,240,1)0%,rgba(220,220,220,1)100%)',
''
]
flashBlocks[index] = flash;
var placeHolder = document.createElement('div');
placeHolder.setAttribute('title', '点我还原Flash');
placeHolder.setAttribute('data-flash-index', '' + index);
flash.parentNode.insertBefore(placeHolder, flash);
flash.parentNode.removeChild(flash);
placeHolder.addEventListener('click', click2ShowFlash, false);
placeHolder.style.cssText += style.join(';');
placeHolder.innerHTML = flashText;
return placeHolder;
};
var parseFlash = function(target){
if(target instanceof HTMLObjectElement) {
if(target.innerHTML.trim() == '') return;
if(target.getAttribute("classid") && !/^java:/.test(target.getAttribute("classid"))) return;
} else if(!(target instanceof HTMLEmbedElement)) return;
var width = target.offsetWidth;
var height = target.offsetHeight;
if(width > 160 && height > 60){
createAPlaceHolder(target, width, height);
}
};
var handleBeforeLoadEvent = function(e){
var target = e.target
if(target.getAttribute('data-flash-show') == 'isshow') return;
parseFlash(target);
};
module.exports = function() {
var embeds = document.getElementsByTagName('embed');
var objects = document.getElementsByTagName('object');
for(var i=0,len=objects.length; i<len; i++) objects[i] && parseFlash(objects[i]);
for(var i=0,len=embeds.length; i<len; i++) embeds[i] && parseFlash(embeds[i]);
// see: http://www.bilibili.com/video/av135433/index_4.html
Array.prototype.slice.call(
document.querySelectorAll('iframe.player[src^="https://secure.bilibili.com"]')
).forEach(x => createAPlaceHolder(x, x.offsetWidth, x.offsetHeight));
}
// document.addEventListener("beforeload", handleBeforeLoadEvent, true);