chrome.windows

Description

Use thechrome.windows API to interact with browser windows. You can use this API to create, modify, and rearrange windows in the browser.

Permissions

When requested, awindows.Window contains an array oftabs.Tab objects. You mustdeclare the"tabs" permission in yourmanifest if you need access to theurl,pendingUrl,title, orfavIconUrl properties oftabs.Tab. For example:

{"name":"My extension",..."permissions":["tabs"],...}

Concepts and usage

The current window

Many functions in the extension system take an optionalwindowId argument, which defaults to thecurrent window.

Thecurrent window is the window that contains the code that is currently executing. It'simportant to realize that this can be different from the topmost or focused window.

For example, say an extension creates a few tabs or windows from a single HTML file, and that theHTML file contains a call totabs.query(). The current window is the window that contains thepage that made the call, no matter what the topmost window is.

In the case ofservice workers, the value of the current window falls back to the last activewindow. Under some circumstances, there may be no current window for background pages.

Examples

To try this API, install thewindows API example from thechrome-extension-samplesrepository.

Two windows, each with one tab
Two windows, each with one tab.

Types

CreateType

Chrome 44+

Specifies what type of browser window to create. 'panel' is deprecated and is available only to existing allowlisted extensions on Chrome OS.

Enum

"normal"
Specifies the window as a standard window.

"popup"
Specifies the window as a popup window.

"panel"
Specifies the window as a panel.

QueryOptions

Chrome 88+

Properties

  • populate

    boolean optional

    If true, thewindows.Window object has atabs property that contains a list of thetabs.Tab objects. TheTab objects only contain theurl,pendingUrl,title, andfavIconUrl properties if the extension's manifest file includes the"tabs" permission.

  • windowTypes

    WindowType[] optional

    If set, thewindows.Window returned is filtered based on its type. If unset, the default filter is set to['normal', 'popup'].

Window

Properties

  • alwaysOnTop

    boolean

    Whether the window is set to be always on top.

  • focused

    boolean

    Whether the window is currently the focused window.

  • height

    number optional

    The height of the window, including the frame, in pixels. In some circumstances a window may not be assigned aheight property; for example, when querying closed windows from thesessions API.

  • id

    number optional

    The ID of the window. Window IDs are unique within a browser session. In some circumstances a window may not be assigned anID property; for example, when querying windows using thesessions API, in which case a session ID may be present.

  • incognito

    boolean

    Whether the window is incognito.

  • left

    number optional

    The offset of the window from the left edge of the screen in pixels. In some circumstances a window may not be assigned aleft property; for example, when querying closed windows from thesessions API.

  • sessionId

    string optional

    The session ID used to uniquely identify a window, obtained from thesessions API.

  • state

    WindowState optional

    The state of this browser window.

  • tabs

    Tab[] optional

    Array oftabs.Tab objects representing the current tabs in the window.

  • top

    number optional

    The offset of the window from the top edge of the screen in pixels. In some circumstances a window may not be assigned atop property; for example, when querying closed windows from thesessions API.

  • type

    WindowType optional

    The type of browser window this is.

  • width

    number optional

    The width of the window, including the frame, in pixels. In some circumstances a window may not be assigned awidth property; for example, when querying closed windows from thesessions API.

WindowState

Chrome 44+

The state of this browser window. In some circumstances a window may not be assigned astate property; for example, when querying closed windows from thesessions API.

Enum

"normal"
Normal window state (not minimized, maximized, or fullscreen).

"minimized"
Minimized window state.

"maximized"
Maximized window state.

"fullscreen"
Fullscreen window state.

"locked-fullscreen"
Locked fullscreen window state. This fullscreen state cannot be exited by user action and is available only to allowlisted extensions on Chrome OS.

WindowType

Chrome 44+

The type of browser window this is. In some circumstances a window may not be assigned atype property; for example, when querying closed windows from thesessions API.

Enum

"normal"
A normal browser window.

"popup"
A browser popup.

"panel"
Deprecated in this API. A Chrome App panel-style window. Extensions can only see their own panel windows.

"app"
Deprecated in this API. A Chrome App window. Extensions can only see their app own windows.

"devtools"
A Developer Tools window.

Properties

WINDOW_ID_CURRENT

The windowId value that represents thecurrent window.

Value

-2

WINDOW_ID_NONE

The windowId value that represents the absence of a Chrome browser window.

Value

-1

Methods

create()

Promise
chrome.windows.create(
  createData?: object,
  callback?: function,
)

Creates (opens) a new browser window with any optional sizing, position, or default URL provided.

Parameters

  • createData

    object optional

    • focused

      boolean optional

      Iftrue, opens an active window. Iffalse, opens an inactive window.

    • height

      number optional

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

    • incognito

      boolean optional

      Whether the new window should be an incognito window.

    • left

      number optional

      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.

    • setSelfAsOpener

      boolean optional

      Chrome 64+

      Iftrue, the newly-created window's 'window.opener' is set to the caller and is in the sameunit of related browsing contexts as the caller.

    • state

      WindowState optional

      Chrome 44+

      The initial state of the window. Theminimized,maximized, andfullscreen states cannot be combined withleft,top,width, orheight.

    • tabId

      number optional

      The ID of the tab to add to the new window.

    • top

      number optional

      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.

    • type

      CreateType optional

      Specifies what type of browser window to create.

    • url

      string | string[] optional

      A URL or array of URLs to open as tabs in the window. Fully-qualified URLs must include a scheme, e.g., 'http://www.google.com', not 'www.google.com'. Non-fully-qualified URLs are considered relative within the extension. Defaults to the New Tab Page.

    • width

      number optional

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

  • callback

    function optional

    Thecallback parameter looks like:

    (window?: Window) => void

    • window

      Window optional

      Contains details about the created window.

Returns

  • Promise<Window | undefined>

    Chrome 88+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

get()

Promise
chrome.windows.get(
  windowId: number,
  queryOptions?: QueryOptions,
  callback?: function,
)

Gets details about a window.

Parameters

  • windowId

    number

  • queryOptions

    QueryOptions optional

    Chrome 88+
  • callback

    function optional

    Thecallback parameter looks like:

    (window: Window) => void

Returns

  • Promise<Window>

    Chrome 88+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

getAll()

Promise
chrome.windows.getAll(
  queryOptions?: QueryOptions,
  callback?: function,
)

Gets all windows.

Parameters

  • queryOptions

    QueryOptions optional

    Chrome 88+
  • callback

    function optional

    Thecallback parameter looks like:

    (windows: Window[]) => void

Returns

  • Promise<Window[]>

    Chrome 88+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

getCurrent()

Promise
chrome.windows.getCurrent(
  queryOptions?: QueryOptions,
  callback?: function,
)

Gets thecurrent window.

Parameters

  • queryOptions

    QueryOptions optional

    Chrome 88+
  • callback

    function optional

    Thecallback parameter looks like:

    (window: Window) => void

Returns

  • Promise<Window>

    Chrome 88+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

getLastFocused()

Promise
chrome.windows.getLastFocused(
  queryOptions?: QueryOptions,
  callback?: function,
)

Gets the window that was most recently focused — typically the window 'on top'.

Parameters

  • queryOptions

    QueryOptions optional

    Chrome 88+
  • callback

    function optional

    Thecallback parameter looks like:

    (window: Window) => void

Returns

  • Promise<Window>

    Chrome 88+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

remove()

Promise
chrome.windows.remove(
  windowId: number,
  callback?: function,
)

Removes (closes) a window and all the tabs inside it.

Parameters

  • windowId

    number

  • callback

    function optional

    Thecallback parameter looks like:

    () => void

Returns

  • Promise<void>

    Chrome 88+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

update()

Promise
chrome.windows.update(
  windowId: number,
  updateInfo: object,
  callback?: function,
)

Updates the properties of a window. Specify only the properties that to be changed; unspecified properties are unchanged.

Parameters

  • windowId

    number

  • updateInfo

    object

    • drawAttention

      boolean optional

      Iftrue, causes the window to be displayed in a manner that draws the user's attention to the window, without changing the focused window. The effect lasts until the user changes focus to the window. This option has no effect if the window already has focus. Set tofalse to cancel a previousdrawAttention request.

    • focused

      boolean optional

      Iftrue, brings the window to the front; cannot be combined with the state 'minimized'. Iffalse, brings the next window in the z-order to the front; cannot be combined with the state 'fullscreen' or 'maximized'.

    • height

      number optional

      The height to resize the window to in pixels. This value is ignored for panels.

    • left

      number optional

      The offset from the left edge of the screen to move the window to in pixels. This value is ignored for panels.

    • state

      WindowState optional

      The new state of the window. The 'minimized', 'maximized', and 'fullscreen' states cannot be combined with 'left', 'top', 'width', or 'height'.

    • top

      number optional

      The offset from the top edge of the screen to move the window to in pixels. This value is ignored for panels.

    • width

      number optional

      The width to resize the window to in pixels. This value is ignored for panels.

  • callback

    function optional

    Thecallback parameter looks like:

    (window: Window) => void

Returns

  • Promise<Window>

    Chrome 88+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

Events

onBoundsChanged

Chrome 86+
chrome.windows.onBoundsChanged.addListener(
  callback: function,
)

Fired when a window has been resized; this event is only dispatched when the new bounds are committed, and not for in-progress changes.

Parameters

  • callback

    function

    Thecallback parameter looks like:

    (window: Window) => void

onCreated

chrome.windows.onCreated.addListener(
  callback: function,
  filters?: object,
)

Fired when a window is created.

Parameters

  • callback

    function

    Chrome 46+

    Thecallback parameter looks like:

    (window: Window) => void

    • window

      Details of the created window.

  • filters

    object optional

    • windowTypes

      Conditions that the window's type being created must satisfy. By default it satisfies['normal', 'popup'].

onFocusChanged

chrome.windows.onFocusChanged.addListener(
  callback: function,
  filters?: object,
)

Fired when the currently focused window changes. Returnschrome.windows.WINDOW_ID_NONE if all Chrome windows have lost focus.Note: On some Linux window managers,WINDOW_ID_NONE is always sent immediately preceding a switch from one Chrome window to another.

Parameters

  • callback

    function

    Chrome 46+

    Thecallback parameter looks like:

    (windowId: number) => void

    • windowId

      number

      ID of the newly-focused window.

  • filters

    object optional

    • windowTypes

      Conditions that the window's type being removed must satisfy. By default it satisfies['normal', 'popup'].

onRemoved

chrome.windows.onRemoved.addListener(
  callback: function,
  filters?: object,
)

Fired when a window is removed (closed).

Parameters

  • callback

    function

    Chrome 46+

    Thecallback parameter looks like:

    (windowId: number) => void

    • windowId

      number

      ID of the removed window.

  • filters

    object optional

    • windowTypes

      Conditions that the window's type being removed must satisfy. By default it satisfies['normal', 'popup'].

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-10-15 UTC.