windows.create()

创建新窗口。

When you create the window, you can:

  • Load one or more new tabs into the window.
  • Move a tab from an existing window into the new window.
  • Set the size and position of the window.
  • Create a "panel" style window, which in this context means a window without any of the normal browser UI (address bar, toolbar, etc.).
  • Set various properties of the window, such as whether it is focused or private.

This is an asynchronous function that returns a Promise .

句法

var creating = browser.windows.create(
createData

// optional object


)


					

参数

createData 可选

对象 .

allowScriptsToClose 可选

boolean . When the window is opened, it will contain a single tab, or more than one tab if url is given and includes an array containing more than one URL. By default scripts running in these pages are not allowed to close their tab using window.close() .  If you include  allowScriptsToClose and set it to true , then this default behavior is changed, so scripts can close their tabs. Note that:

  • this only applies to the tabs that were opened when the window was created. If the user opens more tabs in this window, then scripts will not be able to close those new tabs.
  • if the url(s) given in url point to extension pages (that is, they are pages included with this extension and loaded with the "moz-extension:" protocol) then scripts are by default allowed to close those tabs.
cookieStoreId 可选

integer . If present, specifies the CookieStoreId for all tabs that will be created when the window is opened.

focused 可选

boolean 。若 true , the new window will be focused. If false , the new window will be opened in the background and the currently focused window will stay focused. Defaults to true .

height 可选

integer . The height in pixels of the new window, including the frame. If not specified defaults to a natural height.

incognito 可选

boolean . Whether the new window should be an incognito (private) window. Note that if you specify incognito and tabId , the ID must refer to a private tab — that is, you can't move a non-private tab to a private window.

left 可选

integer . The number of pixels to position the new window from the left edge of the screen. If not specified, the new window is offset naturally from the last focused window. This value is ignored for panels. (In Firefox, this value currently is ignored for popups (bug 1271047) but can be set using browser.windows.update().)

state 可选

A windows.WindowState value. The initial state of the window. The minimized , maximized and, fullscreen states cannot be combined with left , top , width ,或 height .

tabId 可选

integer . If included, moves a tab of the specified ID from an existing window into the new window.

titlePreface 可选

string . Use this to add a string to the beginning of the browser window's title. Depending on the underlying operating system, this might not work on browser windows that don't have a title (such as about:blank in Firefox).

top 可选

integer . The number of pixels to position the new window from the top edge of the screen. If not specified, the new window is offset naturally from the last focused window. This value is ignored for panels. (In Firefox, this value currently is ignored for popups (bug 1271047) but can be set using browser.windows.update().)

type 可选

A windows.CreateType value. Specifies what type of browser window to create. Specify panel or popup here to open a window without any of the normal browser UI (address bar, toolbar, etc).

url 可选

string or array of string s. A URL or array of URLs to open as tabs in the window. Fully-qualified URLs must include a scheme (i.e. http://www.google.com , not www.google.com ). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page.

width 可选

integer . The width in pixels of the new window, including the frame. If not specified defaults to a natural width.

返回值

A Promise that will be fulfilled with a windows.Window object containing the details of the new window. This Window object will always have its tabs property set, unlike the Window objects returned from windows.get() and similar APIs, which only contain tabs populate option is passed. If any error occurs, the promise will be rejected with an error message.

范例

Open a window containing two tabs:

function onCreated(windowInfo) {
  console.log(`Created window: ${windowInfo.id}`);
}
function onError(error) {
  console.log(`Error: ${error}`);
}
browser.browserAction.onClicked.addListener((tab) => {
  var creating = browser.windows.create({
    url: ["https://developer.mozilla.org",
          "https://addons.mozilla.org"]
  });
  creating.then(onCreated, onError);
});

					

Open a window when the user clicks a browser action, and move the currently active tab into it:

function onCreated(windowInfo) {
  console.log(`Created window: ${windowInfo.id}`);
}
function onError(error) {
  console.log(`Error: ${error}`);
}
browser.browserAction.onClicked.addListener((tab) => {
  var creating = browser.windows.create({
    tabId: tab.id
  });
  creating.then(onCreated, onError);
});

					

Open a small panel-style window, and load a locally-packaged file into it:

function onCreated(windowInfo) {
  console.log(`Created window: ${windowInfo.id}`);
}
function onError(error) {
  console.log(`Error: ${error}`);
}
browser.browserAction.onClicked.addListener((tab) => {
  var popupURL = browser.extension.getURL("popup/popup.html");
  var creating = browser.windows.create({
    url: popupURL,
    type: "popup",
    height: 200,
    width: 200
  });
  creating.then(onCreated, onError);
});

					

Example extensions

浏览器兼容性

BCD tables only load in the browser

注意: This API is based on Chromium's chrome.windows API. This documentation is derived from windows.json in the Chromium code.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

另请参阅

Found a problem with this page?

最后修改: , 由 MDN 贡献者

  1. 浏览器扩展名
  2. 快速入门
    1. What are extensions?
    2. Your first extension
    3. Your second extension
    4. Anatomy of an extension
    5. Example extensions
    6. What next?
  3. 概念
    1. Using the JavaScript APIs
    2. Content scripts
    3. Match patterns
    4. Working with files
    5. 国际化
    6. Content Security Policy
    7. Native messaging
    8. Differences between API implementations
    9. Chrome incompatibilities
  4. 用户界面
    1. 用户界面
    2. Toolbar button
    3. Address bar button
    4. Sidebars
    5. Context menu items
    6. Options page
    7. Extension pages
    8. Notifications
    9. Address bar suggestions
    10. Developer tools panels
  5. 如何
    1. Intercept HTTP requests
    2. Modify a web page
    3. Insert external content
    4. Share objects with page scripts
    5. Add a button to the toolbar
    6. Implement a settings page
    7. Work with the Tabs API
    8. Work with the Bookmarks API
    9. Work with the Cookies API
    10. Work with contextual identities
    11. Interact with the clipboard
    12. Build a cross-browser extension
  6. Firefox differentiators
  7. JavaScript API
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browserSettings
    6. browsingData
    7. captivePortal
    8. clipboard
    9. 命令
    10. contentScripts
    11. contextualIdentities
    12. Cookie
    13. devtools
    14. dns
    15. downloads
    16. events
    17. extension
    18. extensionTypes
    19. find
    20. history
    21. i18n
    22. identity
    23. idle
    24. management
    25. menus
    26. notifications
    27. omnibox
    28. pageAction
    29. permissions
    30. pkcs11
    31. privacy
    32. proxy
    33. runtime
    34. search
    35. sessions
    36. sidebarAction
    37. storage
    38. tabs
    39. theme
    40. topSites
    41. 类型
    42. userScripts
    43. webNavigation
    44. webRequest
    45. windows
      1. 方法
        1. create()
        2. get()
        3. getAll()
        4. getCurrent()
        5. getLastFocused()
        6. remove()
        7. update()
      2. 特性
        1. WINDOW_ID_CURRENT
        2. WINDOW_ID_NONE
      3. 类型
        1. CreateType
        2. Window
        3. WindowState
        4. WindowType
      4. 事件
        1. onCreated
        2. onFocusChanged
        3. onRemoved
  8. Manifest keys
    1. 介绍
    1. 作者
    2. background
    3. browser_action
    4. browser_specific_settings
    5. chrome_settings_overrides
    6. chrome_url_overrides
    7. 命令
    8. content_scripts
    9. content_security_policy
    10. default_locale
    11. description
    12. developer
    13. devtools_page
    14. dictionaries
    15. externally_connectable
    16. homepage_url
    17. icons
    18. incognito
    19. manifest_version
    20. name
    21. offline_enabled
    22. omnibox
    23. optional_permissions
    24. options_page
    25. options_ui
    26. page_action
    27. permissions
    28. protocol_handlers
    29. short_name
    30. sidebar_action
    31. storage
    32. theme
    33. theme_experiment
    34. user_scripts
    35. version
    36. version_name
    37. web_accessible_resources
  9. Extension Workshop
    1. Develop
    2. Publish
    3. Manage
    4. Enterprise
  10. Contact us
  11. Channels
    1. Add-ons blog
    2. Add-ons forum
    3. Add-ons chat