从 Firefox 36 开始,Function.__exposedProps__
已不可用。相反,如果想要公开要在内容脚本中使用的 chrome JS 对象,则必须将 Components.utils.cloneInto
与目标一起使用范围为 browser.contentWindow.wrappedJSObject 。
如果不打开 cloneFunctions
标志,则仅克隆那些不是函数的属性。转动该标志也会克隆函数,但不会克隆那些通过 Function.prototype
路径定义的函数。对于这些函数,必须通过 Components.utils.exportTo
导出它们,并将目标范围作为公开的对象。
说到我面临的问题。 (由于我无法用语言表达,所以我添加了 MWE)。
Chrome 端 JS:
function Foo(){
this._nFunc = "something";
this._func = function(){/*do something*/};
}
Foo.prototype.Bar = function(){
this._func();
}
Foo.prototype.FooBar = function(){
this._nFunc = "somthing else";
}
var myFoo = new Foo();
var targetScope = browser.contentWindow.wrappedJSObject;
targetScope.myExposedObject = Components.utils.cloneInto(myFoo, targetScope, {cloneFunctions:true});
Components.utils.exportFunction(myFoo.Bar, targetScope.myExposedObject , {defineAs:"Bar"});
Components.utils.exportFunction(myFoo.FooBar, targetScope.myExposedObject , {defineAs:"FooBar"});
内容端JS:
window.myExposedObject.FooBar(); // works
window.myExposedObject._func(); // works
window.myExposedObject.Bar() // error this._func is undefined
记录函数 Bar()
接收到的 this
作用域后,我们得到 _func:(void 0)
,而 _nFunc
已正确记录。
问题:
- 我是否遗漏了什么,或者这是 Firefox 的限制?如果这是一个限制,请提出解决该限制的可能方法。
- 最初我认为
Bar()
不知何故无法访问调用对象的范围,我尝试将范围作为参数提供给它,即Foo.prototype.Bar = function(scope){scope._func();}
和window.myExposedObject.Bar(window.myExposedObject);
。有趣的是,在记录时,范围对象也被证明是(void 0)
。这是为什么? 我确信我在这里遗漏了一些东西。我期望的是暴露的对象将映射到原始对象,并且在将暴露的对象作为参数发送后,chrome 端 JS 将能够获取原始对象。
最佳答案
虽然您尝试做的事情可能可以通过 cloneInto
/exportFunction
和放弃 xray 的正确组合来实现,但我建议您只需加载使用 subscript loader 将类层次结构直接放入目标上下文中并且在创建原型(prototype)后仅将最少量的特权函数挂接到原型(prototype)中。
这应该会减少攻击面并避免继承带来的麻烦。
此外,这些可能会很有用:
https://developer.mozilla.org/en-US/docs/Components.utils.createObjectIn https://developer.mozilla.org/en-US/docs/Components.utils.makeObjectPropsNormal
关于javascript - 火狐浏览器插件: function not available after juggling scopes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30331249/