-
Notifications
You must be signed in to change notification settings - Fork 2
/
flashblocknotifier.js
213 lines (190 loc) · 8.62 KB
/
flashblocknotifier.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* This is a wrapper for swfobject that detects FlashBlock in browser.
*
* Wrapper detects:
* - Chrome
* - FlashBlock (https://chrome.google.com/webstore/detail/cdngiadmnkhgemkimkhiilgffbjijcie)
* - FlashBlock (https://chrome.google.com/webstore/detail/gofhjkjmkpinhpoiabjplobcaignabnl)
* - FlashFree (https://chrome.google.com/webstore/detail/ebmieckllmmifjjbipnppinpiohpfahm)
* - Firefox Flashblock (https://addons.mozilla.org/ru/firefox/addon/flashblock/)
* - Opera >= 11.5 "Enable plugins on demand" setting
* - Safari ClickToFlash Extension (http://hoyois.github.com/safariextensions/clicktoplugin/)
* - Safari ClickToFlash Plugin (for Safari < 5.0.6) (http://rentzsch.github.com/clicktoflash/)
*
* Tested on:
* - Chrome 12
* - FlashBlock by Lex1 1.2.11.12
* - FlashBlock by josorek 0.9.31
* - FlashFree 1.1.3
* - Firefox 5.0.1 + Flashblock 1.5.15.1
* - Opera 11.5
* - Safari 5.1 + ClickToFlash (2.3.2)
* - Safari 5.1 + ClickToFlash (2.6)
*
* Also this wrapper can remove blocked swf and let you downgrade to other options.
*
* Feel free to contact me via email.
*
* Copyright 2011, Alexey Androsov
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) or GPL Version 3 (http://www.gnu.org/licenses/gpl.html) licenses.
*
* @requires swfobject
* @author Alexey Androsov <[email protected]>
* @version 1.0.2
*
* Thanks to flashblockdetector project (http://code.google.com/p/flashblockdetector)
*/
(function(/**document*/document, /**window*/window) {
function remove(node) {
var parent = node.parentNode;
parent && parent.removeChild(node);
}
/**
* FlashBlockNotifier is a wrapper for swfobject that detects FlashBlock in browser.
*/
var FlashBlockNotifier = window['FlashBlockNotifier'] = {
/**
* CSS-class for swf wrapper.
* @protected
* @default fbn-swf-wrapper
* @type String
*/
__SWF_WRAPPER_CLASS: 'fbn-swf-wrapper',
/**
* Timeout for flash block detect
* @default 500
* @protected
* @type Number
*/
__TIMEOUT: 500,
__TESTS: [
// Chrome notification "The flash plug-in was blocked because it is out of date"
//TODO: this test is very unstable, try to find another one
function(swfNode) {
return swfNode.getAttribute('title') === 'Flash';
},
// Chome FlashBlock extension (https://chrome.google.com/webstore/detail/cdngiadmnkhgemkimkhiilgffbjijcie)
// Chome FlashBlock extension (https://chrome.google.com/webstore/detail/gofhjkjmkpinhpoiabjplobcaignabnl)
function(swfNode, wrapperNode) {
// we expect that swf is the only child of wrapper
return wrapperNode.childNodes.length > 1
},
// older Safari ClickToFlash (http://rentzsch.github.com/clicktoflash/)
function(swfNode) {
// IE has no swfNode.type
return swfNode.type && swfNode.type != 'application/x-shockwave-flash'
},
// FlashBlock for Firefox (https://addons.mozilla.org/ru/firefox/addon/flashblock/)
// Chrome FlashFree (https://chrome.google.com/webstore/detail/ebmieckllmmifjjbipnppinpiohpfahm)
function(swfNode) {
// swf have been detached from DOM
return !swfNode.parentNode;
},
// Safari ClickToFlash Extension (http://hoyois.github.com/safariextensions/clicktoplugin/)
function(swfNode) {
var parentNode = swfNode.parentNode;
if (parentNode) {
if (parentNode.className.indexOf('CTFnodisplay') > -1) {
return true;
}
try {
// safe parentNode chain :)
if (parentNode.parentNode.parentNode.id.indexOf('CTP') > -1) {
//CTF Version >= 2.6
return true;
}
} catch (e) {
}
}
return false;
}
],
/**
* Embed SWF info page. This function has same options as swfobject.embedSWF except last param removeBlockedSWF.
* @see http://code.google.com/p/swfobject/wiki/api
* @param swfUrlStr
* @param replaceElemIdStr
* @param widthStr
* @param heightStr
* @param swfVersionStr
* @param xiSwfUrlStr
* @param flashvarsObj
* @param parObj
* @param attObj
* @param callbackFn
* @param {Boolean} [removeBlockedSWF=true] Remove swf if blocked
*/
embedSWF: function(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn, removeBlockedSWF) {
var swfobject = window['swfobject'];
if (!swfobject) {
return;
}
swfobject.addDomLoadEvent(function() {
var replaceElement = document.getElementById(replaceElemIdStr);
if (!replaceElement) {
return;
}
// We need to create div-wrapper because some flash block plugins replace swf with another content.
// Also some flash requires wrapper to work properly.
var wrapper = document.createElement('div');
wrapper.className = FlashBlockNotifier.__SWF_WRAPPER_CLASS;
replaceElement.parentNode.replaceChild(wrapper, replaceElement);
wrapper.appendChild(replaceElement);
swfobject.embedSWF(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, function(e) {
// e.success === false means that browser don't have flash or flash is too old
// @see http://code.google.com/p/swfobject/wiki/api
if (!e || e.success === false) {
callbackFn(e);
} else {
var swfElement = e['ref'],
getSVGDocument = false;
try {
//IE9 has this method, but crash on execute
getSVGDocument = swfElement && swfElement['getSVGDocument'] && swfElement['getSVGDocument']();
} catch (e) {
getSVGDocument = false;
}
if (getSVGDocument) {
// Opera 11.5 and above replaces flash with SVG button
onFailure(e);
} else {
//set timeout to let FlashBlock plugin detect swf and replace it some contents
window.setTimeout(function() {
var TESTS = FlashBlockNotifier.__TESTS;
for (var i = 0, j = TESTS.length; i < j; i++) {
if (TESTS[i](swfElement, wrapper)) {
onFailure(e);
return;
}
}
callbackFn(e);
}, FlashBlockNotifier.__TIMEOUT);
}
}
function onFailure(e) {
if (removeBlockedSWF !== false) {
//remove swf
swfobject.removeSWF(replaceElemIdStr);
//remove wrapper
remove(wrapper);
//remove extension artefacts
//ClickToFlash artefacts
var ctf = document.getElementById('CTFstack');
if (ctf) {
remove(ctf);
}
//Chrome FlashBlock artefact
var lastBodyChild = document.body.lastChild;
if (lastBodyChild && lastBodyChild.className == 'ujs_flashblock_placeholder') {
remove(lastBodyChild);
}
}
e.success = false;
e.__fbn = true;
callbackFn(e);
}
});
});
}
};
})(document, window);