content script access to webpage js [duplicate]

拈花ヽ惹草 提交于 2019-12-13 03:42:36

问题


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();

回答1:


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.



来源:https://stackoverflow.com/questions/8844633/content-script-access-to-webpage-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!