forked from scribd/flash_heed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash_heed.js
94 lines (76 loc) · 3.44 KB
/
flash_heed.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
var FlashHeed = (function(window) {
var document = window.document;
var gsub = function(string, pattern, replacement) {
var result = '', source = string, match;
while (source.length > 0) {
if (match = source.match(pattern)) {
result += source.slice(0, match.index);
result += replacement;
source = source.slice(match.index + match[0].length);
} else {
result += source, source = '';
}
}
return result;
};
var heed = function(el) {
if(el === undefined || el === null) var el = document;
var objects = el.getElementsByTagName('object');
var len = objects.length;
var i;
for(i = 0; i < len; i++) {
var o = objects[i];
var params = o.getElementsByTagName('param');
var params_length = params.length;
var embeds = o.getElementsByTagName('embed');
var embed = null;
if(embeds.length > 0) var embed = embeds[0];
// Handle embed tag (for non-IE)
if(embed) {
// Need to set the embed wmode attribute
// In this case, we need to remove and re-add the child node
embed.setAttribute('wmode', 'transparent');
var nx = embed.nextSibling, pn = embed.parentNode;
pn.removeChild(embed);
pn.insertBefore(embed, nx);
}
// Handle param tags (for IE)
var correct_wmode_found = false;
var incorrect_wmode_found = false;
for(var j = 0; j < params_length; j++) {
if(params[j].name === 'wmode') {
if(/transparent/i.test(params[j].value) || /opaque/i.test(params[j].value)) {
// an existing wmode with "transparent" or "opaque" is found
correct_wmode_found = true;
} else {
incorrect_wmode_found = true;
}
}
}
if(!correct_wmode_found || incorrect_wmode_found) {
var html = o.outerHTML;
var nx = o.nextSibling, pn = o.parentNode;
// Do a string replacement for a window param that IE injects be default to the innerhtml
html = gsub(html, /<param name="wmode".*?>/i, '');
// Add the correct transparent wmode param
html = gsub(html, /<\/object>/i, '<PARAM NAME="WMode" VALUE="Transparent"></object>');
// Totally remove the object tag from the dom
pn.removeChild(o);
// Add it to our new div, only to clobber it immediately.
// This is the only way we've found to force IE to unrender the object.
// We use a new container for this because you can't mess with the innerhtml
// of an object tag. (throws a runtime error)
var div = document.createElement("div");
div.appendChild(o);
div.innerHTML = '';
// Update it with our new HTML that has the correct param tag
div.innerHTML = html;
// Finally, insert this new div back in the original spot
pn.insertBefore(div, nx);
}
}
}
return {
heed: heed
}
})(window);