Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FireFox Addon: How to override native js function

I am trying to run a script on page load using pageMod: but I could not see its effect

var data = require("sdk/self").data;
var attachTo = require("sdk/content/mod").attachTo;
var pageMod = require("sdk/page-mod");

pageMod.PageMod({
  include: "*",
   contentScriptWhen: "start",
  allow:true,
   attachTo: ["existing", "top"],
  contentScriptFile: [data.url("jquery-2.1.1.min.js"),
                     data.url("somejs.js")],


})

Inside my somejs.js file, I have overriden form submit function:

document.forms['frmMain'].submit=function submit(){alert("Submitting")... 
...do some stuff  

};

On my web page there is a button which submits the form:

    frmMain.method="post"
    frmMain.action = "someurl";
    frmMain.submit();   

But When I click button it does not call the overriden method I defined above. When I override the function using firebug console It works! SO what does firebug do to run the command so that I can do same in my addon code to achieve same thing.

like image 941
kashif Avatar asked Jul 20 '14 12:07

kashif


1 Answers

Per Interacting with page scripts you'll

  1. need use the unsafeWindow so that changes are reflected in the content script and
  2. need to properly export a function from the content script to be callable from the page script.

The following works for me:

function submit() {
  console.log("Submitting");
  alert("Submitting");
};
unsafeWindow.document.forms['frmMain'].submit =
  exportFunction(submit, unsafeWindow);
like image 72
nmaier Avatar answered Nov 19 '22 08:11

nmaier