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 topmost window.
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
getLastError
chrome.extensionlastError
Methods
create
void
chrome.windows.create(, object
createData, function
callback)
Undocumented.
Creates (opens) a new browser with any optional sizing, position or default URL provided.
Parameters
-
createData
(
optional
Type
array of
object
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
url
(
optional
Type
array of
string
)
-
Undocumented.
- The URL to navigate the first tab to. 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.
-
-
left
(
optional
Type
array of
integer
)
-
Undocumented.
- 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.
-
-
top
(
optional
Type
array of
integer
)
-
Undocumented.
- 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.
-
-
width
(
optional
Type
array of
integer
)
-
Undocumented.
- The width in pixels of the new window. If not specified defaults to a natural width.
-
-
height
(
optional
Type
array of
integer
)
-
Undocumented.
- The height in pixels of the new window. If not specified defaults to a natural height.
-
-
incognito
(
optional
Type
array of
boolean
)
-
Undocumented.
- Whether the new window should be an incognito window.
-
-
type
(
optional
Type
array of
string
)
-
Undocumented.
- Specifies what type of browser window to create.
-
-
callback
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
Returns
Callback function
The callback parameter should specify a function
that looks like this:
If you specify the callback parameter, it should
specify a function that looks like this:
function(Window window) {...});
-
window
(
optional
Window
array of
paramType
)
-
Undocumented.
- Contains details about the created window.
-
get
void
chrome.windows.get(, integer
windowId, function
callback)
Undocumented.
Gets details about a window.
Parameters
-
windowId
(
optional
Type
array of
integer
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
callback
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
Returns
Callback function
The callback parameter should specify a function
that looks like this:
If you specify the callback parameter, it should
specify a function that looks like this:
function(Window window) {...});
-
window
(
optional
Window
array of
paramType
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
getAll
void
chrome.windows.getAll(, object
getInfo, function
callback)
Undocumented.
Gets all windows.
Parameters
-
getInfo
(
optional
Type
array of
object
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
populate
(
optional
Type
array of
boolean
)
-
Undocumented.
- If true, each window object will have a tabs property that contains a list of the Tab objects for that window.
-
-
callback
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
Returns
Callback function
The callback parameter should specify a function
that looks like this:
If you specify the callback parameter, it should
specify a function that looks like this:
function(array of Window windows) {...});
-
windows
(
optional
Type
array of
Window
array of
paramType
paramType
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
getCurrent
void
chrome.windows.getCurrent(, function
callback)
Undocumented.
Gets the current window.
Parameters
-
callback
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
Returns
Callback function
The callback parameter should specify a function
that looks like this:
If you specify the callback parameter, it should
specify a function that looks like this:
function(Window window) {...});
-
window
(
optional
Window
array of
paramType
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
getLastFocused
void
chrome.windows.getLastFocused(, function
callback)
Undocumented.
Gets the window that was most recently focused — typically the window 'on top'.
Parameters
-
callback
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
Returns
Callback function
The callback parameter should specify a function
that looks like this:
If you specify the callback parameter, it should
specify a function that looks like this:
function(Window window) {...});
-
window
(
optional
Window
array of
paramType
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
remove
void
chrome.windows.remove(, integer
windowId, function
callback)
Undocumented.
Removes (closes) a window, and all the tabs inside it.
Parameters
-
windowId
(
optional
Type
array of
integer
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
callback
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
Returns
Callback function
The callback parameter should specify a function
that looks like this:
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...});
update
void
chrome.windows.update(, integer
windowId, object
updateInfo, function
callback)
Undocumented.
Updates the properties of a window. Specify only the properties that you want to change; unspecified properties will be left unchanged.
Parameters
-
windowId
(
optional
Type
array of
integer
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
updateInfo
(
optional
Type
array of
object
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
left
(
optional
Type
array of
integer
)
-
Undocumented.
- The offset from the left edge of the screen to move the window to in pixels.
-
-
top
(
optional
Type
array of
integer
)
-
Undocumented.
- The offset from the top edge of the screen to move the window to in pixels.
-
-
width
(
optional
Type
array of
integer
)
-
Undocumented.
- The width to resize the window to in pixels.
-
-
height
(
optional
Type
array of
integer
)
-
Undocumented.
- The height to resize the window to in pixels.
-
-
callback
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
Returns
Callback function
The callback parameter should specify a function
that looks like this:
If you specify the callback parameter, it should
specify a function that looks like this:
function(Window window) {...});
-
window
(
optional
Window
array of
paramType
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
Events
onCreated
chrome.windows.onCreated.addListener(function(Window window) {...});
Undocumented.
Fired when a window is created.
Parameters
-
window
(
optional
Window
array of
paramType
)
-
Undocumented.
- Details of the window that was created.
-
onFocusChanged
chrome.windows.onFocusChanged.addListener(function(integer windowId) {...});
Undocumented.
Fired when the currently focused window changes.
Parameters
-
windowId
(
optional
Type
array of
integer
)
-
Undocumented.
- ID of the newly focused window.
-
onRemoved
chrome.windows.onRemoved.addListener(function(integer windowId) {...});
Undocumented.
Fired when a window is removed (closed).
Parameters
-
windowId
(
optional
Type
array of
integer
)
-
Undocumented.
- ID of the removed window.
-
Types
Window
paramName
(
optional
Type
array of
object
)
Undocumented.
Description of this parameter from the json schema.
-
id
(
optional
Type
array of
integer
)
-
Undocumented.
- The ID of the window. Window IDs are unique within a browser session.
-
-
focused
(
optional
Type
array of
boolean
)
-
Undocumented.
- Whether the window is currently the focused window.
-
-
top
(
optional
Type
array of
integer
)
-
Undocumented.
- The offset of the window from the top edge of the screen in pixels.
-
-
left
(
optional
Type
array of
integer
)
-
Undocumented.
- The offset of the window from the left edge of the screen in pixels.
-
-
width
(
optional
Type
array of
integer
)
-
Undocumented.
- The width of the window in pixels.
-
-
height
(
optional
Type
array of
integer
)
-
Undocumented.
- The height of the window in pixels.
-
-
tabs
(
optional
Type
array of
Tab
array of
paramType
paramType
)
-
Undocumented.
- Array of Tab objects representing the current tabs in the window.
-
-
incognito
(
optional
Type
array of
boolean
)
-
Undocumented.
- Whether the window is incognito.
-
-
type
(
optional
Type
array of
string
)
-
Undocumented.
- The type of browser window this is.
-