c21ad588479b9c7aea14409ed50c3e4431a512bf
[openembedded.git] /
1
2 window.addEventListener("load", function() { myExtension.init(); }, false);
3
4 var myExtension = {
5         init: function() {
6                 var appcontent = document.getElementById("appcontent");   // browser
7                 if(appcontent)
8                         appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
9                 var messagepane = document.getElementById("messagepane"); // mail
10                 if(messagepane)
11                         messagepane.addEventListener("load", function () { myExtension.onPageLoad(); }, true);
12         },
13         
14         onPageLoad: function(aEvent) {
15                 checkPageOnLoad(aEvent);
16         }
17 }
18
19
20         var idInterval=-1;
21 var stUrl = "";
22 var timeout=2000;
23
24 var width = 0;
25 var height = 0;
26
27 function rev(str) {
28         if(!str)
29                 return'';
30         var revstr='';
31         for(i=str.length-1;i>=0;i--) {
32                 revstr+=str.charAt(i);
33         }
34         return revstr;
35 }
36
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);
42         return aArray;
43 }
44
45 function getBrowserForDocument(aDocument) {
46         // This function can return the browser for not only top level documents
47         // but also documents contained in subframes
48         
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];
53         }
54         
55         // Else it must be in a subframe, so check all frames in each browser
56         // to find the parent browser
57         var documents;
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];
62                 }
63         }
64         
65         // Else the browser could not be determined
66         return null;
67 }
68
69 function checkPageOnLoad(aEvent) {
70
71         var doc=aEvent.originalTarget;
72         var loc=doc.location.href;
73         var host=doc.location.hostname;
74         idInterval=-1;
75
76         if (host.match(/video.google\./i)!=null) {
77                 // Get the browser for the document (if required)
78            
79                 var browser = this.getBrowserForDocument(doc);
80                 if(!browser){
81                         return;
82                 }
83
84                 embmedia=doc.getElementById("embed_player_1");
85                 if ((embmedia!=null)&&(idInterval==-1)) {
86                 
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));
92
93                         return;
94                 }
95
96                 setTimeout(checkPageOnLoad, timeout,aEvent);
97         }
98
99         if (host.match(/youtube\./i)!=null) {
100                 embmedia=doc.embeds;
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);
106                                 return;
107                         }
108
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);                       
114                         }
115                                               
116                 }
117                 setTimeout(checkPageOnLoad,timeout,aEvent);
118         }
119
120         if (host.match(/myspace\./i)!=null) {
121
122                 embmedia=doc.embeds;
123                 if ((embmedia!=null)&&(idInterval==-1)) {
124                     
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'));
128                                 if (source==null){
129                                         return;
130                                 }else{
131                                         id=-1;
132                                         if(source.match(/videoid=/i)!=null){
133                                                 // regular video
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];                               
138                                         }
139                             
140                                         if(id!=-1){
141                                                 stUrl=getMySpaceURL(doc, id);
142                                                 idInterval = setInterval(replaceTag, timeout, doc, " ", new Array(embmedia[i]));
143                                                 return;
144                                         }
145                                 }
146                         }
147                 }
148
149         }
150         
151         if (host.match(/video.yahoo\./i)!=null) {
152                 embmedia=doc.embeds;
153                 if ((embmedia!=null)&&(idInterval==-1)) {
154                         for(i=0;i<embmedia.length;i++){
155                                 embid = embmedia[i].getAttribute('id');
156                                 if(embid=="video1"){
157                                         stUrl = getURLfromKeepVid(loc);
158                                         if (stUrl != null){
159                                                 this.replaceTag(doc, embid, new Array(embmedia[i]));
160                                                 return;
161                                         }
162                                 }
163                         }
164                 }
165                 setTimeout(checkPageOnLoad,timeout,aEvent);                         
166                 return;
167         }
168
169         if (host.match(/video.yahoo\./i)!=null) {
170                 embmedia=doc.embeds;
171                 if ((embmedia!=null)&&(idInterval==-1)) {
172                         for(i=0;i<embmedia.length;i++){
173                                 embid = embmedia[i].getAttribute('id');
174                                 if(embid=="video1"){
175                                         stUrl = getURLfromKeepVid(loc);
176                                         if (stUrl != null){
177                                                 this.replaceTag(doc, embid, new Array(embmedia[i]));
178                                                 return;
179                                         }
180                                 }
181                         }
182                 }
183                 setTimeout(checkPageOnLoad,timeout,aEvent);                         
184                 return;
185         }
186         
187         
188
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;
195         //              }
196         //      }
197         //}
198
199         if (host.match(/dailymotion\./i)!=null) {
200          
201                 embmedia=doc.embeds;
202                 if ((embmedia!=null)&&(idInterval==-1)) {
203                         embid=embmedia[0].getAttribute('id');
204
205                         // regular video
206                         if (embid=='videoplayer') {
207                                 // current player
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);
212                         }
213                 }
214                 setTimeout(checkPageOnLoad,timeout,aEvent);
215         }
216 }
217
218
219
220 function replaceTagFrame(doc, frame, stTag) {
221         replaceTag(doc, stTag, frame.document.embeds);
222 }
223
224
225 function replaceTag(doc, stTag, embeds) {
226         for (i=0; i < embeds.length; i++) {
227                 if ((stTag == " ")||(embeds[i].id == stTag)) {
228                         var obj = embeds[i];
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);
240                         }
241                 }
242         }
243 }
244
245
246 function getURLfromKeepVid(loc){
247         if(loc.match(/watch\//i)!=null){
248                 url = escape(loc);
249                 while (url.match("/")){
250                         url = url.replace("/","%2F"); 
251                 }
252                 url = "http://keepvid.com/?url="+url;
253
254                 var req = new XMLHttpRequest();
255                 req.open('GET', url, false);
256                 req.send(null);
257             
258                 var res = req.responseText;
259
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 <<")) {
269                     
270                    return atag[i].href;
271                    }
272                    }
273                 */
274                 // ... so we parse the HTML instead
275                 matchString = "&rsaquo;&rsaquo; Download &lsaquo;&lsaquo;";
276                 if (res.match(matchString)){
277                         indexEnd = res.indexOf(matchString)-1;
278                         indexStart = 0;
279                         tag = "<a href=\"";
280                         var url = res;
281                         while(url.match(tag)){
282                                 indexStart = url.indexOf(tag)+tag.length;
283                                 url = url.substring(indexStart,indexEnd);
284                                 indexEnd -= indexStart;
285                         }
286                         url = url.split("\"")[0];
287                         while(url.match("&amp;")){
288                                 url = url.replace("&amp;","&");
289                         }
290                         return url;
291                 }
292             
293         }
294         return null;
295 }
296
297 function getMySpaceURL(doc, id) {
298         url = 'http://mediaservices.myspace.com/services/rss.ashx?type=video&videoID=' + id; 
299
300         var req = new XMLHttpRequest();
301         req.open('GET', url, false);
302         req.send(null);
303
304         var xml = req.responseText;
305         return xml.split('media:content url="')[1].split('"')[0];
306 }       
307
308 function getFeaturedYouTubeURL(doc, id) {
309         var url = "http://www.youtube.com/watch?v=" + id;
310
311         var req = new XMLHttpRequest();
312         req.open('GET', url, false);
313         req.send(null);
314
315         var html = req.responseText;
316
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');
319 }
320
321 function replaceInnerHTML_Yahoo(doc) {
322         var obj = doc.getElementById("y_embed");
323
324         if (obj == null)
325                 return;
326
327         var src = obj.innerHTML;
328
329         if (src.indexOf("makeplaylist.dll") != -1) {
330                 var indexStart = obj.innerHTML.indexOf("pList=")+6;
331                 var indexEnd = obj.innerHTML.indexOf("&", indexStart);
332
333                 var newSrc = unescape(obj.innerHTML.substring(indexStart, indexEnd));
334
335                 var req = new XMLHttpRequest();
336                 req.open('GET', newSrc, false);
337                 req.send(null);
338                 
339                 var newEmbed = req.responseText;
340
341                 newEmbed = newEmbed.replace(/application\/x\-shockwave\-flash/, "video/x-flv");
342
343                 indexStart = newEmbed.indexOf("<EMBED");
344                 indexEnd = newEmbed.indexOf("</EMBED>")+8;
345
346                 newHTML = newEmbed.substring(indexStart, indexEnd);
347
348                 if (width != 0) {
349                         newHTML = newHTML.replace(/width=(\S+)\s/i, "width=" + width + " ");
350                 }
351                 if (height != 0) {                      
352                         newHTML = newHTML.replace(/height=(\S+)\s/i, "height=" + height + " ");
353                 }
354
355                 obj.innerHTML = newHTML;
356                 clearInterval(idInterval);
357         }
358 }
359
360