内容脚本访问网页js [英] content script access to webpage js

查看:130
本文介绍了内容脚本访问网页js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用在线插件生成器创建了一个Firefox插件。如何从插件中使用网页上的现有功能?这个网站不属于我,所以我不能绑定事件监听器,以便从我的插件发送一个事件。



更新:现在我有这段代码,但它不起作用:

main.js

$ p $ var widgets = require(widget);
var tabs = require(tabs);
var data = require(self)。data;

$ b var widget = widgets.Widget({
id:transfer,
label:Transfer,
content:Transfer,
width:100,
onClick:function(){
tabs.activeTab.attach({
//将使用window.confirm的本地实现
contentScriptFile: data.url(new.js)
});
}
});

new.js:

  function foo()
{
call(); //在网页中的功能

}
unsafeWindow.foo();


解决方案

page-mod 包)并在内容脚本中调用 unsafeWindow.foo()来调用函数 foo()从内容脚本。请参阅文档了解更多信息。请确保阅读警告:您正在依靠网页功能按照您期望的方式行事,但可能不会。特别是,如果函数返回了某个内容,并且您想要处理该结果,那么您可能会无意中引入了一个安全漏洞。


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

解决方案

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.

这篇关于内容脚本访问网页js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆