javascript - Replace complete HTML by mozilla addon SDK -


is there way replace complete html, , add additional javascript files mozilla addon sdk?

with chrome can done running script @ "document_start", stop window , replace complete html xhr response. don't understand why complicated, can live that. far understand addon sdk has page-mod module, "running scripts in context of web pages url matches given pattern." in theory should done page-mod module, did not find example overrides whole html. in theory window.stop , replacing complete html should work here too, not able access addon (backend) files context of webpage (frontend). chrome done via "web_accessible_resources" in manifest , chrome.extension.geturl. firefox cannot use related addon sdk frontend, self.data.url not work...

i tried send file contents way (since addon file access denied firefox frontend):

index.js

var pagemod = require("sdk/page-mod"); var data = require("sdk/self").data;  pagemod.pagemod({     include: ["http://example.com/", "http://example.com/index.html"],     contentscriptfile: "./inject.js",     contentscriptwhen: "start",     onattach: function (worker){         console.log("injector attached");         worker.port.emit("inject", {             "index.html": data.load("client/index.html"),             "main.js": data.load("client/main.js")         });     } }); 

inject.js

!function () {     self.port.on("inject", function (files) {         console.log("injector trigger");         if (typeof hasrun == 'undefined') {             console.log("injecting");             hasrun = true;             window.stop();             var html = files["index.html"].replace("<script src=\"./main.js\"></script>", "<script>"+files["main.js"]+"</script>");             document.documentelement.innerhtml = html;         }     }); }(); 

if replace html hello world, works. html appears invalid, got no error message , console shows empty html skeleton (i got empty page). same index.html , main.js code works chrome plugin, want use in firefox. thing chrome plugin, js file cancelled out, surely not loaded before stopping window. tried same, maybe did not work, don't know.

index.js

let { ci, cu } = require('chrome');  cu.import("resource://gre/modules/services.jsm"); cu.import("resource://gre/modules/xpcomutils.jsm");  var observer = {     queryinterface: xpcomutils.generateqi([         ci.nsiobserver,         ci.nsisupportsweakreference     ]),      observe: function (subject, topic, data) {         if (topic == "http-on-opening-request" &&             subject instanceof ci.nsihttpchannel) {             var uri = subject.uri;             if (uri.host == "example.com" && /some\.js/.test(uri.path))                 subject.cancel();         }     } };  services.obs.addobserver(observer, "http-on-opening-request", true); 

the subject.cancel() runs some.js file. if add logging:

            console.log("cancelling", uri.path);             subject.cancel();             console.log("cancelled"); 

then "cancelled" not appear in console, "cancelling, /some.js". don't know whether normal, got no error message.

by trying regular html webpage

<!doctype html> <html> <head>     <meta charset="utf-8"> </head> <body>     <script>     //<![cdata[         !function (){             window.stop();             var html = '<!doctype html>\n<html>\n<head>\n    <meta charset="utf-8">\n</head>\n<body>\n  <script>console.log("loaded");</script>\ntext\n</body>\n</html>';             document.documentelement.innerhtml = html;         }();     //]]>     </script> </body> </html> 

i got unterminated string literal syntax error, funnier. guess somehow injector code not work if contains javascript, not addon related issue think. if use <\/script> okay, console.log("loaded"); script not run. chrome run, problem think.

i added jquery "contentscriptfile" , used $("html").html(html) instead document.documentelement.innerhtml = html. runs scripts now, still not work properly.

added fix cancel():

let { ci, cu, cr } = require('chrome'); //... subject.cancel(cr.ns_binding_aborted); 

cancel appears have required status parameter, silently fails when not it. cancelling files works too, plugin still fails somewhere. :s

i think injector wrote works perfectly, , problem injected html , js files. don't intend debug them (since $.load uses eval, hard), sent code chrome plugin developers, maybe can fix somehow.


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -