Question

This question already has an answer here:

I create a firefox addon with the online addon builder. How can one use an existing function on a webpage, from within the addon? This site doesn't belong to me, so I can't bind event listeners, in order to send an event to it from my addon.

Update: Now I have this code but it doesn't work:

main.js

var widgets = require("widget");
var tabs = require("tabs");
var data = require("self").data;


var widget = widgets.Widget({
  id: "transfer",
  label: "Transfer",
  content: "Transfer",
  width: 100,
  onClick: function() {
    tabs.activeTab.attach({
      // native implementation of window.confirm will be used
      contentScriptFile: data.url("new.js")
    });
  }
});

new.js:

function foo()
{
    call(); // function in webpage

}
unsafeWindow.foo();
Was it helpful?

Solution

You inject a content script into the page (e.g. via page-mod package) and call unsafeWindow.foo() in the content script to call function foo() from the content script. See documentation for more information. Please make sure to read the warnings: you are relying on the web page function to behave the way you expect but it might not. In particular, if the function returns something and you want to process that result, you might inadvertently introduce a security hole.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top