您可以检查一个对象是否被 XPConnect (Firefox) 包装吗?
我的场景: 我正在迭代窗口对象并尝试仅检索用户定义的变量/函数并过滤掉本机浏览器对象。
for(var i in window) {
// Right now I just have a bunch of if checks on window[i]
}
我注意到本机浏览器对象/XPCOM 组件是通过 XPConnect 包装的,它返回对象的包装器,允许它与 Javascript 交互。我在想,如果我能以某种方式检查并查看该对象是否是包装器,那么我就可以将其过滤掉。 有没有办法检查对象是否通过 XPConnect 包装?我想过滤掉作为此处列出的任何包装器类型包装的所有对象: https://developer.mozilla.org/en/XPConnect_wrappers
My scenario:
I am iterating over the window object and trying to retrieve only user-defined variables/functions and filtering out native browser objects.
for(var i in window) {
// Right now I just have a bunch of if checks on window[i]
}
I noticed that native browser objects/XPCOM components are wrapped via XPConnect which returns a wrapper of the object that allows it to interface with Javascript. I am thinking that if I could somehow check and see if the object is a wrapper then I can filter it out.
Is there a way to check if an object is wrapped via XPConnect? I would like to filter out all objects that are wrapped as any of the wrapper types listed here:
https://developer.mozilla.org/en/XPConnect_wrappers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检测 XPCWrappedNative,因为
x instanceof Components.interfaces.nsISupports
返回 true。但是,对于 DOM 节点、文档、窗口等,这也会返回 true。如果这不是您想要的,则后续的 x.QueryInterface(Components.interfaces.nsIClassInfo) 对于大多数 DOM 对象应该会成功。除非底层 JS 对象公开wrappedJSObject 属性,否则您无法检测 XPCWrappedJS。 (您实际上看不到 XPCWrappedJS 对象本身,因为它是一个 C++ 对象,但该对象可以作为 XPCWrappedNative 传回 JS。)
您可以使用
x == XPCNativeWrapper(x)< 检测 XPCNativeWrapper /代码>。当然,底层对象本身就是 XPCWrappedNative。
您无法真正检测到 XPCSafeJSObjectWrapper,您只需知道如果您为内容对象解开 XPCNativeWrapper,那么您将获得 XPCSafeJSObjectWrapper。
You can detect an XPCWrappedNative because
x instanceof Components.interfaces.nsISupports
returns true. However, this also returns true for DOM nodes, documents, windows etc. If this isn't what you want, a subsequentx.QueryInterface(Components.interfaces.nsIClassInfo)
should succeed for most DOM objects.You can't detect an XPCWrappedJS unless the underlying JS object exposes the wrappedJSObject property. (You don't actually see the XPCWrappedJS object itself, since that's a C++ object, but that object could then get passed back into JS as an XPCWrappedNative.)
You can detect an XPCNativeWrapper using
x == XPCNativeWrapper(x)
. The underlying object will itself be an XPCWrappedNative, of course.You can't really detect an XPCSafeJSObjectWrapper, you just have to know that if you unwrap an XPCNativeWrapper for a content object then you will get an XPCSafeJSObjectWrapper.
为什么不直接检查包装对象公开的名为
wrappedJSObject
的属性是否存在?如果它像鸭子一样嘎嘎叫……Why not just check for the existence of a property named
wrappedJSObject
which wrapped objects expose? If it quacks like a duck...