Frame script environment 编辑

The frame script's global is a ContentFrameMessageManager, giving it the following environment:

contentThe DOM window of the content loaded in the browser. may be null (see below)
docShellThe nsIDocShell associated with the browser.
addEventListener()Listen to events from content.
removeEventListener()Stop listening to events from content.
addMessageListener()Listen to messages from chrome.
removeMessageListener()Stop listening to messages from chrome.
sendAsyncMessage()Send an asynchronous message to chrome.
sendSyncMessage()Send a synchronous message to chrome.
dump()Print a message to the console.
atob()Base64 decode.
btoa()Base64 encode.
ComponentsThe usual Components object.

In particular, note that a frame script accesses the DOM window using content, not window:

// frame script
var links = content.document.getElementsByTagName("a");

All the frame scripts running in a tab share this global. However, any top-level variables defined by a script are not stored on the global: instead, top-level variables are stored in a special per-script object that delegates to the per-tab global. This means you don't have to worry about global variables you define conflicting with global variables defined by another frame script. You can still access the global directly via this.

Frame scripts run with system principals. If you want other principals, you can use a Sandbox.

Frame scripts run with system privileges and have access to the Components object, enabling them to use XPCOM objects and JSMs. However, some APIs  that work in the chrome process will not work in a frame script. See Limitations of frame scripts for more details.

Events

Besides the regular DOM events being captured/bubbling up from content the current content object the following additional events get fired in a frame script environment:

unload

Fires when the frame script environment is shut down, i.e. when a tab gets closed.

If you use a capturing event listener on the ContentFrameMessageManager, you should verify that its event.target is set to the ContentFrameMessageManager global object in order to avoid handling unload events from content.

Does not bubble.

DOMWindowCreated

Fires when a new content object is created.

This can be used if a framescript needs to interact with individual DOM windows instead of simply listening for events bubbling up from content.
Another use is to interact with the content very early in the page load process, long before DOMContentLoaded event is fired.
 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:53 次

字数:4555

最后编辑:7年前

编辑次数:0 次

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