我们正在为 Firefox 使用自定义的 Javascript-only 附加组件,它被用于我们的一些内部网站。这个插件应该从用户的 PC 加载一个特定的文本文件,然后将特定的变量暴露给我们的一些内部网页。
当前的实现适用于 FF3 到 FF28。在 FF29 中,wrappedJSObject 的行为发生了变化。
这是我在插件代码中得到的:
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
function PrivateClass() {
this.wrappedJSObject = this;
}
PrivateClass.prototype = {
// Component details
classDescription: "...",
classID: Components.ID("{...}"),
contractID: "@foo.bar/PrivateClass;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsIClassInfo]),
getInterfaces: function(countRef) {
var interfaces = [Ci.nsIClassInfo, Ci.nsISupports];
countRef.value = interfaces.length;
return interfaces;
},
implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
flags: Ci.nsIClassInfo.DOM_OBJECT,
getHelperForLanguage: function(count) { return null; },
// We use the default _xpcom_factory
// Categories to register
_xpcom_categories: [{
category: "JavaScript global property",
entry: "PrivateClass", // optional, defaults to the object's classDescription. Needed for FF3.
value: "@foo.bar/PrivateClass;1", // optional, defaults to the object's contractID. Needed for FF3.
service: false
}],
// nsISecurityCheckedComponent permissions
// return "AllAccess"; / return "NoAccess";
canCreateWrapper : function canCreateWrapper(aIID) { return "AllAccess"; },
canCallMethod: function canCallMethod(aIID, methodName) { return "AllAccess"; },
canGetProperty: function canGetProperty(aIID, propertyName) { return "AllAccess"; }, // needed to access wrappedJSObject
canSetProperty: function canSetProperty(aIID, propertyName) { return "NoAccess"; },
getFunctionA : function() { return "This is A"; },
getFunctionB : function() { return "This is B"; },
// New functionality, needed for FF 17+
// https://developer.mozilla.org/en-US/docs/XPConnect_wrappers#__exposedProps__
__exposedProps__ : { getFunctionA : "r", getFunctionB : "r" }
}
在客户端页面:
if (typeof PrivateClass != "undefined") {
var obj = PrivateClass.wrappedJSObject;
var a = obj.getFunctionA()
var b = obj.getFunctionB();
}
但是,现在 FF 在这一行返回 Error: Attempt to use .wrappedJSObject in untrusted code
:
var obj = PrivateClass.wrappedJSObject;
我在这篇博文中读到了 FF30 中 wrappedJSObject 即将发生的变化: https://blog.mozilla.org/addons/2014/04/10/changes-to-unsafewindow-for-the-add-on-sdk/
...但是推荐的解决方案对我不起作用
请注意,wrappedJSObject 解决方案在官方 Mozilla 开发人员文档中 here和 here .它也可以在整个 Internet 上找到(例如 here ),但显然它在 FF29 中已被破坏/更改,因此这些都不适用。
我检查了出色的 FF 附加组件 jsPrintSetup ,并且它不使用 wrappedJSObject(尽管它也有一个 C++ 二进制组件,这可能解释了这一点)。我已经阅读了很多关于 wrappedJSObject 的这个问题的论坛帖子,但是我找不到关于它的行为的这个特定变化的任何信息。谁能阐明需要做什么才能使此功能在 FF29+ 下运行?
最佳答案
是的,我添加了一个针对不受信任内容的检查来检查 XPCOM 组件的内部结构,这实际上与它无关。它在特权代码中应该仍然可以正常工作。
Also note that nsISecurityCheckedComponent was removed ,因此您不再需要该部分。
一般来说,将 API 暴露给内容的最具前瞻性的方法是使用 exportFunction 将其直接注入(inject)内容范围。
关于javascript - Firefox 29、XPCOM 和 wrappedJSObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23835434/