2 window.addEventListener("load", function() { myExtension.init(); }, false);
6 var appcontent = document.getElementById("appcontent"); // browser
8 appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
9 var messagepane = document.getElementById("messagepane"); // mail
11 messagepane.addEventListener("load", function () { myExtension.onPageLoad(); }, true);
14 onPageLoad: function(aEvent) {
15 checkPageOnLoad(aEvent);
31 for(i=str.length-1;i>=0;i--) {
32 revstr+=str.charAt(i);
37 function getDocuments(aFrame,aArray) {
38 const frames = aFrame.frames;
39 aArray.push(aFrame.document);
40 for(var i = 0; i < frames.length; i++)
41 this.getDocuments(frames[i],aArray);
45 function getBrowserForDocument(aDocument) {
46 // This function can return the browser for not only top level documents
47 // but also documents contained in subframes
49 // If the document is top level then return its browser here
50 var browsers = gBrowser.browsers;
51 for(var i = 0; i < browsers.length; i++) {
52 if(aDocument == browsers[i].contentDocument) return browsers[i];
55 // Else it must be in a subframe, so check all frames in each browser
56 // to find the parent browser
58 for(var j = 0; j < browsers.length; j++) {
59 documents = this.getDocuments(browsers[j].contentWindow,new Array());
60 for(var k = 0; k < documents.length; k++) {
61 if(aDocument == documents[k]) return browsers[j];
65 // Else the browser could not be determined
69 function checkPageOnLoad(aEvent) {
71 var doc=aEvent.originalTarget;
72 var loc=doc.location.href;
73 var host=doc.location.hostname;
76 if (host.match(/video.google\./i)!=null) {
77 // Get the browser for the document (if required)
79 var browser = this.getBrowserForDocument(doc);
84 embmedia=doc.getElementById("embed_player_1");
85 if ((embmedia!=null)&&(idInterval==-1)) {
87 // Extract video URL (look at clive code in parse.py for probably stabler code)
88 source=unescape(embmedia.getAttribute('src'));
89 stUrl=source.substr(source.indexOf('videoUrl=')+9);
90 stUrl=stUrl.split("&thumbnailUrl=")[0];
91 idInterval = setInterval(replaceTag, timeout, doc, "embed_player_1", new Array(embmedia));
96 setTimeout(checkPageOnLoad, timeout,aEvent);
99 if (host.match(/youtube\./i)!=null) {
101 if ((embmedia!=null)&&(idInterval==-1)) {
102 embid=embmedia[0].getAttribute('id');
103 flashvars=unescape(embmedia[0].getAttribute('flashvars'));
104 if ((embid==null)||(flashvars==null)){
105 setTimeout(checkPageOnLoad,timeout,aEvent);
109 if ((embid=='movie_player')||(flashvars.match(/video_id=/i)!=null)) {
110 id = flashvars.split("video_id=")[1].split("&")[0];
111 valid = flashvars.split("&t=")[1].split("&")[0];
112 stUrl = "http://www.youtube.com/get_video?video_id="+id+"&t="+valid;//&"&fmt=18"; for quality control
113 idInterval = setInterval(replaceTag, timeout, doc, "movie_player", embmedia);
117 setTimeout(checkPageOnLoad,timeout,aEvent);
120 if (host.match(/myspace\./i)!=null) {
123 if ((embmedia!=null)&&(idInterval==-1)) {
125 // Myspace loves embedded flash ads, so we have to find the good one for the video...
126 for(i=0;i<embmedia.length;i++){
127 source=unescape(embmedia[i].getAttribute('src'));
132 if(source.match(/videoid=/i)!=null){
134 id = (source.split("videoid=")[1]).split("&")[0];
135 }else if (source.match(/m=/i)!=null){
136 // video channels myspace or hot stuff
137 id = (source.split("m=")[1]).split("&")[0].split(",")[0];
141 stUrl=getMySpaceURL(doc, id);
142 idInterval = setInterval(replaceTag, timeout, doc, " ", new Array(embmedia[i]));
151 if (host.match(/video.yahoo\./i)!=null) {
153 if ((embmedia!=null)&&(idInterval==-1)) {
154 for(i=0;i<embmedia.length;i++){
155 embid = embmedia[i].getAttribute('id');
157 stUrl = getURLfromKeepVid(loc);
159 this.replaceTag(doc, embid, new Array(embmedia[i]));
165 setTimeout(checkPageOnLoad,timeout,aEvent);
169 if (host.match(/video.yahoo\./i)!=null) {
171 if ((embmedia!=null)&&(idInterval==-1)) {
172 for(i=0;i<embmedia.length;i++){
173 embid = embmedia[i].getAttribute('id');
175 stUrl = getURLfromKeepVid(loc);
177 this.replaceTag(doc, embid, new Array(embmedia[i]));
183 setTimeout(checkPageOnLoad,timeout,aEvent);
189 //if (loc.match(/my\.yahoo\..*\/\?rd\=nux/i)!=null) {
190 // atag=doc.getElementsByTagName('a');
191 // for (i=0;i<atag.length;++i) {
192 // if (atag[i].text.match(/Continue to My Yahoo! Beta/i)) {
193 // url = atag[i].href;
194 // doc.location.href = url;
199 if (host.match(/dailymotion\./i)!=null) {
202 if ((embmedia!=null)&&(idInterval==-1)) {
203 embid=embmedia[0].getAttribute('id');
206 if (embid=='videoplayer') {
208 source='&'+unescape(embmedia[0].getAttribute('flashvars'));
209 stUrl=source.substr(source.indexOf("http://www.dailymotion.com/cdn/H264-512x384"));
210 stUrl=stUrl.split("@@")[0];
211 idInterval = setInterval(replaceTag, timeout, doc, "videoplayer", embmedia);
214 setTimeout(checkPageOnLoad,timeout,aEvent);
220 function replaceTagFrame(doc, frame, stTag) {
221 replaceTag(doc, stTag, frame.document.embeds);
225 function replaceTag(doc, stTag, embeds) {
226 for (i=0; i < embeds.length; i++) {
227 if ((stTag == " ")||(embeds[i].id == stTag)) {
229 if (obj.id != "pconrails") {
230 var rails = doc.createElement("embed");
231 rails.setAttribute("type", "video/x-flv");
232 rails.setAttribute("id", "pconrails");
233 rails.setAttribute("width", obj.clientWidth);
234 rails.setAttribute("height", obj.clientHeight);
235 rails.setAttribute("src", stUrl);
236 rails.setAttribute("autostart", "true");
237 doc.body.appendChild(rails);
238 obj.parentNode.replaceChild(rails, obj);
239 clearInterval(idInterval);
246 function getURLfromKeepVid(loc){
247 if(loc.match(/watch\//i)!=null){
249 while (url.match("/")){
250 url = url.replace("/","%2F");
252 url = "http://keepvid.com/?url="+url;
254 var req = new XMLHttpRequest();
255 req.open('GET', url, false);
258 var res = req.responseText;
260 /* should better parse DOM tree, but first have to build it, and this code does not work...
261 var dom = document.createElement('div');
262 dom.innerHTML = res.replace(/<script(.|\s)*?\/script>/g, '');
263 alert(dom+" "+dom.innerHTML);
264 atag = dom.getElementsByTagName("h2");
265 alert("atag.length="+atag.length+" atag="+atag);
266 for (i=0;i<atag.length;i++) {
267 alert("atag["+i+"]= "+atag[i]);
268 if (atag[i].text.match(">> Download <<")) {
274 // ... so we parse the HTML instead
275 matchString = "›› Download ‹‹";
276 if (res.match(matchString)){
277 indexEnd = res.indexOf(matchString)-1;
281 while(url.match(tag)){
282 indexStart = url.indexOf(tag)+tag.length;
283 url = url.substring(indexStart,indexEnd);
284 indexEnd -= indexStart;
286 url = url.split("\"")[0];
287 while(url.match("&")){
288 url = url.replace("&","&");
297 function getMySpaceURL(doc, id) {
298 url = 'http://mediaservices.myspace.com/services/rss.ashx?type=video&videoID=' + id;
300 var req = new XMLHttpRequest();
301 req.open('GET', url, false);
304 var xml = req.responseText;
305 return xml.split('media:content url="')[1].split('"')[0];
308 function getFeaturedYouTubeURL(doc, id) {
309 var url = "http://www.youtube.com/watch?v=" + id;
311 var req = new XMLHttpRequest();
312 req.open('GET', url, false);
315 var html = req.responseText;
317 source=html.match(/video_id=\S+&.+&t=.+&f/i);
318 return String(source).replace(/(video_id=\S+)&.+(&t=.+)&f/i,'http:\/\/www.youtube.com\/get_video?$1$2');
321 function replaceInnerHTML_Yahoo(doc) {
322 var obj = doc.getElementById("y_embed");
327 var src = obj.innerHTML;
329 if (src.indexOf("makeplaylist.dll") != -1) {
330 var indexStart = obj.innerHTML.indexOf("pList=")+6;
331 var indexEnd = obj.innerHTML.indexOf("&", indexStart);
333 var newSrc = unescape(obj.innerHTML.substring(indexStart, indexEnd));
335 var req = new XMLHttpRequest();
336 req.open('GET', newSrc, false);
339 var newEmbed = req.responseText;
341 newEmbed = newEmbed.replace(/application\/x\-shockwave\-flash/, "video/x-flv");
343 indexStart = newEmbed.indexOf("<EMBED");
344 indexEnd = newEmbed.indexOf("</EMBED>")+8;
346 newHTML = newEmbed.substring(indexStart, indexEnd);
349 newHTML = newHTML.replace(/width=(\S+)\s/i, "width=" + width + " ");
352 newHTML = newHTML.replace(/height=(\S+)\s/i, "height=" + height + " ");
355 obj.innerHTML = newHTML;
356 clearInterval(idInterval);