gecko-mediaplayer: make ai-flash.js work with midori as well
[vuplus_openembedded] / recipes / gnome-mplayer / gecko-mediaplayer / extensions / flash@alwaysinnovating.com / chrome / content / ai-flash.js
1 // ==UserScript==
2 // @name Flash AI
3 // @description Flash AI add-on
4 // @include *
5 // ==/UserScript==
6
7
8 var firefox = true;
9 if (navigator.userAgent.match("midori")) firefox = false;
10
11 if (!firefox){
12         window.addEventListener("load", function() { myExtension.onPageLoad(); }, true);
13 }else{
14         window.addEventListener("load", function() { myExtension.init(); }, false);
15 }
16 var myExtension = {
17         init: function() {
18                 var appcontent = document.getElementById("appcontent");   // browser
19                 if(appcontent)
20                         appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
21                 var messagepane = document.getElementById("messagepane"); // mail
22                 if(messagepane)
23                         messagepane.addEventListener("load", function () { myExtension.onPageLoad(); }, true);
24         },
25         
26         onPageLoad: function(aEvent) {
27                 checkPageOnLoad(aEvent);
28         }
29 }
30
31
32 var idInterval=-1;
33 var stUrl = "";
34 var timeout=2000;
35
36 var width = 0;
37 var height = 0;
38
39 function rev(str) {
40         if(!str)
41                 return'';
42         var revstr='';
43         for(i=str.length-1;i>=0;i--) {
44                 revstr+=str.charAt(i);
45         }
46         return revstr;
47 }
48
49 function getDocuments(aFrame,aArray) {
50         const frames = aFrame.frames;
51         aArray.push(aFrame.document);
52         for(var i = 0; i < frames.length; i++)
53                 this.getDocuments(frames[i],aArray);
54         return aArray;
55 }
56
57 function getBrowserForDocument(aDocument) {
58         // This function can return the browser for not only top level documents
59         // but also documents contained in subframes
60         
61         // If the document is top level then return its browser here
62         var browsers = gBrowser.browsers;
63         for(var i = 0; i < browsers.length; i++) {
64                 if(aDocument == browsers[i].contentDocument) return browsers[i];
65         }
66         
67         // Else it must be in a subframe, so check all frames in each browser
68         // to find the parent browser
69         var documents;
70         for(var j = 0; j < browsers.length; j++) {
71                 documents = this.getDocuments(browsers[j].contentWindow,new Array());
72                 for(var k = 0; k < documents.length; k++) {
73                         if(aDocument == documents[k]) return browsers[j];
74                 }
75         }
76         
77         // Else the browser could not be determined
78         return null;
79 }
80
81 function checkPageOnLoad(aEvent) {
82         var doc;        
83         if(firefox){
84                 doc=aEvent.originalTarget;
85         }else{
86                 doc=document;
87         }
88         var loc=doc.location.href;
89         var host=doc.location.hostname;
90         idInterval=-1;
91
92         if (host.match(/video.google\./i)!=null) {
93                 // Get the browser for the document (if required)
94            
95                 var browser = this.getBrowserForDocument(doc);
96                 if(!browser){
97                         return;
98                 }
99
100                 embmedia=doc.getElementById("embed_player_1");
101                 if ((embmedia!=null)&&(idInterval==-1)) {
102                 
103                         // Extract video URL (look at clive code in parse.py for probably stabler code)
104                         source=unescape(embmedia.getAttribute('src'));
105                         stUrl=source.substr(source.indexOf('videoUrl=')+9);
106                         stUrl=stUrl.split("&thumbnailUrl=")[0];
107                         idInterval = setInterval(replaceTag, timeout, doc, "embed_player_1", new Array(embmedia));
108
109                         return;
110                 }
111
112                 setTimeout(checkPageOnLoad, timeout,aEvent);
113         }
114
115         if (host.match(/youtube\./i)!=null) {
116                 embmedia=doc.embeds;            
117                 if ((embmedia!=null)&&(idInterval==-1)) {
118                         embid=embmedia[0].getAttribute('id');
119                         flashvars=unescape(embmedia[0].getAttribute('flashvars'));
120                         if ((embid==null)||(flashvars==null)){
121                                 setTimeout(checkPageOnLoad,timeout,aEvent);
122                                 return;
123                         }
124
125                         if ((embid=='movie_player')||(flashvars.match(/video_id=/i)!=null)) {
126                                 id = flashvars.split("video_id=")[1].split("&")[0];
127                                 valid = flashvars.split("&t=")[1].split("&")[0];
128                                 stUrl = "http://www.youtube.com/get_video?video_id="+id+"&t="+valid;//&"&fmt=18"; for quality control
129                                 idInterval = setInterval(replaceTag, timeout, doc, "movie_player", embmedia);                       
130                         }
131                                               
132                 }
133                 setTimeout(checkPageOnLoad,timeout,aEvent);
134         }
135
136         if (host.match(/myspace\./i)!=null) {
137
138                 embmedia=doc.embeds;
139                 if ((embmedia!=null)&&(idInterval==-1)) {
140                                     
141                         // Myspace loves embedded flash ads, so we have to find the good one for the video...
142                         for(i=0;i<embmedia.length;i++){
143                                 source=unescape(embmedia[i].getAttribute('src'));
144                                 if (source==null){
145                                         return;
146                                 }else{
147                                         id=-1;
148                                         if(source.match(/videoid=/i)!=null){
149                                                 // regular video
150                                                 id = (source.split("videoid=")[1]).split("&")[0];
151                                         
152                                         }else if (source.match(/m=/i)!=null){
153                                                 // video channels myspace or hot stuff
154                                                 id = (source.split("m=")[1]).split("&")[0].split(",")[0];                               
155                                         }
156                                         if(id!=-1){
157                                                 stUrl=getMySpaceURL(doc, id);
158                                                 idInterval = setInterval(replaceTag, timeout, doc, " ", new Array(embmedia[i]));
159                                                 return;
160                                         }
161                                 }
162                         }
163                 }
164
165         }
166         
167         if (host.match(/video.yahoo\./i)!=null) {
168                 embmedia=doc.embeds;
169
170                 if ((embmedia!=null)&&(idInterval==-1)) {
171                         for(i=0;i<embmedia.length;i++){
172                                 embid = embmedia[i].getAttribute('id');
173                                 if(embid=="video1"){
174                                         stUrl = getURLfromKeepVid(loc);
175                                         if (stUrl != null){
176                                                 this.replaceTag(doc, embid, new Array(embmedia[i]));
177                                                 return;
178                                         }
179                                 }
180                         }
181                 }
182                 setTimeout(checkPageOnLoad,timeout,aEvent);                         
183                 return;
184         }
185
186
187         //if (loc.match(/my\.yahoo\..*\/\?rd\=nux/i)!=null) {
188         //       atag=doc.getElementsByTagName('a');
189         //        for (i=0;i<atag.length;++i) {
190         //              if (atag[i].text.match(/Continue to My Yahoo! Beta/i)) {
191         //                      url = atag[i].href;
192         //                      doc.location.href = url;
193         //              }
194         //      }
195         //}
196
197         if (host.match(/dailymotion\./i)!=null) {
198                 embmedia=doc.embeds;
199                 if ((embmedia!=null)&&(idInterval==-1)) {
200                         embid=embmedia[0].getAttribute('id');
201
202                         // regular video
203                         if (embid=='videoplayer') {
204                                 // current player
205                                 source='&'+unescape(embmedia[0].getAttribute('flashvars'));
206                                 stUrl=source.substr(source.indexOf("http://www.dailymotion.com/cdn/H264-512x384"));
207                                 stUrl=stUrl.split("@@")[0];
208                                 idInterval = setInterval(replaceTag, timeout, doc, "videoplayer", embmedia);
209                         }
210                 }
211                 setTimeout(checkPageOnLoad,timeout,aEvent);
212         }
213 }
214
215
216
217 function replaceTagFrame(doc, frame, stTag) {
218         replaceTag(doc, stTag, frame.document.embeds);
219 }
220
221
222 function replaceTag(doc, stTag, embeds) {
223         for (i=0; i < embeds.length; i++) {
224                 if ((stTag == " ")||(embeds[i].id == stTag)) {
225                         var obj = embeds[i];
226                         if (obj.id != "pconrails") {
227                                 var rails = doc.createElement("embed");
228                                 rails.setAttribute("type", "video/x-flv");
229                                 rails.setAttribute("id", "pconrails");
230                                 rails.setAttribute("width", obj.clientWidth);
231                                 rails.setAttribute("height", obj.clientHeight);
232                                 rails.setAttribute("src", stUrl);
233                                 rails.setAttribute("autostart", "true");
234                                 doc.body.appendChild(rails);
235                                 obj.parentNode.replaceChild(rails, obj);
236                                 clearInterval(idInterval);
237                         }
238                 }
239         }
240 }
241
242
243 function getURLfromKeepVid(loc){
244         if(loc.match(/watch\//i)!=null){
245                 url = escape(loc);
246                 while (url.match("/")){
247                         url = url.replace("/","%2F"); 
248                 }
249                 url = "http://keepvid.com/?url="+url;
250
251                 var req = new XMLHttpRequest();
252                 req.open('GET', url, false);
253                 req.send(null);
254             
255                 var res = req.responseText;
256
257                 /* should better parse DOM tree, but first have to build it, and this code does not work...
258                    var dom = document.createElement('div');
259                    dom.innerHTML = res.replace(/<script(.|\s)*?\/script>/g, '');
260                    alert(dom+"    "+dom.innerHTML);
261                    atag = dom.getElementsByTagName("h2");
262                    alert("atag.length="+atag.length+" atag="+atag);
263                    for (i=0;i<atag.length;i++) {
264                    alert("atag["+i+"]= "+atag[i]);
265                    if (atag[i].text.match(">> Download <<")) {
266                     
267                    return atag[i].href;
268                    }
269                    }
270                 */
271                 // ... so we parse the HTML instead
272                 matchString = "&rsaquo;&rsaquo; Download &lsaquo;&lsaquo;";
273                 if (res.match(matchString)){
274                         indexEnd = res.indexOf(matchString)-1;
275                         indexStart = 0;
276                         tag = "<a href=\"";
277                         var url = res;
278                         while(url.match(tag)){
279                                 indexStart = url.indexOf(tag)+tag.length;
280                                 url = url.substring(indexStart,indexEnd);
281                                 indexEnd -= indexStart;
282                         }
283                         url = url.split("\"")[0];
284                         while(url.match("&amp;")){
285                                 url = url.replace("&amp;","&");
286                         }
287                         return url;
288                 }
289             
290         }
291         return null;
292 }
293
294 function getMySpaceURL(doc, id) {
295         url = 'http://mediaservices.myspace.com/services/rss.ashx?type=video&videoID=' + id; 
296
297         var req = new XMLHttpRequest();
298         req.open('GET', url, false);
299         req.send(null);
300
301         var xml = req.responseText;
302         return xml.split('media:content url="')[1].split('"')[0];
303 }       
304
305 function getFeaturedYouTubeURL(doc, id) {
306         var url = "http://www.youtube.com/watch?v=" + id;
307
308         var req = new XMLHttpRequest();
309         req.open('GET', url, false);
310         req.send(null);
311
312         var html = req.responseText;
313
314         source=html.match(/video_id=\S+&.+&t=.+&f/i);
315         return String(source).replace(/(video_id=\S+)&.+(&t=.+)&f/i,'http:\/\/www.youtube.com\/get_video?$1$2');
316 }
317
318 function replaceInnerHTML_Yahoo(doc) {
319         var obj = doc.getElementById("y_embed");
320
321         if (obj == null)
322                 return;
323
324         var src = obj.innerHTML;
325
326         if (src.indexOf("makeplaylist.dll") != -1) {
327                 var indexStart = obj.innerHTML.indexOf("pList=")+6;
328                 var indexEnd = obj.innerHTML.indexOf("&", indexStart);
329
330                 var newSrc = unescape(obj.innerHTML.substring(indexStart, indexEnd));
331
332                 var req = new XMLHttpRequest();
333                 req.open('GET', newSrc, false);
334                 req.send(null);
335                 
336                 var newEmbed = req.responseText;
337
338                 newEmbed = newEmbed.replace(/application\/x\-shockwave\-flash/, "video/x-flv");
339
340                 indexStart = newEmbed.indexOf("<EMBED");
341                 indexEnd = newEmbed.indexOf("</EMBED>")+8;
342
343                 newHTML = newEmbed.substring(indexStart, indexEnd);
344
345                 if (width != 0) {
346                         newHTML = newHTML.replace(/width=(\S+)\s/i, "width=" + width + " ");
347                 }
348                 if (height != 0) {                      
349                         newHTML = newHTML.replace(/height=(\S+)\s/i, "height=" + height + " ");
350                 }
351
352                 obj.innerHTML = newHTML;
353                 clearInterval(idInterval);
354         }
355 }
356
357