Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

框架脚本环境

这篇翻译不完整。请帮忙从英语翻译这篇文章

框架脚本的全局是 ContentFrameMessageManager,提供下列环境:

content The DOM window of the content loaded in the browser. may be null (see below)
docShell The 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.
Components The usual Components object.

特别注意,框架脚本使用 content 访问 DOM 窗口,而不是 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.

框架脚本使用系统主体运行。如果你想使用其他主题,可以使用 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.

事件

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
Bubbles
No

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.

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.
 

 

文档标签和贡献者

 此页面的贡献者: yfdyh000
 最后编辑者: yfdyh000,