从Firefox扩展访问文档的JavaScript变量 [英] Accessing document's javascript variable from firefox extension

查看:167
本文介绍了从Firefox扩展访问文档的JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firefox扩展(工具栏)可以访问文档的变量吗?详细解释如下。

加载文件:

 <脚本类型= 文本/ JavaScript的 > 
var variableForExtension ='something';
< / script>

扩展名:

  var win = window.top.getBrowser()。selectedBrowser.contentWindow; 
alert(win.variableForExtension); // undefined

这是第一次尝试,而且由于安全机制(<强> XPCNativeWrapper )。我已阅读通过 wrappedJSObject 并使用事件(将侦听器添加到文档和从扩展分派事件)访问它,但没有运气。虽然没有太努力。所以,在我深入挖掘之前('events method'听起来像是一种方法),我想知道这是甚至可能的吗?



谢谢

$是的,在内容中访问一个JS变量总是可能的。 1)如果你控制的是网络,那么你可以使用一个简单的方法来实现这个功能。页面,并希望将信息传递给扩展,您应该确实使用事件技术<一>。 2)如果你想从内容文档中读取一个值,你可以绕过XPCNativeWrapper:

p>

  var win = window.top.getBrowser()。selectedBrowser.contentWindow; 
//顺便说一句,这可能只是
// var win = content;
//或
// var win = gBrowser.contentWindow;
alert(win.variableForExtension); //未定义
win.wrappedJSObject.variableForExtension //瞧!

这在Firefox 3之前是不安全的。在Firefox 3及更高版本中,另一种包装( XPCSafeJSObjectWrapper ),它看起来与内容页面中的对象相同3)如果您需要在内容网页中调用某个函数或者运行你自己的代码在页面的上下文中,这更复杂。有人多次询问和回答,但不幸的是没有完整记录。由于这与您的问题无关,所以我不会详细讨论。


is it possible for Firefox extension (toolbar) to access document's variables? detailed explanation follows..

loaded document:

<script type="text/javascript">
var variableForExtension = 'something';
</script>

extension:

var win = window.top.getBrowser().selectedBrowser.contentWindow;
alert(win.variableForExtension); // undefined

it was first thing to try, and it's inaccessible this way because of security mechanisms (XPCNativeWrapper). i've read about accessing it trough wrappedJSObject and using events (adding listener to document and dispatching event from extension), but no luck. didn't try too hard, though. so, before i dig deeper ('events method' sounds like a way to go) i'd like to know is this even possible?

thanks

解决方案

Yes, accessing a JS variable in content is and always was possible. Doing this the naive way wasn't safe (in the sense that a malicious web page could get chrome privileges) in older Firefox versions.

1) If you control the web page and want to pass information to the extension, you should indeed use the events technique. This worked and was/is safe in all Firefox versions.

2) If you want to read a value from the content document, you can just bypass the XPCNativeWrapper:

var win = window.top.getBrowser().selectedBrowser.contentWindow;
// By the way, this could just be
//   var win = content;
// or 
//   var win = gBrowser.contentWindow;
alert(win.variableForExtension); // undefined
win.wrappedJSObject.variableForExtension // voila!

This was unsafe prior to Firefox 3. In Firefox 3 and later it is OK to use, you get another kind of wrapper (XPCSafeJSObjectWrapper), which looks the same as the object from the content page to your code, but ensures the content page won't be able to do anything malicious.

3) If you need to call a function in a content web page or run your own code in the page's context, it's more complicated. It was asked and answered elsewhere many times, but unfortunately never documented fully. Since this is unrelated to your question, I won't go into the details.

这篇关于从Firefox扩展访问文档的JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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