Yansky

iviewfox 1.3.1 source

May 7th, 2011
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var EXPORTED_SYMBOLS = ["iViewFox"];  
  3.  
  4. let Cc = Components.classes;
  5. let Ci = Components.interfaces;
  6. let Cu = Components.utils;
  7.  
  8.  
  9. var iViewFox = {
  10.         log:function(msg){
  11.             var lg = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
  12.             lg.logStringMessage(msg);
  13.         },
  14.         currentWindow:function(){
  15.             let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
  16.             return wm.getMostRecentWindow('navigator:browser');
  17.         },
  18.         seriesIndex:null,
  19.         hashReg:new RegExp(/http:\/\/www.abc.net.au\/iview\/#\/view\/[0-9]{6}/),
  20.         uA:null,
  21.         osString:Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS,    
  22.         prefs:Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("extensions.iViewFox."),
  23.         extItem:null,
  24.         getrtDump:null,
  25.         hideIViewFoxStatusBeforeUnload:function(){
  26.             let win=iViewFox.currentWindow();
  27.             win.document.getElementById('iViewFoxStatus').setAttribute('hidden','true');
  28.             win.gBrowser.contentWindow.removeEventListener('beforeunload',iViewFox.hideIViewFoxStatusBeforeUnload,false);              
  29.         },
  30.         osxPermissionsFixer:function(){
  31.  
  32.             try {
  33.                 let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  34.  
  35.                 let args = [777, iViewFox.getrtDump+'b.command'];
  36.                 iViewFox.log(args.toSource());
  37.                 file.initWithPath("/bin/chmod");
  38.  
  39.                 let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  40.                 process.init(file);
  41.                 process.run(false, args, args.length, {});
  42.             }
  43.             catch(exc){
  44.                 Cu.reportError(exc);
  45.             }
  46.  
  47.         },
  48.         removeiViewFoxVersionData:{
  49.             observe : function(aSubject, aTopic, aData) {
  50.                 let extID = aSubject.QueryInterface(Components.interfaces.nsIUpdateItem).id;
  51.                 if(extID =="[email protected]" && data =='item-uninstalled' && iViewFox.prefs.getCharPref('version')){
  52.                     iViewFox.prefs.clearUserPref('version');
  53.                 }
  54.             },
  55.             onUninstalling: function(addon) {
  56.                 if (addon.id == "[email protected]" && iViewFox.prefs.getCharPref('version')) {
  57.                     iViewFox.prefs.clearUserPref('version');
  58.                 }
  59.             }
  60.         }, 
  61.         stuffForOSX:function(aAddon){
  62.  
  63.             iViewFox.extItem=aAddon.version;
  64.  
  65.             if( iViewFox.extItem != iViewFox.prefs.getCharPref('version')){
  66.                 iViewFox.prefs.setCharPref("version", iViewFox.extItem);
  67.                 //this is technically file io, so best to wait a bit and not add to the startup time
  68.                 let timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
  69.                 timer.initWithCallback({notify: iViewFox.osxPermissionsFixer}, 2000, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  70.             }
  71.             try{
  72.                 AddonManager.addAddonListener(iViewFox.removeiViewFoxVersionData);
  73.             }
  74.             catch(e){
  75.                 let observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  76.                 observerService.addObserver(iViewFox.removeiViewFoxVersionData, "em-action-requested", false);
  77.             }
  78.         },
  79.         status:function(chromeWin){
  80.  
  81.             if(!iViewFox.getrtDump){
  82.  
  83.                 let gB=chromeWin.gBrowser;
  84.  
  85.                 if(!iViewFox.prefs.getBoolPref("enabled")){
  86.                     chromeWin.document.getElementById('iViewFoxStatus').src="chrome://iViewFox/content/abc_logo2.png";
  87.                 }
  88.  
  89.                 gB.tabContainer.addEventListener("TabSelect", function(){
  90.                     if(gB.selectedBrowser.contentWindow.location.toString().match("http://www.abc.net.au/iview/")){
  91.                         chromeWin.document.getElementById('iViewFoxStatus').setAttribute('hidden','false');
  92.                     }
  93.                     else{
  94.                         chromeWin.document.getElementById('iViewFoxStatus').setAttribute('hidden','true');
  95.                     }
  96.                 }, false);
  97.  
  98.                 chromeWin.document.getElementById('iViewFoxStatus').addEventListener('click', function(e){
  99.                         chromeWin.openDialog('chrome://iViewFox/content/iViewFoxoptions.xul'); 
  100.                 },false);
  101.  
  102.                 let dirService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
  103.  
  104.                 let pathAndExeText='/extensions/[email protected]/rtmpdump/';
  105.                 if(iViewFox.osString=="WINNT"){
  106.                     pathAndExeText='\\extensions\\[email protected]\\rtmpdump\\rtmpdump.exe';
  107.                 }
  108.                 iViewFox.getrtDump = dirService.get("ProfD", Ci.nsIFile).path+pathAndExeText;
  109.  
  110.                 if(iViewFox.osString=="Darwin" && !iViewFox.extItem){
  111.                     try {
  112.                         // Firefox 4 and later; Mozilla 2 and later
  113.                         Components.utils.import("resource://gre/modules/AddonManager.jsm");
  114.                         AddonManager.getAddonByID("[email protected]", function(aAddon){iViewFox.stuffForOSX(aAddon);});
  115.                     }
  116.                     catch (ex) {
  117.                         // Firefox 3.6 and before; Mozilla 1.9.2 and before
  118.                             let pre4ID = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager).getItemForID("[email protected]");
  119.                             iViewFox.stuffForOSX(pre4ID);              
  120.                     }
  121.                 }
  122.                 iViewFox.init(gB,chromeWin);
  123.             }
  124.                    
  125.         },     
  126.         init:function(gB,chromeWin){
  127.  
  128.             gB.addEventListener("DOMContentLoaded", function(event){
  129.  
  130.                 var evOrTar=event.originalTarget;
  131.  
  132.                 if(evOrTar instanceof chromeWin.HTMLDocument){
  133.  
  134.                     if(evOrTar.location && evOrTar.location.toString().match("http://www.abc.net.au/iview/")){ 
  135.  
  136.                         iViewFox.iVtabDoc=evOrTar;
  137.                         if(!iViewFox.uA){
  138.                             iViewFox.uA=gB.contentWindow.navigator.userAgent;
  139.                         }
  140.                         if(gB.selectedBrowser.contentDocument.location.href==evOrTar.location.href){   
  141.                             chromeWin.document.getElementById('iViewFoxStatus').setAttribute('hidden','false');
  142.                         }
  143.                         gB.contentWindow.addEventListener('beforeunload',iViewFox.hideIViewFoxStatusBeforeUnload,false);   
  144.  
  145.                         var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
  146.                         req.onload = function(data){
  147.                             var rT = req.responseText;
  148.                             iViewFox.seriesIndex=JSON.parse(rT);
  149.                         };
  150.                         req.onerror = function(err){
  151.                             iViewFox.log(err);
  152.                         };
  153.                         req.open("GET", 'http://tviview.abc.net.au/iview/api2/?seriesIndex', true);
  154.                         req.send(null);
  155.  
  156.                         gB.contentWindow.addEventListener('hashchange',function(e){
  157.                             var tL = this.location;
  158.                             if(tL.href.match(iViewFox.hashReg) && iViewFox.prefs.getBoolPref("enabled")){
  159.                                 var locSplit=tL.href.split('#/view/');
  160.                                 iViewFox.findEpDetails(locSplit[1],chromeWin);
  161.                                 //https://developer.mozilla.org/en/nsITimer
  162.                                 //have to add a small delay here otherwise flash sometimes doesn't seem to notice the hashchange if it's too quick
  163.                                 let event = {
  164.                                     notify: function(timer) {
  165.                                         tL.href=locSplit[0]+'#';
  166.                                     }
  167.                                 };
  168.                                 let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  169.                                 let secs=2000;
  170.                                 if(locSplit[1].match(/587926|597007/)){
  171.                                     secs=5000;
  172.                                 }
  173.                                 timer.initWithCallback(event,secs, Ci.nsITimer.TYPE_ONE_SHOT);                     
  174.                             }
  175.                         },false);
  176.  
  177.                     }
  178.  
  179.                 }
  180.             }, false);
  181.                            
  182.         },
  183.         findEpDetails:function(epNumber,chromeWin){
  184.             iViewFox.seriesIndex.forEach(function(sitem, sindex, sarray){
  185.                 var seriesNum = sitem.a;
  186.                 sitem.f.forEach(function(item, index, array){
  187.                     if(item.a==epNumber){
  188.                         var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
  189.                         req.onload = function(data){
  190.                             var rT = req.responseText;
  191.                             var jPar=JSON.parse(rT);
  192.                             if(jPar[0].f.length){
  193.                                 jPar[0].f.forEach(function(jfitem, jfindex, jfarray){
  194.                                     if(jfitem.a == epNumber){
  195.                                         iViewFox.handshake(jfitem.n, seriesNum, jfitem.b,chromeWin,jPar[0].b);                             
  196.                                     }
  197.                                 });
  198.                             }
  199.                         };
  200.                         req.onerror = function(err){
  201.                             iViewFox.log(err);
  202.                         };
  203.                         req.open("GET", 'http://tviview.abc.net.au/iview/api2/?series='+seriesNum, true);
  204.                         req.send(null);                        
  205.                     }
  206.                 });
  207.             });
  208.    
  209.         },
  210.         handshake:function(epDS, seriesNum, epName,chromeWin,seriesTitle){
  211.             var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
  212.             req.onload = function(data){
  213.                 var rT = req.responseText;
  214.                 var x= new chromeWin.gBrowser.contentWindow.DOMParser().parseFromString(rT, "text/xml");
  215.                 var xStore={};
  216.                 [].forEach.call(x.querySelectorAll('*'),function(item,index,array){xStore[item.nodeName]=item.textContent;});   //may as well store it all         
  217.                 iViewFox.grabEpisode(epDS, xStore, seriesNum, epName,chromeWin,seriesTitle);               
  218.             };
  219.             req.onerror = function(err){
  220.                 iViewFox.log(err);
  221.             };
  222.             req.open("GET", 'http://tviview.abc.net.au/iview/auth/?v2', true);
  223.             req.send(null);            
  224.         },
  225.         parseCaptionsXML:function(f, x){
  226.             var data="";
  227.             [].forEach.call(x.querySelectorAll('title'), function (element, index, array) {
  228.                 data+=(index+1)+"\n"+(element.getAttribute('start')+"0 --> "+
  229.                     element.getAttribute('end')+"0\n").replace(/((?:\d{2}:){2}\d{2}):(\d{3})/g, "$1,$2")+
  230.                         element.textContent.replace(/\|/g,'\n')+"\n\n";
  231.             });
  232.             iViewFox.writeTextFile(f, data);       
  233.         },
  234.         writeTextFile:function(fyle, data){
  235.             var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
  236.        
  237.             foStream.init(fyle, 0x02 | 0x08 | 0x20, 0666, 0);
  238.             var converter = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(Ci.nsIConverterOutputStream);
  239.             converter.init(foStream, "UTF-8", 0, 0);   
  240.        
  241.             converter.writeString(data);
  242.             converter.close();         
  243.         },
  244.         grabEpisode:function(epDS, xStore, seriesNum, epName,chromeWin,seriesTitle){
  245.  
  246.             var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  247.  
  248.             var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
  249.             fp.init(chromeWin, "Save To...", Ci.nsIFilePicker.modeSave);
  250.             /*var defString=epDS;
  251.             if(defString.indexOf('/')>-1){
  252.                 defString=defString.slice(defString.lastIndexOf('/')+1);
  253.             }   */     
  254.             var fTypeSplit = epDS.split('.');          
  255.             var serCheq=(seriesNum=='2932730');
  256.             var savedEpFileName=epName.replace(/[^a-zA-Z0-9]/g," ")+'.'+fTypeSplit[1];
  257.             if(serCheq){
  258.                 savedEpFileName=epName.replace(/[^a-zA-Z0-9]/g," ")+' '+new Date().toString()+'.'+fTypeSplit[1];
  259.             }
  260.             fp.defaultString=seriesTitle+' '+savedEpFileName;
  261.             if(serCheq){ //if 24 hour news stream
  262.                     var params = {time:'30', quality:'news24-hi@28773'};
  263.                     if(epName.match('Medium Bandwidth')){
  264.                         params.quality='news24-med@28772';
  265.                     }
  266.                     chromeWin.openDialog("chrome://iViewFox/content/24streamchoice.xul", "", "chrome, dialog, modal, resizable=yes", params).focus();
  267.             }
  268.             var rv = fp.show();
  269.             if (rv == Ci.nsIFilePicker.returnOK || rv == Ci.nsIFilePicker.returnReplace) {
  270.                 var filePathAndName = fp.file.path;
  271.                 //http://rtmpdump.mplayerhq.hu/rtmpdump.1.html
  272.                 var args = [];
  273.                 var rTimeOut = iViewFox.prefs.getIntPref('rTimeOut');
  274.                 if(serCheq){    //if 24 hour news stream
  275.                     args = ["-r",
  276.                             "rtmp://cp81899.live.edgefcs.net/",
  277.                             "-y",
  278.                             params.quality,
  279.                             "-a",
  280.                             "live/"+params.quality+"?auth="+xStore.token,
  281.                             "-W",
  282.                             "http://www.abc.net.au/iview/images/iview.jpg",
  283.                             "-o",
  284.                             filePathAndName,
  285.                             "-v",
  286.                             "-B",
  287.                             (Number(params.time)*60).toString(),
  288.                             "-m",   //timeout number
  289.                             rTimeOut];                 
  290.                 }
  291.                 else{
  292.                     args = ['-r',
  293.                         'rtmp://cp53909.edgefcs.net////flash/playback/_definst_/'+epDS,
  294.                         '-a',
  295.                         'ondemand?auth='+xStore.token,
  296.                         '-o',
  297.                         filePathAndName,
  298.                         "-m",   //timeout number
  299.                         rTimeOut,
  300.                         '-W',
  301.                         'http://www.abc.net.au/iview/images/iview.jpg'];
  302.                     if(xStore.host=='Hostworks'){
  303.                         var fileEx;
  304.                         if(fTypeSplit[1]=='mp4'){
  305.                             fileEx='mp4:';
  306.                         }
  307.                         else{
  308.                             fileEx='flv:';
  309.                         }
  310.                         args = ['-r',
  311.                             'rtmp://203.18.195.10/',
  312.                                 '-a',
  313.                             'ondemand?auth='+xStore.token,
  314.                             '-y',
  315.                             fileEx+fTypeSplit[0],
  316.                             '-o',
  317.                             filePathAndName,
  318.                             "-m",   //timeout number
  319.                             rTimeOut,
  320.                             '-W',
  321.                             'http://www.abc.net.au/iview/images/iview.jpg'];           
  322.                     }              
  323.                 }
  324.                 if(iViewFox.prefs.getBoolPref("debugMode")){
  325.                     args.push('-z');
  326.                 }
  327.                 iViewFox.log(args);
  328.                 if(iViewFox.prefs.getBoolPref("downloadSubtitles")){
  329.                     var subFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  330.                     var fName = fp.file.path;
  331.                     if(fName.match('.')){
  332.                         fName=fName.slice(0,fName.lastIndexOf('.'));
  333.                     }
  334.                     subFile.initWithPath(fName+'.srt');
  335.                     var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
  336.                     req.onreadystatechange = function (aEvt) {
  337.                         if (req.readyState == 4) {
  338.                             if(req.status == 200){
  339.                                 var subbersXML=new chromeWin.gBrowser.contentWindow.DOMParser().parseFromString(req.responseText, "text/xml");
  340.                                 iViewFox.parseCaptionsXML(subFile,subbersXML);
  341.                             }
  342.                             else{
  343.                                 iViewFox.log("No Captions Available");
  344.                             }
  345.                         }
  346.                     };                 
  347.                     req.onerror = function(err){
  348.                         iViewFox.log(err);
  349.                     };
  350.                     req.open("GET", 'http://www.abc.net.au/iview/captions/'+fTypeSplit[0]+'.xml', true);
  351.                     req.send(null);                
  352.                 }              
  353.                    
  354.                 if(iViewFox.osString==="Darwin"){
  355.                     let fileCC = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);  
  356.                     fileCC.initWithPath(iViewFox.getrtDump+'b.command');
  357.                     var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
  358.                
  359.                     foStream.init(fileCC, 0x02 | 0x08 | 0x20, 0666, 0);
  360.                     var converter = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(Ci.nsIConverterOutputStream);
  361.                     converter.init(foStream, "UTF-8", 0, 0);
  362.                
  363.                     var data='#!/bin/bash\n"'+iViewFox.getrtDump+'rtmpdump" ';
  364.                     /*if(iViewFox.uA.match('PPC Mac')){
  365.                         data='#!/bin/bash\n"'+iViewFox.getrtDump+'rtmpdump-OSX-PPC" ';
  366.                     }*/
  367.                     args.forEach(function(item,index,array){
  368.                         if(index%2){
  369.                             data+=' "'+item+'" ';
  370.                         }
  371.                         else{
  372.                             data+=item;
  373.                         }
  374.                     });
  375.                     converter.writeString(data);
  376.                     converter.close();
  377.                     fileCC.launch();
  378.                 }
  379.                 else{
  380.                     let fileCC = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);  
  381.                     fileCC.initWithPath(iViewFox.getrtDump);
  382.                     process.init(fileCC);
  383.                     process.run(false, args, args.length);     
  384.                 }      
  385.             }
  386.  
  387.         },
  388.         iViewFoxStreamChoiceonOK:function(w) {
  389.            w.arguments[0].time=w.document.getElementById("time").value;
  390.            //w.arguments[0].quality=document.querySelector("radio[selected='true']").id;
  391.            return true;
  392.         },
  393.         saveiVPref:function(d){
  394.  
  395.             var dip2=d.querySelector('#enableIVF');
  396.  
  397.             let cwin = iViewFox.currentWindow();   
  398.             if(dip2.checked){
  399.                 cwin.document.getElementById('iViewFoxStatus').src="chrome://iViewFox/content/abc_logo2-off.png";
  400.             }
  401.             else{
  402.                 cwin.document.getElementById('iViewFoxStatus').src="chrome://iViewFox/content/abc_logo2.png";
  403.             }
  404.         }
  405. };
Advertisement
Add Comment
Please, Sign In to add comment