Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var EXPORTED_SYMBOLS = ["iViewFox"];
- let Cc = Components.classes;
- let Ci = Components.interfaces;
- let Cu = Components.utils;
- var iViewFox = {
- log:function(msg){
- var lg = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
- lg.logStringMessage(msg);
- },
- currentWindow:function(){
- let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
- return wm.getMostRecentWindow('navigator:browser');
- },
- seriesIndex:null,
- hashReg:new RegExp(/http:\/\/www.abc.net.au\/iview\/#\/view\/[0-9]{6}/),
- uA:null,
- osString:Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS,
- prefs:Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("extensions.iViewFox."),
- extItem:null,
- getrtDump:null,
- hideIViewFoxStatusBeforeUnload:function(){
- let win=iViewFox.currentWindow();
- win.document.getElementById('iViewFoxStatus').setAttribute('hidden','true');
- win.gBrowser.contentWindow.removeEventListener('beforeunload',iViewFox.hideIViewFoxStatusBeforeUnload,false);
- },
- osxPermissionsFixer:function(){
- try {
- let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
- let args = [777, iViewFox.getrtDump+'b.command'];
- iViewFox.log(args.toSource());
- file.initWithPath("/bin/chmod");
- let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
- process.init(file);
- process.run(false, args, args.length, {});
- }
- catch(exc){
- Cu.reportError(exc);
- }
- },
- removeiViewFoxVersionData:{
- observe : function(aSubject, aTopic, aData) {
- let extID = aSubject.QueryInterface(Components.interfaces.nsIUpdateItem).id;
- if(extID =="[email protected]" && data =='item-uninstalled' && iViewFox.prefs.getCharPref('version')){
- iViewFox.prefs.clearUserPref('version');
- }
- },
- onUninstalling: function(addon) {
- iViewFox.prefs.clearUserPref('version');
- }
- }
- },
- stuffForOSX:function(aAddon){
- iViewFox.extItem=aAddon.version;
- if( iViewFox.extItem != iViewFox.prefs.getCharPref('version')){
- iViewFox.prefs.setCharPref("version", iViewFox.extItem);
- //this is technically file io, so best to wait a bit and not add to the startup time
- let timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
- timer.initWithCallback({notify: iViewFox.osxPermissionsFixer}, 2000, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
- }
- try{
- AddonManager.addAddonListener(iViewFox.removeiViewFoxVersionData);
- }
- catch(e){
- let observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
- observerService.addObserver(iViewFox.removeiViewFoxVersionData, "em-action-requested", false);
- }
- },
- status:function(chromeWin){
- if(!iViewFox.getrtDump){
- let gB=chromeWin.gBrowser;
- if(!iViewFox.prefs.getBoolPref("enabled")){
- chromeWin.document.getElementById('iViewFoxStatus').src="chrome://iViewFox/content/abc_logo2.png";
- }
- gB.tabContainer.addEventListener("TabSelect", function(){
- if(gB.selectedBrowser.contentWindow.location.toString().match("http://www.abc.net.au/iview/")){
- chromeWin.document.getElementById('iViewFoxStatus').setAttribute('hidden','false');
- }
- else{
- chromeWin.document.getElementById('iViewFoxStatus').setAttribute('hidden','true');
- }
- }, false);
- chromeWin.document.getElementById('iViewFoxStatus').addEventListener('click', function(e){
- chromeWin.openDialog('chrome://iViewFox/content/iViewFoxoptions.xul');
- },false);
- let dirService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
- if(iViewFox.osString=="WINNT"){
- }
- iViewFox.getrtDump = dirService.get("ProfD", Ci.nsIFile).path+pathAndExeText;
- if(iViewFox.osString=="Darwin" && !iViewFox.extItem){
- try {
- // Firefox 4 and later; Mozilla 2 and later
- Components.utils.import("resource://gre/modules/AddonManager.jsm");
- }
- catch (ex) {
- // Firefox 3.6 and before; Mozilla 1.9.2 and before
- let pre4ID = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager).getItemForID("[email protected]");
- iViewFox.stuffForOSX(pre4ID);
- }
- }
- iViewFox.init(gB,chromeWin);
- }
- },
- init:function(gB,chromeWin){
- gB.addEventListener("DOMContentLoaded", function(event){
- var evOrTar=event.originalTarget;
- if(evOrTar instanceof chromeWin.HTMLDocument){
- if(evOrTar.location && evOrTar.location.toString().match("http://www.abc.net.au/iview/")){
- iViewFox.iVtabDoc=evOrTar;
- if(!iViewFox.uA){
- iViewFox.uA=gB.contentWindow.navigator.userAgent;
- }
- if(gB.selectedBrowser.contentDocument.location.href==evOrTar.location.href){
- chromeWin.document.getElementById('iViewFoxStatus').setAttribute('hidden','false');
- }
- gB.contentWindow.addEventListener('beforeunload',iViewFox.hideIViewFoxStatusBeforeUnload,false);
- var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
- req.onload = function(data){
- var rT = req.responseText;
- iViewFox.seriesIndex=JSON.parse(rT);
- };
- req.onerror = function(err){
- iViewFox.log(err);
- };
- req.open("GET", 'http://tviview.abc.net.au/iview/api2/?seriesIndex', true);
- req.send(null);
- gB.contentWindow.addEventListener('hashchange',function(e){
- var tL = this.location;
- if(tL.href.match(iViewFox.hashReg) && iViewFox.prefs.getBoolPref("enabled")){
- var locSplit=tL.href.split('#/view/');
- iViewFox.findEpDetails(locSplit[1],chromeWin);
- //https://developer.mozilla.org/en/nsITimer
- //have to add a small delay here otherwise flash sometimes doesn't seem to notice the hashchange if it's too quick
- let event = {
- notify: function(timer) {
- tL.href=locSplit[0]+'#';
- }
- };
- let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
- let secs=2000;
- if(locSplit[1].match(/587926|597007/)){
- secs=5000;
- }
- timer.initWithCallback(event,secs, Ci.nsITimer.TYPE_ONE_SHOT);
- }
- },false);
- }
- }
- }, false);
- },
- findEpDetails:function(epNumber,chromeWin){
- iViewFox.seriesIndex.forEach(function(sitem, sindex, sarray){
- var seriesNum = sitem.a;
- sitem.f.forEach(function(item, index, array){
- if(item.a==epNumber){
- var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
- req.onload = function(data){
- var rT = req.responseText;
- var jPar=JSON.parse(rT);
- if(jPar[0].f.length){
- jPar[0].f.forEach(function(jfitem, jfindex, jfarray){
- if(jfitem.a == epNumber){
- iViewFox.handshake(jfitem.n, seriesNum, jfitem.b,chromeWin,jPar[0].b);
- }
- });
- }
- };
- req.onerror = function(err){
- iViewFox.log(err);
- };
- req.open("GET", 'http://tviview.abc.net.au/iview/api2/?series='+seriesNum, true);
- req.send(null);
- }
- });
- });
- },
- handshake:function(epDS, seriesNum, epName,chromeWin,seriesTitle){
- var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
- req.onload = function(data){
- var rT = req.responseText;
- var x= new chromeWin.gBrowser.contentWindow.DOMParser().parseFromString(rT, "text/xml");
- var xStore={};
- [].forEach.call(x.querySelectorAll('*'),function(item,index,array){xStore[item.nodeName]=item.textContent;}); //may as well store it all
- iViewFox.grabEpisode(epDS, xStore, seriesNum, epName,chromeWin,seriesTitle);
- };
- req.onerror = function(err){
- iViewFox.log(err);
- };
- req.open("GET", 'http://tviview.abc.net.au/iview/auth/?v2', true);
- req.send(null);
- },
- parseCaptionsXML:function(f, x){
- var data="";
- [].forEach.call(x.querySelectorAll('title'), function (element, index, array) {
- data+=(index+1)+"\n"+(element.getAttribute('start')+"0 --> "+
- element.getAttribute('end')+"0\n").replace(/((?:\d{2}:){2}\d{2}):(\d{3})/g, "$1,$2")+
- element.textContent.replace(/\|/g,'\n')+"\n\n";
- });
- iViewFox.writeTextFile(f, data);
- },
- writeTextFile:function(fyle, data){
- var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
- foStream.init(fyle, 0x02 | 0x08 | 0x20, 0666, 0);
- var converter = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(Ci.nsIConverterOutputStream);
- converter.init(foStream, "UTF-8", 0, 0);
- converter.writeString(data);
- converter.close();
- },
- grabEpisode:function(epDS, xStore, seriesNum, epName,chromeWin,seriesTitle){
- var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
- var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
- fp.init(chromeWin, "Save To...", Ci.nsIFilePicker.modeSave);
- /*var defString=epDS;
- if(defString.indexOf('/')>-1){
- defString=defString.slice(defString.lastIndexOf('/')+1);
- } */
- var fTypeSplit = epDS.split('.');
- var serCheq=(seriesNum=='2932730');
- var savedEpFileName=epName.replace(/[^a-zA-Z0-9]/g," ")+'.'+fTypeSplit[1];
- if(serCheq){
- savedEpFileName=epName.replace(/[^a-zA-Z0-9]/g," ")+' '+new Date().toString()+'.'+fTypeSplit[1];
- }
- fp.defaultString=seriesTitle+' '+savedEpFileName;
- if(serCheq){ //if 24 hour news stream
- var params = {time:'30', quality:'news24-hi@28773'};
- if(epName.match('Medium Bandwidth')){
- params.quality='news24-med@28772';
- }
- chromeWin.openDialog("chrome://iViewFox/content/24streamchoice.xul", "", "chrome, dialog, modal, resizable=yes", params).focus();
- }
- var rv = fp.show();
- if (rv == Ci.nsIFilePicker.returnOK || rv == Ci.nsIFilePicker.returnReplace) {
- var filePathAndName = fp.file.path;
- //http://rtmpdump.mplayerhq.hu/rtmpdump.1.html
- var args = [];
- var rTimeOut = iViewFox.prefs.getIntPref('rTimeOut');
- if(serCheq){ //if 24 hour news stream
- args = ["-r",
- "rtmp://cp81899.live.edgefcs.net/",
- "-y",
- params.quality,
- "-a",
- "live/"+params.quality+"?auth="+xStore.token,
- "-W",
- "http://www.abc.net.au/iview/images/iview.jpg",
- "-o",
- filePathAndName,
- "-v",
- "-B",
- (Number(params.time)*60).toString(),
- "-m", //timeout number
- rTimeOut];
- }
- else{
- args = ['-r',
- 'rtmp://cp53909.edgefcs.net////flash/playback/_definst_/'+epDS,
- '-a',
- 'ondemand?auth='+xStore.token,
- '-o',
- filePathAndName,
- "-m", //timeout number
- rTimeOut,
- '-W',
- 'http://www.abc.net.au/iview/images/iview.jpg'];
- if(xStore.host=='Hostworks'){
- var fileEx;
- if(fTypeSplit[1]=='mp4'){
- fileEx='mp4:';
- }
- else{
- fileEx='flv:';
- }
- args = ['-r',
- 'rtmp://203.18.195.10/',
- '-a',
- 'ondemand?auth='+xStore.token,
- '-y',
- fileEx+fTypeSplit[0],
- '-o',
- filePathAndName,
- "-m", //timeout number
- rTimeOut,
- '-W',
- 'http://www.abc.net.au/iview/images/iview.jpg'];
- }
- }
- if(iViewFox.prefs.getBoolPref("debugMode")){
- args.push('-z');
- }
- iViewFox.log(args);
- if(iViewFox.prefs.getBoolPref("downloadSubtitles")){
- var subFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
- var fName = fp.file.path;
- if(fName.match('.')){
- fName=fName.slice(0,fName.lastIndexOf('.'));
- }
- subFile.initWithPath(fName+'.srt');
- var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
- req.onreadystatechange = function (aEvt) {
- if (req.readyState == 4) {
- if(req.status == 200){
- var subbersXML=new chromeWin.gBrowser.contentWindow.DOMParser().parseFromString(req.responseText, "text/xml");
- iViewFox.parseCaptionsXML(subFile,subbersXML);
- }
- else{
- iViewFox.log("No Captions Available");
- }
- }
- };
- req.onerror = function(err){
- iViewFox.log(err);
- };
- req.open("GET", 'http://www.abc.net.au/iview/captions/'+fTypeSplit[0]+'.xml', true);
- req.send(null);
- }
- if(iViewFox.osString==="Darwin"){
- let fileCC = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
- fileCC.initWithPath(iViewFox.getrtDump+'b.command');
- var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
- foStream.init(fileCC, 0x02 | 0x08 | 0x20, 0666, 0);
- var converter = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(Ci.nsIConverterOutputStream);
- converter.init(foStream, "UTF-8", 0, 0);
- var data='#!/bin/bash\n"'+iViewFox.getrtDump+'rtmpdump" ';
- /*if(iViewFox.uA.match('PPC Mac')){
- data='#!/bin/bash\n"'+iViewFox.getrtDump+'rtmpdump-OSX-PPC" ';
- }*/
- args.forEach(function(item,index,array){
- if(index%2){
- data+=' "'+item+'" ';
- }
- else{
- data+=item;
- }
- });
- converter.writeString(data);
- converter.close();
- fileCC.launch();
- }
- else{
- let fileCC = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
- fileCC.initWithPath(iViewFox.getrtDump);
- process.init(fileCC);
- process.run(false, args, args.length);
- }
- }
- },
- iViewFoxStreamChoiceonOK:function(w) {
- w.arguments[0].time=w.document.getElementById("time").value;
- //w.arguments[0].quality=document.querySelector("radio[selected='true']").id;
- return true;
- },
- saveiVPref:function(d){
- var dip2=d.querySelector('#enableIVF');
- let cwin = iViewFox.currentWindow();
- if(dip2.checked){
- cwin.document.getElementById('iViewFoxStatus').src="chrome://iViewFox/content/abc_logo2-off.png";
- }
- else{
- cwin.document.getElementById('iViewFoxStatus').src="chrome://iViewFox/content/abc_logo2.png";
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment