-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplacer.js
More file actions
83 lines (76 loc) · 2.51 KB
/
replacer.js
File metadata and controls
83 lines (76 loc) · 2.51 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
var create_new_video = function(width, height, video_src, link_src){
var newVideo = document.createElement("iframe");
newVideo.height = height;
newVideo.width = width;
newVideo.src = video_src;
newVideo.frameBorder = "0";
var newLink = document.createElement("a")
newLink.innerHTML = "*";
newLink.href = link_src;
newLink.target = "_blank";
return newVideo.outerHTML + newLink.outerHTML;
}
var replace_object = function(o){
if (o.x_html5_to_video == 'yes'){
return;
}
var src = o.data;
var type = o.type;
if (o.getElementsByTagName("embed").length > 0) {
var e = o.getElementsByTagName("embed")[0];
src = e.src;
type = e.type;
}
//console.debug(o);
//console.debug(src);
if (type == 'application/x-shockwave-flash'){
//console.debug('maybe video');
var iframe_src, link_src;
if (src.search("http://(www\.)?youtube.com/v/") == 0) {
//console.debug('yourtube');
iframe_src = src.replace(/youtube.com\/v\/([\w-]+)([^\"\']*)/, "youtube.com/embed/$1");
link_src = src.replace("?", "&").replace("youtube.com/v/", "youtube.com/watch?v=");
} else if (src.search('http://vimeo.com') == 0) {
//console.debug('vimeo');
var clip_id = src.replace(/http:\/\/.*clip_id=(\d+).*/, '$1')
iframe_src = 'http://player.vimeo.com/video/' + clip_id;
link_src = 'http://vimeo.com/' + clip_id;
}
if (iframe_src && link_src) {
//console.debug("try replace");
o.outerHTML = create_new_video(
o.width, o.height,
iframe_src,
link_src
);
}
} else {
//console.debug('ingore');
o.x_html5_to_video = 'yes';
}
}
var do_replace = function(scope, why){
if (scope != document && scope.nodeType != 1) {
// console.debug('ignore scope');
// console.debug(scope);
return
}
//console.debug(why);
if (scope.tagName == "OBJECT") {
replace_object(scope);
return;
}
if (scope.tagName == "EMBED" && scope.parentNode) {
replace_object(scope.parentNode);
return;
}
var objects = scope.getElementsByTagName("object");
for (var i = 0; i < objects.length; ++i) {
//console.debug(why);
replace_object(objects[i]);
}
}
document.addEventListener("DOMNodeInserted", function(event){
do_replace(event.target, 'bind');
});
do_replace(document, 'doc');