chrome.windows
- Description
Use the chrome.windows API to interact with browser windows. You can use this API to create, modify, and rearrange windows in the browser.
# Manifest
When requested, a windows.Window will contain an array of tabs.Tab objects. You must declare the "tabs" permission in your manifest if you require access to the url, pendingUrl, title, or favIconUrl properties of tabs.Tab. For example:
{"name": "My extension",
...
"permissions": ["tabs"],
...
}
# The current window
Many functions in the extension system take an optional windowId parameter, which defaults to the current window.
The current window is the window that contains the code that is currently executing. It's important 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 the HTML file contains a call to tabs.query. The current window is the window that contains the page that made the call, no matter what the topmost window is.
In the case of the event page, the value of the current window falls back to the last active window. Under some circumstances, there may be no current window for background pages.
# Examples
You can find simple examples of using the windows module in the examples/api/windows directory. Another example is in the tabs_api.html file of the inspector example. For other examples and for help in viewing the source code, see Samples.
Summary
- TypesCreateTypeQueryOptionsWindowWindowStateWindowType
- PropertiesWINDOW_ID_CURRENTWINDOW_ID_NONE
- MethodscreategetgetAllgetCurrentgetLastFocusedremoveupdate
- EventsonBoundsChangedonCreatedonFocusChangedonRemoved
Types
CreateType
Specifies what type of browser window to create. 'panel' is deprecated and is available only to existing whitelisted extensions on Chrome OS.
Type
"normal", "popup", or "panel"
QueryOptions
Properties
- populatebooleanoptional
If true, the windows.Window object has a tabs property that contains a list of the tabs.Tab objects. The Tab objects only contain the url, pendingUrl, title, and favIconUrl properties if the extension's manifest file includes the "tabs" permission.
- windowTypesWindowType[]optional
If set, the windows.Window returned is filtered based on its type. If unset, the default filter is set to ['normal', 'popup'].
Window
Properties
- alwaysOnTopboolean
Whether the window is set to be always on top.
- focusedboolean
Whether the window is currently the focused window.
- heightnumberoptional
The height of the window, including the frame, in pixels. In some circumstances a window may not be assigned a height property; for example, when querying closed windows from the sessions API.
- idnumberoptional
The ID of the window. Window IDs are unique within a browser session. In some circumstances a window may not be assigned an ID property; for example, when querying windows using the sessions API, in which case a session ID may be present.
- incognitoboolean
Whether the window is incognito.
- leftnumberoptional
The offset of the window from the left edge of the screen in pixels. In some circumstances a window may not be assigned a left property; for example, when querying closed windows from the sessions API.
- sessionIdstringoptional
The session ID used to uniquely identify a window, obtained from the sessions API.
- stateWindowStateoptional
The state of this browser window.
- tabsTab[]optional
Array of tabs.Tab objects representing the current tabs in the window.
- topnumberoptional
The offset of the window from the top edge of the screen in pixels. In some circumstances a window may not be assigned a top property; for example, when querying closed windows from the sessions API.
- typeWindowTypeoptional
The type of browser window this is.
- widthnumberoptional
The width of the window, including the frame, in pixels. In some circumstances a window may not be assigned a width property; for example, when querying closed windows from the sessions API.
WindowState
The state of this browser window. In some circumstances a window may not be assigned a state property; for example, when querying closed windows from the sessions API.
Type
"normal", "minimized", "maximized", "fullscreen", or "locked-fullscreen"
WindowType
The type of browser window this is. In some circumstances a window may not be assigned a type property; for example, when querying closed windows from the sessions API.
Type
"normal", "popup", "panel", "app", or "devtools"
Properties
WINDOW_ID_CURRENT
The windowId value that represents the current window.
Value
WINDOW_ID_NONE
The windowId value that represents the absence of a Chrome browser window.
Value
Methods
create
chrome.windows.create[createData?: object,
callback?: function,
]
Creates [opens] a new browser window with any optional sizing, position, or default URL provided.
Parameters
- createDataobjectoptional
- focusedbooleanoptional
If true, opens an active window. If false, opens an inactive window.
- heightnumberoptional
The height in pixels of the new window, including the frame. If not specified, defaults to a natural height.
- incognitobooleanoptional
Whether the new window should be an incognito window.
- leftnumberoptional
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.
- setSelfAsOpenerbooleanoptionalChrome 64+
If true, the newly-created window's 'window.opener' is set to the caller and is in the same unit of related browsing contexts as the caller.
- stateWindowStateoptionalChrome 44+
The initial state of the window. The minimized, maximized, and fullscreen states cannot be combined with left, top, width, or height.
- tabIdnumberoptional
The ID of the tab to add to the new window.
- topnumberoptional
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.
- typeCreateTypeoptional
Specifies what type of browser window to create.
- urlstring | string[]optional
A URL or array of URLs to open as tabs in the window. Fully-qualified URLs must include a scheme, e.g., '//www.google.com', not 'www.google.com'. Non-fully-qualified URLs are considered relative within the extension. Defaults to the New Tab Page.
- widthnumberoptional
The width in pixels of the new window, including the frame. If not specified, defaults to a natural width.
- callbackfunctionoptional
The callback parameter looks like: [window?: Window] => void
- windowWindowoptional
Contains details about the created window.
Returns
- PromisePending
This only returns a Promise when the callback parameter is not specified, and with MV3+. The type inside the Promise is the same as the 1st argument to callback.
get
chrome.windows.get[windowId: number,
queryOptions?: QueryOptions,
callback?: function,
]
Gets details about a window.
Parameters
- windowIdnumber
- queryOptionsQueryOptionsoptionalChrome 88+
- callbackfunctionoptional
The callback parameter looks like: [window: Window] => void
- windowWindow
Returns
- PromisePending
This only returns a Promise when the callback parameter is not specified, and with MV3+. The type inside the Promise is the same as the 1st argument to callback.
getAll
chrome.windows.getAll[queryOptions?: QueryOptions,
callback?: function,
]
Gets all windows.
Parameters
- queryOptionsQueryOptionsoptionalChrome 88+
- callbackfunctionoptional
The callback parameter looks like: [windows: Window[]] => void
- windowsWindow[]
Returns
- PromisePending
This only returns a Promise when the callback parameter is not specified, and with MV3+. The type inside the Promise is the same as the 1st argument to callback.
getCurrent
chrome.windows.getCurrent[queryOptions?: QueryOptions,
callback?: function,
]
Gets the current window.
Parameters
- queryOptionsQueryOptionsoptionalChrome 88+
- callbackfunctionoptional
The callback parameter looks like: [window: Window] => void
- windowWindow
Returns
- PromisePending
This only returns a Promise when the callback parameter is not specified, and with MV3+. The type inside the Promise is the same as the 1st argument to callback.
getLastFocused
chrome.windows.getLastFocused[queryOptions?: QueryOptions,
callback?: function,
]
Gets the window that was most recently focused typically the window 'on top'.
Parameters
- queryOptionsQueryOptionsoptionalChrome 88+
- callbackfunctionoptional
The callback parameter looks like: [window: Window] => void
- windowWindow
Returns
- PromisePending
This only returns a Promise when the callback parameter is not specified, and with MV3+. The type inside the Promise is the same as the 1st argument to callback.
remove
chrome.windows.remove[windowId: number,
callback?: function,
]
Removes [closes] a window and all the tabs inside it.
Parameters
- windowIdnumber
- callbackfunctionoptional
The callback parameter looks like: [] => void
Returns
- PromisePending
This only returns a Promise when the callback parameter is not specified, and with MV3+. The type inside the Promise is the same as the 1st argument to callback.
update
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
- windowIdnumber
- updateInfoobject
- drawAttentionbooleanoptional
If true, 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 to false to cancel a previous drawAttention request.
- focusedbooleanoptional
If true, brings the window to the front; cannot be combined with the state 'minimized'. If false, brings the next window in the z-order to the front; cannot be combined with the state 'fullscreen' or 'maximized'.
- heightnumberoptional
The height to resize the window to in pixels. This value is ignored for panels.
- leftnumberoptional
The offset from the left edge of the screen to move the window to in pixels. This value is ignored for panels.
- stateWindowStateoptional
The new state of the window. The 'minimized', 'maximized', and 'fullscreen' states cannot be combined with 'left', 'top', 'width', or 'height'.
- topnumberoptional
The offset from the top edge of the screen to move the window to in pixels. This value is ignored for panels.
- widthnumberoptional
The width to resize the window to in pixels. This value is ignored for panels.
- callbackfunctionoptional
The callback parameter looks like: [window: Window] => void
- windowWindow
Returns
- PromisePending
This only returns a Promise when the callback parameter is not specified, and with MV3+. The type inside the Promise is the same as the 1st argument to callback.
Events
onBoundsChanged
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
- callbackfunction
The callback parameter looks like: [window: Window] => void
- windowWindow
onCreated
chrome.windows.onCreated.addListener[callback: function,
filters?: object,
]
Fired when a window is created.
Parameters
- callbackfunctionChrome 46+
The callback parameter looks like: [window: Window] => void
- windowWindow
Details of the created window.
- filtersobjectoptional
- windowTypesWindowType[]
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. Returns chrome.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
- callbackfunctionChrome 46+
The callback parameter looks like: [windowId: number] => void
- windowIdnumber
ID of the newly-focused window.
- filtersobjectoptional
- windowTypesWindowType[]
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
- callbackfunctionChrome 46+
The callback parameter looks like: [windowId: number] => void
- windowIdnumber
ID of the removed window.
- filtersobjectoptional
- windowTypesWindowType[]
Conditions that the window's type being removed must satisfy. By default it satisfies ['normal', 'popup'].