如何创建在远程页面中定义的类的对象? [英] How can I create an object of a class which is defined in the remote page?

查看:39
本文介绍了如何创建在远程页面中定义的类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在远程网页中,有如下代码片段:

For example, in the remote webpage, there is a snippet of code like this:

<script>
function foo(){
this.bar = 0;
}

在我的油脂猴子脚本中,我想创建一个此类的对象:

In my greasemonkey script, I want to create an object of this class:

var _foo= unsafeWindow['foo'];
new _foo();

然后我看到一个非法值错误.

Then I got an Illegal Value error.

推荐答案

方法如下:

var _foo = eval('(' + unsafeWindow.foo.toSource() + ')');
var x = new _foo();

尽管我不确定,由于Greasemonkey的安全区域或沙箱不同,可能需要此解决方法.

This workaround may be required due to the different security zones or sandboxing that Greasemonkey does, though I'm not entirely sure.

这篇关于如何创建在远程页面中定义的类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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