非标
此特征是非标准的,且不在标准轨道中。不要在面向 Web 的生产站点中使用它:它不适用于每个用户。实现之间可能存在大的不兼容性,且行为将来可能改变。

bug 1318532 for exposing this API to WebExtensions in Firefox .

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:

Browser API interfaces

To support the requirement of a browser <iframe> 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()
设置当前 <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:

<iframe> gains support for the methods of the EventTarget interface
addEventListener() , removeEventListener() ,和 dispatchEvent() .
HTMLIFrameElement.sendMouseEvent()
Sends a MouseEvent <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 了解更多细节。

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 <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.

事件

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> 内容。
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 代替。
mozbrowsershowmodalprompt
Sent when alert() , confirm() ,或 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() .

规范

This API isn't part of any official specification.

浏览器兼容性

Supported Firefox 47 in chrome (privileged) code only.

另请参阅

元数据

  • 最后修改:
  1. 指南
    1. Using the Browser API
  2. 方法
    1. HTMLIFrameElement.addNextPaintListener()
    2. HTMLIFrameElement.clearMatch()
    3. HTMLIFrameElement.download()
    4. HTMLIFrameElement.executeScript()
    5. HTMLIFrameElement.findAll()
    6. HTMLIFrameElement.findNext()
    7. HTMLIFrameElement.getActive()
    8. HTMLIFrameElement.getCanGoBack()
    9. HTMLIFrameElement.getCanGoForward()
    10. HTMLIFrameElement.getContentDimensions()
    11. HTMLIFrameElement.getMainfest()
    12. HTMLIFrameElement.getMuted()
    13. HTMLIFrameElement.getScreenshot()
    14. HTMLIFrameElement.getStructuredData()
    15. HTMLIFrameElement.goBack()
    16. HTMLIFrameElement.goForward()
    17. HTMLIFrameElement.getVisible()
    18. HTMLIFrameElement.getVolume()
    19. HTMLIFrameElement.mute()
    20. HTMLIFrameElement.purgeHistory()
    21. HTMLIFrameElement.reload()
    22. HTMLIFrameElement.removeNextPaintListener()
    23. HTMLIFrameElement.sendMouseEvent()
    24. HTMLIFrameElement.sendTouchEvent()
    25. HTMLIFrameElement.setActive()
    26. HTMLIFrameElement.setInputMethodActive()
    27. HTMLIFrameElement.setNFCFocus()
    28. HTMLIFrameElement.setVisible()
    29. HTMLIFrameElement.setVolume()
    30. HTMLIFrameElement.stop()
    31. HTMLIFrameElement.unmute()
    32. HTMLIFrameElement.zoom()
  3. 事件
    1. mozbrowseractivitydone
    2. mozbrowserasyncscroll
    3. mozbrowseraudioplaybackchange
    4. mozbrowsercaretstatechanged
    5. mozbrowserclose
    6. mozbrowsercontextmenu
    7. mozbrowserdocumentfirstpaint
    8. mozbrowsererror
    9. mozbrowserfindchange
    10. mozbrowserfirstpaint
    11. mozbrowsericonchange
    12. mozbrowserloadend
    13. mozbrowserloadstart
    14. mozbrowserlocationchange
    15. mozbrowsermanifestchange
    16. mozbrowsermetachange
    17. mozbrowseropensearch
    18. mozbrowseropentab
    19. mozbrowseropenwindow
    20. mozbrowserresize
    21. mozbrowserscroll
    22. mozbrowserscrollareachanged
    23. mozbrowserscrollviewchange
    24. mozbrowsersecuritychange
    25. mozbrowserselectionstatechanged
    26. mozbrowsershowmodalprompt
    27. mozbrowsertitlechange
    28. mozbrowserusernameandpasswordrequired
    29. mozbrowservisibilitychange