3 // @description Flash AI add-on
9 if (navigator.userAgent.match("midori")) firefox = false;
12 window.addEventListener("load", function() { myExtension.onPageLoad(); }, true);
14 window.addEventListener("load", function() { myExtension.init(); }, false);
18 var appcontent = document.getElementById("appcontent"); // browser
20 appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
21 var messagepane = document.getElementById("messagepane"); // mail
23 messagepane.addEventListener("load", function () { myExtension.onPageLoad(); }, true);
26 onPageLoad: function(aEvent) {
27 checkPageOnLoad(aEvent);
43 for(i=str.length-1;i>=0;i--) {
44 revstr+=str.charAt(i);
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);
57 function getBrowserForDocument(aDocument) {
58 // This function can return the browser for not only top level documents
59 // but also documents contained in subframes
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];
67 // Else it must be in a subframe, so check all frames in each browser
68 // to find the parent browser
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];
77 // Else the browser could not be determined
81 function checkPageOnLoad(aEvent) {
84 doc=aEvent.originalTarget;
88 var loc=doc.location.href;
89 var host=doc.location.hostname;
92 if (host.match(/video.google\./i)!=null) {
93 // Get the browser for the document (if required)
95 var browser = this.getBrowserForDocument(doc);
100 embmedia=doc.getElementById("embed_player_1");
101 if ((embmedia!=null)&&(idInterval==-1)) {
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));
112 setTimeout(checkPageOnLoad, timeout,aEvent);
115 if (host.match(/youtube\./i)!=null) {
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);
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);
133 setTimeout(checkPageOnLoad,timeout,aEvent);
136 if (host.match(/myspace\./i)!=null) {
139 if ((embmedia!=null)&&(idInterval==-1)) {
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'));
148 if(source.match(/videoid=/i)!=null){
150 id = (source.split("videoid=")[1]).split("&")[0];
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];
157 stUrl=getMySpaceURL(doc, id);
158 idInterval = setInterval(replaceTag, timeout, doc, " ", new Array(embmedia[i]));
167 if (host.match(/video.yahoo\./i)!=null) {
170 if ((embmedia!=null)&&(idInterval==-1)) {
171 for(i=0;i<embmedia.length;i++){
172 embid = embmedia[i].getAttribute('id');
174 stUrl = getURLfromKeepVid(loc);
176 this.replaceTag(doc, embid, new Array(embmedia[i]));
182 setTimeout(checkPageOnLoad,timeout,aEvent);
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;
197 if (host.match(/dailymotion\./i)!=null) {
199 if ((embmedia!=null)&&(idInterval==-1)) {
200 embid=embmedia[0].getAttribute('id');
203 if (embid=='videoplayer') {
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);
211 setTimeout(checkPageOnLoad,timeout,aEvent);
217 function replaceTagFrame(doc, frame, stTag) {
218 replaceTag(doc, stTag, frame.document.embeds);
222 function replaceTag(doc, stTag, embeds) {
223 for (i=0; i < embeds.length; i++) {
224 if ((stTag == " ")||(embeds[i].id == stTag)) {
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);
243 function getURLfromKeepVid(loc){
244 if(loc.match(/watch\//i)!=null){
246 while (url.match("/")){
247 url = url.replace("/","%2F");
249 url = "http://keepvid.com/?url="+url;
251 var req = new XMLHttpRequest();
252 req.open('GET', url, false);
255 var res = req.responseText;
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 <<")) {
271 // ... so we parse the HTML instead
272 matchString = "›› Download ‹‹";
273 if (res.match(matchString)){
274 indexEnd = res.indexOf(matchString)-1;
278 while(url.match(tag)){
279 indexStart = url.indexOf(tag)+tag.length;
280 url = url.substring(indexStart,indexEnd);
281 indexEnd -= indexStart;
283 url = url.split("\"")[0];
284 while(url.match("&")){
285 url = url.replace("&","&");
294 function getMySpaceURL(doc, id) {
295 url = 'http://mediaservices.myspace.com/services/rss.ashx?type=video&videoID=' + id;
297 var req = new XMLHttpRequest();
298 req.open('GET', url, false);
301 var xml = req.responseText;
302 return xml.split('media:content url="')[1].split('"')[0];
305 function getFeaturedYouTubeURL(doc, id) {
306 var url = "http://www.youtube.com/watch?v=" + id;
308 var req = new XMLHttpRequest();
309 req.open('GET', url, false);
312 var html = req.responseText;
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');
318 function replaceInnerHTML_Yahoo(doc) {
319 var obj = doc.getElementById("y_embed");
324 var src = obj.innerHTML;
326 if (src.indexOf("makeplaylist.dll") != -1) {
327 var indexStart = obj.innerHTML.indexOf("pList=")+6;
328 var indexEnd = obj.innerHTML.indexOf("&", indexStart);
330 var newSrc = unescape(obj.innerHTML.substring(indexStart, indexEnd));
332 var req = new XMLHttpRequest();
333 req.open('GET', newSrc, false);
336 var newEmbed = req.responseText;
338 newEmbed = newEmbed.replace(/application\/x\-shockwave\-flash/, "video/x-flv");
340 indexStart = newEmbed.indexOf("<EMBED");
341 indexEnd = newEmbed.indexOf("</EMBED>")+8;
343 newHTML = newEmbed.substring(indexStart, indexEnd);
346 newHTML = newHTML.replace(/width=(\S+)\s/i, "width=" + width + " ");
349 newHTML = newHTML.replace(/height=(\S+)\s/i, "height=" + height + " ");
352 obj.innerHTML = newHTML;
353 clearInterval(idInterval);