您可以检查一个对象是否被 XPConnect (Firefox) 包装吗?

发布于 2024-10-22 05:37:10 字数 468 浏览 6 评论 0原文

我的场景: 我正在迭代窗口对象并尝试仅检索用户定义的变量/函数并过滤掉本机浏览器对象。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-10-29 05:37:10

您可以检测 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 subsequent x.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.

荒芜了季节 2024-10-29 05:37:10

为什么不直接检查包装对象公开的名为 wrappedJSObject 的属性是否存在?如果它像鸭子一样嘎嘎叫……

Why not just check for the existence of a property named wrappedJSObject which wrapped objects expose? If it quacks like a duck...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文