Browser API 编辑

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

See bug 1318532 for exposing this API to WebExtensions in Firefox.

The HTML Browser API is an extension of the HTML <iframe> element that allows web apps to implement browsers or browser-like applications. It currently works in (privileged) chrome code on Firefox desktop (version 47 and above).

Browser API concepts and usage

The Browser API consists of two major parts:

  • Custom <iframe> attributes: By adding a mozbrowser attribute to the <iframe> element we can make it appear like a top-level browser window to the embedded content. This means that window.top, window.parent, window.frameElement, etc. should not reflect the frame hierarchy.
  • API extensions: The API includes several new methods and events to manipulate and listen for changes to the embedded content's state, interited by the HTMLIFrameElement interface.

Browser API interfaces

To support the requirement of a browser <iframe>, the HTMLIFrameElement DOM interface has been extended with new methods that give the <iframe> some super powers.

The following navigation methods allow navigation through the browsing history of the <iframe>. They are necessary to be able to implement back, forward, stop, and reload buttons.

HTMLIFrameElement.reload()
Allows reloading of the <iframe> element content.
HTMLIFrameElement.stop()
Allows stopping of the <iframe>'s content loading.
HTMLIFrameElement.getCanGoBack()
Indicates whether it's possible to navigate backwards.
HTMLIFrameElement.goBack()
Changes the location of the <iframe> for the previous location in its browsing history.
HTMLIFrameElement.getCanGoForward()
Indicates whether it's possible to navigate forward.
HTMLIFrameElement.goForward()
Changes the location of the <iframe> for the next location in its browsing history.

Management methods

The next set of methods manage the resources used by a browser <iframe>. These are especially useful for implementing tabbed browser application.

HTMLIFrameElement.setVisible()
Changes the visibility state of a browser <iframe>. This can influence resource allocation and some function usage such as requestAnimationFrame.
HTMLIFrameElement.getVisible()
Indicates the current visibility state of the browser <iframe>.
HTMLIFrameElement.setActive()
Sets the current <iframe> as the active frame, which has an effect on how it is prioritized by the process manager.
HTMLIFrameElement.getActive()
Indicates whether the current browser <iframe> is the currently active frame.

The following methods allow direct control of sound in the browser element.

HTMLIFrameElement.getVolume()
Gets the current volume of the browser <iframe>.
HTMLIFrameElement.setVolume()
Sets the current volume of the browser <iframe>.
HTMLIFrameElement.mute()
Mutes any audio playing in the browser <iframe>.
HTMLIFrameElement.unmute()
Unmutes any audio playing in the browser <iframe>.
HTMLIFrameElement.getMuted()
Indicates whether the browser <iframe> is currently muted.

In order to manage the browser <iframe>'s content, many new events were added (see below). The following methods are used to deal with those events:

The <iframe> gains support for the methods of the EventTarget interface
addEventListener(), removeEventListener(), and dispatchEvent().
HTMLIFrameElement.sendMouseEvent()
Sends a MouseEvent to the <iframe>'s content.

Utility methods

Last, there are some utility methods, useful for apps that host a browser <iframe>.

HTMLIFrameElement.getStructuredData()
Retrieves any structured microdata (and hCard and hCalendar microformat data) contained in the HTML loaded in the browser <iframe> and returns it as JSON.

Browser API methods removed in Firefox 65

In Firefox 65, several of the Mozilla Browser API methods were removed in an effort to cut down on DOMRequest usage in the browser, and remove the parts of the Browser API that are no longer needed (it is used only by the Firefox DevTools at this point). See bug 1503070 for more details.

The methods removed in Firefox 65 are:

HTMLIFrameElement.addNextPaintListener()
Defines a handler to listen for the next MozAfterPaint event in the browser <iframe>.
HTMLIFrameElement.clearMatch()
Clears any content highlighted by findAll() or findNext().
HTMLIFrameElement.download()
Downloads a specified URL, storing it at the specified filename/path.
HTMLIFrameElement.executeScript()
Allows a specified script to be executed against a page loaded in the browser <iframe>.
HTMLIFrameElement.findAll()
Searches for a string in a browser <iframe>'s content; if found, the first instance of the string relative to the caret position will be highlighted.
HTMLIFrameElement.findNext()
Highlights the next or previous instance of a search result after a findAll() search has been carried out.
HTMLIFrameElement.getContentDimensions()
Retrieves the X and Y dimensions of the content window.
HTMLIFrameElement.getManifest()
Retrieves the manifest of an app loaded in the browser <iframe> and returns it as JSON.
HTMLIFrameElement.getScreenshot()
Takes a screenshot of the browser <iframe>'s content. This is particularly useful to get thumbnails of tabs in a tabbed browser app.
HTMLIFrameElement.purgeHistory()
Clears all the resources (cookies, localStorage, cache, etc.) associated with the browser <iframe>.
HTMLIFrameElement.sendTouchEvent()
Sends a TouchEvent to the <iframe>'s content. Note that this method is available for touch enabled devices only.
HTMLIFrameElement.removeNextPaintListener()
Removes a handler previously set with addNextPaintListener().
HTMLIFrameElement.zoom()
Changes the zoom factor of the browser <iframe>'s content. This is particularly useful for zooming in/out on non-touch-enabled devices.

Events

In order to allow an application to manage the browser <iframe>, the application can listen for new events about what's happening within the browser <iframe>. The following new events can be listened for:

mozbrowseractivitydone
Sent when something inside the browser <iframe> triggers a web activity, and that web activity's message is consumed by the receiving app.
mozbrowserasyncscroll
Sent when the browser <iframe> content scrolls (the APCZ version.) See also mozbrowserscroll. Note that this is obsolete.
mozbrowseraudioplaybackchange
Sent when audio starts or stops playing within the browser <iframe> content.
mozbrowsercaretstatechanged
Sent when the text selected inside the browser <iframe> content changes.
mozbrowserclose
Sent when window.close() is called within a browser <iframe>.
mozbrowsercontextmenu
Sent when a browser <iframe> tries to open a context menu. This allows  handling of the <menuitem> element available within the browser <iframe>'s content.
mozbrowserdocumentfirstpaint
Sent when a new paint occurs on any document in the browser <iframe>.
mozbrowsererror
Sent when an error occurs while trying to load content within a browser <iframe>.
mozbrowserfindchange
Sent when a search operation is performed on the browser <iframe> content (see HTMLIFrameElement search methods.)
mozbrowserfirstpaint
Sent when the <iframe> paints content for the first time (this doesn't include the initial paint from about:blank.)
mozbrowsericonchange
Sent when a new icon (e.g. <link rel="icon"> or <link rel="apple-touch-icon">) is available in the browser <iframe>'s content.
mozbrowserloadend
Sent when the browser <iframe> has finished loading all its assets.
mozbrowserloadstart
Sent when the browser <iframe> starts to load a new page.
mozbrowserlocationchange
Sent when a browser <iframe>'s location changes.
mozbrowsermanifestchange
Sent when a the path to the app manifest changes, in the case of a browser <iframe> with an open web app embedded in it.
mozbrowsermetachange
Sent when a <meta> elelment is added to, removed from or changed in the browser <iframe>'s content.
mozbrowseropenwindow
Sent when window.open() is called within a browser <iframe>.
mozbrowseropentab
Sent when a new tab is opened within a browser <iframe> as a result of the user issuing a command to open a link target in a new tab (for example ctrl/cmd + click.)
mozbrowserresize
Sent when the browser <iframe>'s window size has changed.
mozbrowserscroll
Sent when the browser <iframe> content scrolls.
mozbrowserscrollareachanged
Sent when the available scrolling area in the browser <iframe> changes. This can occur on resize and when the page size changes (while loading for example.)
mozbrowserscrollviewchange
Sent when asynchronous scrolling (i.e. APCZ) starts or stops.
mozbrowsersecuritychange
Sent when the SSL state changes within a browser <iframe>.
mozbrowserselectionstatechanged
Sent when the text selected inside the browser <iframe> content changes. Note that this is obsolete, and newer implementations use mozbrowsercaretstatechanged instead.
mozbrowsershowmodalprompt
Sent when alert(), confirm(), or prompt() are called within a browser <iframe>.
mozbrowsertitlechange
Sent when the document title changes within a browser <iframe>.
mozbrowserusernameandpasswordrequired
Sent when an HTTP authentification is requested.
mozbrowseropensearch
Sent when a link to a search engine is found.
mozbrowservisibilitychange
Sent when the visibility state of the current browser iframe <iframe> changes, for example due to a call to setVisible().

Specifications

This API isn't part of any official specification.

Browser compatibility

Supported Firefox 47 in chrome (privileged) code only.

See also

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

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

发布评论

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

词条统计

浏览:95 次

字数:37693

最后编辑:6 年前

编辑次数:0 次

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