Firefox addon: how to override js built-in function

I am trying to run a script while loading a page 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 a form submit function:

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

There is a button on my webpage that represents the form:

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

But when I click the button, it does not call the override method described above. When I redefine a function using firebug console, it works! SO what does firebug do to run the command so that I can do the same in my add-on code to achieve the same.

+6
source share
1 answer

Per interaction with page scripts you will

  • you need to use unsafeWindow so that the changes are reflected in the contents of the script and
  • You must correctly export the function from the contents of the script to be called from the script page.

The following works for me:

 function submit() { console.log("Submitting"); alert("Submitting"); }; unsafeWindow.document.forms['frmMain'].submit = exportFunction(submit, unsafeWindow); 
+3
source

Source: https://habr.com/ru/post/972530/


All Articles