antimony

drive firefox from node.js
git clone https://wehaveforgeathome.hates.computer/antimony.git
Log | Files | Refs | Submodules | LICENSE

content.js (2219B)


      1 Components.utils.import('resource://antimony/common.js');
      2 require.path = [
      3   'resource://antimony',
      4   'file:///home/ryan/repos/borderstylo/curatron/lib'
      5 ];
      6 
      7 var $ = require('windex');
      8 
      9 var Poller = function (url, handler) {
     10   this.xhr = null;
     11   this.url = url;
     12   this.handler = handler;
     13   this.connect();
     14 };
     15 
     16 Poller.prototype.connect = function () {
     17   var that = this,
     18       handler = this.handler,
     19       xhr = this.xhr = new XMLHttpRequest();
     20   xhr.open("GET", this.url, true);
     21   xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
     22   xhr.onreadystatechange = function (event) {
     23     if (xhr.readyState >= 1 && xhr.readyState <= 3) { return; }
     24     if (xhr.readyState === 0) { return that.restart(); }
     25     if (xhr.status !== 200) { return that.restart(); }
     26     var json = xhr.responseText;
     27     that.restart();
     28     handler(json);
     29   };
     30   xhr.setRequestHeader("Accept", "application/json");
     31   xhr.send(null);
     32 };
     33 
     34 Poller.prototype.restart = function () {
     35   this.xhr.abort();
     36   this.connect();
     37 };
     38 
     39 var runInBrowser = function (script, callback) {
     40   eval('var f = ' + script);
     41   f(callback);
     42 };
     43 
     44 var runOnPage = function (url, script, callback) {
     45   var tab = gBrowser.selectedTab = gBrowser.addTab(url);
     46   var browser = gBrowser.getBrowserForTab(tab);
     47   var chwin = window;
     48   chwin.addEventListener('DOMContentLoaded', function (e) {
     49     var document = browser.contentDocument.wrappedJSObject;
     50     if (!document || e.originalTarget != document) { return; }
     51     chwin.removeEventListener('DOMContentLoaded', arguments.callee, false);
     52     var window = document.defaultView;
     53     eval('var f = ' + script);
     54     f(function (data) {
     55       callback(data);
     56       gBrowser.removeTab(tab);
     57     });
     58   }, false);
     59 };
     60 
     61 new Poller('http://localhost:1234/', function (json) {
     62   var message = JSON.parse(json);
     63   var callback = function (data) {
     64     var xhr = new XMLHttpRequest();
     65     xhr.open("POST", 'http://localhost:1235/browser/' + message.id, true);
     66     xhr.send(JSON.stringify({res: data}));
     67   };
     68   try {
     69     if (message.type == 'page') {
     70       runOnPage(message.url, message.script, callback);
     71     }
     72     else {
     73       runInBrowser(message.script, callback);
     74     }
     75   }
     76   catch (e) {
     77     Cu.reportError(e);
     78     callback(e);
     79   }
     80 });