Windows
Windows
Use the chrome.windows
module
to interact with browser windows.
You can use this module to
create, modify, and rearrange windows in the browser.
Manifest
To use the windows API,
you must declare the "tabs" permission
in manifest.json.
(No, that isn't a typo —
the window and tabs modules interact so closely we
decided to just share one permission between them.)
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
chrome.tabs.getSelected.
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 background 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.
API reference: chrome.windows
Properties
WINDOW_ID_NONE
chrome.windows.WINDOW_ID_NONE
WINDOW_ID_NONE
(
-1
)
The windowId value that represents the absence of a chrome browser window.
WINDOW_ID_CURRENT
chrome.windows.WINDOW_ID_CURRENT
WINDOW_ID_CURRENT
(
-2
)
The windowId value that represents the current window.
Methods
create
chrome.windows.create(object
createData, function
callback)
Creates (opens) a new browser with any optional sizing, position or default URL provided.
Parameters
-
createData
(
optional
object
)
-
Undocumented.
-
-
url
(
optional
string or array of string
)
- A URL or list 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.
-
tabId
(
optional
integer
)
- The id of the tab for which you want to adopt to the new window.
-
left
(
optional
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 focusd window. This value is ignored for panels.
-
top
(
optional
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 focusd window. This value is ignored for panels.
-
width
(
optional
integer
)
- The width in pixels of the new window. If not specified defaults to a natural width.
-
height
(
optional
integer
)
- The height in pixels of the new window. If not specified defaults to a natural height.
-
focused
(
optional
boolean
)
- If true, opens an active window. If false, opens an inactive window.
-
incognito
(
optional
boolean
)
- Whether the new window should be an incognito window.
-
type
(
optional
enumerated
string
["normal", "popup", "panel"]
)
- Specifies what type of browser window to create. The 'panel' type creates a popup unless the '--enable-panels' flag is set.
-
callback
(
optional
function
)
-
Undocumented.
Callback function
If you specify the callback parameter, it should
specify a function that looks like this:
function(Window window) {...};
-
window
- Contains details about the created window.
get
chrome.windows.get(integer
windowId, object
getInfo, function
callback)
Gets details about a window.
Parameters
-
windowId
(
integer
)
-
Undocumented.
-
getInfo
(
optional
object
)
-
Undocumented.
-
-
populate
(
optional
boolean
)
- If true, the window object will have a tabs property that contains a list of the Tab objects
-
callback
(
function
)
-
Undocumented.
Callback function
The callback parameter should specify a function
that looks like this:
function(Window window) {...};
getAll
chrome.windows.getAll(object
getInfo, function
callback)
Gets all windows.
Parameters
-
getInfo
(
optional
object
)
-
Undocumented.
-
-
populate
(
optional
boolean
)
- If true, each window object will have a tabs property that contains a list of the Tab objects for that window.
-
callback
(
function
)
-
Undocumented.
Callback function
The callback parameter should specify a function
that looks like this:
function(array of Window windows) {...};
getCurrent
chrome.windows.getCurrent(object
getInfo, function
callback)
Gets the current window.
Parameters
-
getInfo
(
optional
object
)
-
Undocumented.
-
-
populate
(
optional
boolean
)
- If true, the window object will have a tabs property that contains a list of the Tab objects
-
callback
(
function
)
-
Undocumented.
Callback function
The callback parameter should specify a function
that looks like this:
function(Window window) {...};
getLastFocused
chrome.windows.getLastFocused(object
getInfo, function
callback)
Gets the window that was most recently focused — typically the window 'on top'.
Parameters
-
getInfo
(
optional
object
)
-
Undocumented.
-
-
populate
(
optional
boolean
)
- If true, the window object will have a tabs property that contains a list of the Tab objects
-
callback
(
function
)
-
Undocumented.
Callback function
The callback parameter should specify a function
that looks like this:
function(Window window) {...};
remove
chrome.windows.remove(integer
windowId, function
callback)
Removes (closes) a window, and all the tabs inside it.
Parameters
-
windowId
(
integer
)
-
Undocumented.
-
callback
(
optional
function
)
-
Undocumented.
Callback function
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
update
chrome.windows.update(integer
windowId, object
updateInfo, function
callback)
Updates the properties of a window. Specify only the properties that you want to change; unspecified properties will be left unchanged.
Parameters
-
windowId
(
integer
)
-
Undocumented.
-
updateInfo
(
object
)
-
Undocumented.
-
-
left
(
optional
integer
)
- The offset from the left edge of the screen to move the window to in pixels. This value is ignored for panels.
-
top
(
optional
integer
)
- The offset from the top edge of the screen to move the window to in pixels. This value is ignored for panels.
-
width
(
optional
integer
)
- The width to resize the window to in pixels. This value is ignored for panels.
-
height
(
optional
integer
)
- The height to resize the window to in pixels. This value is ignored for panels.
-
focused
(
optional
boolean
)
- If true, brings the window to the front. If false, brings the next window in the z-order to the front.
-
drawAttention
(
optional
boolean
)
- 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 draw attention request.
-
state
(
optional
enumerated
string
["normal", "minimized", "maximized", "fullscreen"]
)
- The new state of the window. The 'minimized', 'maximized' and 'fullscreen' states cannot be combined with 'left', 'top', 'width' or 'height'.
-
callback
(
optional
function
)
-
Undocumented.
Callback function
If you specify the callback parameter, it should
specify a function that looks like this:
function(Window window) {...};
Events
onCreated
chrome.windows.onCreated.addListener(function(Window window) {...});
Fired when a window is created.
Listener parameters
-
window
- Details of the window that was created.
onFocusChanged
chrome.windows.onFocusChanged.addListener(function(integer windowId) {...});
Fired when the currently focused window changes. Will be chrome.windows.WINDOW_ID_NONE if all chrome windows have lost focus. Note: On some Linux window managers, WINDOW_ID_NONE will always be sent immediately preceding a switch from one chrome window to another.
Listener parameters
-
windowId
(
integer
)
- ID of the newly focused window.
onRemoved
chrome.windows.onRemoved.addListener(function(integer windowId) {...});
Fired when a window is removed (closed).
Listener parameters
-
windowId
(
integer
)
- ID of the removed window.
Types
Window
(
object
)
Undocumented.
-
id
(
integer
)
- The ID of the window. Window IDs are unique within a browser session.
-
focused
(
boolean
)
- Whether the window is currently the focused window.
-
top
(
integer
)
- The offset of the window from the top edge of the screen in pixels.
-
left
(
integer
)
- The offset of the window from the left edge of the screen in pixels.
-
width
(
integer
)
- The width of the window in pixels.
-
height
(
integer
)
- The height of the window in pixels.
-
tabs
(
optional
array of
Tab
)
- Array of Tab objects representing the current tabs in the window.
-
incognito
(
boolean
)
- Whether the window is incognito.
-
type
(
enumerated
string
["normal", "popup", "panel", "app"]
)
- The type of browser window this is.
-
state
(
enumerated
string
["normal", "minimized", "maximized", "fullscreen"]
)
- The state of this browser window.