Contents
Use the chrome.tabs
module
to interact with the browser's tab system.
You can use this module to
create, modify, and rearrange tabs in the browser.
Manifest
You must declare the "tabs" permission in your extension's manifest to use the tabs API. For example:
{ "name": "My extension that uses tabs", "version": "0.1", "permissions": [ "tabs" ] }
API reference: chrome.tabs
Methods
captureVisibleTab
Captures the visible area of the currently selected tab in the specified window.
Parameters
-
windowId
( optional integer )
- The target window. Defaults to the current window.
-
callback
( function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(string dataUrl) {...});
-
dataUrl
( string )
- A data URL of a JPEG encoding of the visible area of the captured tab. May be assigned to the 'src' property of an HTML Image element for display.
connect
Connects to the content script(s) in the specified tab. The chrome.extension.onConnect event is fired in each content script running in the specified tab for the current extension. For more details, see Content Script Messaging.
Parameters
-
tabId
( integer )
- Undocumented.
-
connectInfo
( optional object )
- Undocumented.
-
-
name
( optional string )
- Will be passed into onConnect for content scripts that are listening for the connection event.
-
name
Returns
-
paramName
( Port )
- A port that can be used to communicate with the content scripts running in the specified tab.
create
Creates a new tab.
Parameters
-
createProperties
( object )
- Undocumented.
-
-
windowId
( optional integer )
- The window to create the new tab in. Defaults to the current window.
-
index
( optional integer )
- The position the tab should take in the window. The provided value will be clamped to between zero and the number of tabs in the window.
-
url
( optional string )
- The URL to navigate the tab to initially. 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.
-
selected
( optional boolean )
- Whether the tab should become the selected tab in the window. Defaults to true
-
windowId
-
callback
( optional function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(Tab tab) {...});
-
tab
( Tab )
- Details about the created tab. Will contain the ID of the new tab.
detectLanguage
Detects the primary language of the content in a tab.
Parameters
-
tabId
( optional integer )
- Defaults to the selected tab of the current window.
-
callback
( function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(string language) {...});
-
language
( string )
- An ISO language code like en or fr. For the complete list of languages supported by this method, see 2nd column of kLanguageInfoTable in http://src.chromium.org/viewvc/chrome/trunk/src/third_party/cld/bar/toolbar/cld/i18n/languages/internal/languages.cc
executeScript
Undocumented.
Parameters
-
tabId
( optional integer )
- The id of tab which run the script, default to selected tab of current window.
-
scriptDef
( optional object )
- Note:scripts are injected in the following definition order.
-
-
code
( optional string )
- JavaScript code to execute.
-
file
( optional string )
- JavaScript file to execute.
-
code
-
callback
( optional function )
- When all scripts are executed, this callback is called.
Returns
-
paramName
( boolean )
- Whether this call is successful
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function() {...});
get
Retrieves details about the specified tab.
Parameters
-
tabId
( integer )
- Undocumented.
-
callback
( function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(Tab tab) {...});
-
tab
( Tab )
- Undocumented.
getAllInWindow
Gets details about all tabs in the specified window.
Parameters
-
windowId
( optional integer )
- Defaults to the current window.
-
callback
( function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(array of Tab tabs) {...});
-
tabs
( array of Tab )
- Undocumented.
getSelected
Gets the tab that is selected in the specified window.
Parameters
-
windowId
( optional integer )
- Defaults to the current window.
-
callback
( function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(Tab tab) {...});
-
tab
( Tab )
- Undocumented.
insertCSS
Undocumented.
Parameters
-
tabId
( optional integer )
- The id of tab which run the script, default to selected tab of current window.
-
scriptDef
( optional object )
- Note:css are injected in the following definition order.
-
-
code
( optional string )
- CSS code to be injected.
-
file
( optional string )
- CSS file to be injected.
-
code
-
callback
( optional function )
- When all css are inserted, this callback is called.
Returns
-
paramName
( boolean )
- Whether this call is successful
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function() {...});
move
Move a tab to a new position within its window, or to a new window.
Parameters
-
tabId
( integer )
- Undocumented.
-
moveProperties
( object )
- Undocumented.
-
-
windowId
( optional integer )
- Defaults to the window the tab is currently in.
-
index
( integer )
- The position to move the window to. The provided value will be clamped to between zero and the number of tabs in the window.
-
windowId
-
callback
( optional function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(Tab tab) {...});
-
tab
( Tab )
- Details about the moved tab.
remove
Closes a tab.
Parameters
-
tabId
( integer )
- Undocumented.
-
callback
( optional function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function() {...});
sendRequest
Sends a single request to the content script(s) in the specified tab, with an optional callback to run when a response is sent back. The chrome.extension.onRequest event is fired in each content script running in the specified tab for the current extension.
Parameters
-
tabId
( integer )
- Undocumented.
-
request
( any )
- Undocumented.
-
responseCallback
( optional function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(null) {...});
update
Modify the properties of a tab. Properties that are not specified in updateProperties are not modified.
Parameters
-
tabId
( integer )
- Undocumented.
-
updateProperties
( object )
- Undocumented.
-
-
url
( optional string )
- Undocumented.
-
selected
( optional boolean )
- Undocumented.
-
url
-
callback
( optional function )
- Undocumented.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(Tab tab) {...});
-
tab
( Tab )
- Details about the updated tab.
Events
onAttached
Fired when a tab is attached to a window, for example because it was moved between windows.
Parameters
-
tabId
( integer )
- Undocumented.
-
attachInfo
( object )
- Undocumented.
-
-
newWindowId
( integer )
- Undocumented.
-
newPosition
( integer )
- Undocumented.
-
newWindowId
onDetached
Fired when a tab is detached from a window, for example because it is being moved between windows.
Parameters
-
tabId
( integer )
- Undocumented.
-
detachInfo
( object )
- Undocumented.
-
-
oldWindowId
( integer )
- Undocumented.
-
oldPosition
( integer )
- Undocumented.
-
oldWindowId
onMoved
Fires when a tab is moved within a window. Only one move event is fired, representing the tab the user directly moved. Move events are not fired for the other tabs that must move in response. This event is not fired when a tab is moved between windows. For that, see onDetached.
Parameters
-
tabId
( integer )
- Undocumented.
-
moveInfo
( object )
- Undocumented.
-
-
windowId
( integer )
- Undocumented.
-
fromIndex
( integer )
- Undocumented.
-
toIndex
( integer )
- Undocumented.
-
windowId
onRemoved
Fires when a tab is closed.
Parameters
-
tabId
( integer )
- Undocumented.
onSelectionChanged
Fires when the selected tab in a window changes.
Parameters
-
tabId
( integer )
- The ID of the tab that has become selected.
-
selectInfo
( object )
- Undocumented.
-
-
windowId
( integer )
- The ID of the window the selected tab changed inside of.
-
windowId
onUpdated
Fires when a tab is updated.
Parameters
-
tabId
( integer )
- Undocumented.
-
changeInfo
( object )
- Undocumented.
-
-
status
( string )
- The status of the tab. Can be either loading or complete.
-
url
( optional string )
- Only specified if the tab's URL changed.
-
status
Types
Tab
-
id
( integer )
- The ID of the tab. Tab IDs are unique within a browser session.
-
index
( integer )
- The zero-based index of the tab within its window.
-
windowId
( integer )
- The ID of the window the tab is contained within.
-
selected
( boolean )
- Whether the tab is selected.
-
url
( string )
- The URL the tab is displaying.
-
title
( optional string )
- The title of the tab. This may not be available if the tab is loading.
-
favIconUrl
( optional string )
- The URL of the tab's favicon. This may not be available if the tab is loading.
-
status
( optional string )
- Either loading or complete.