You are viewing extension docs in chrome via the 'file:' scheme: are you expecting to see local changes when you refresh? You'll need run chrome with --allow-file-access-from-files.
WARNING: This is the BETA documentation. It may not work with the stable release of Chrome.
WARNING: This is unofficial documentation. It may not work with the current release of Chrome.

Google Chrome Extensions (Labs)

chrome.extension

The chrome.extension module has utilities that can be used by any extension page. It includes support for exchanging messages between an extension and its content scripts or between extensions, as described in detail in Message Passing.

Support for content scripts

Unlike the other chrome.* APIs, parts of chrome.extension can be used by content scripts:

sendRequest() and onRequest
Simple communication with extension pages
connect() and onConnect
Extended communication with extension pages
getURL()
Access to extension resources such as image files

For details, see Content Scripts.

API reference: chrome.extension

Properties

lastError

chrome.extension.lastError
lastError
( optional object )
Set for the lifetime of a callback if an ansychronous extension api has resulted in an error. If no error has occured lastError will be undefined.
message
( string )
Description of the error that has taken place.

inIncognitoContext

chrome.extension.inIncognitoContext
inIncognitoContext
( optional boolean )
True for content scripts running inside incognito tabs, and for extension pages running inside an incognito process. The latter only applies to extensions with 'split' incognito_behavior.

Methods

connect

Port chrome.extension.connect(string extensionId, object connectInfo)

Attempts to connect to other listeners within the extension (such as the extension's background page). This is primarily useful for content scripts connecting to their extension processes. Extensions may connect to content scripts embedded in tabs via chrome.tabs.connect().

Parameters

extensionId
( optional string )
The extension ID of the extension you want to connect to. If omitted, default is your own extension.
connectInfo
( optional object )
Undocumented.
name
( optional string )
Will be passed into onConnect for extension processes that are listening for the connection event.

Returns

( Port )
Port through which messages can be sent and received with the extension. The port's onDisconnect event is fired if extension does not exist.

getBackgroundPage

DOMWindow chrome.extension.getBackgroundPage()

Returns the JavaScript 'window' object for the background page running inside the current extension. Returns null if the extension has no background page.

Returns

( DOMWindow )
Undocumented.

getURL

string chrome.extension.getURL(string path)

Converts a relative path within an extension install directory to a fully-qualified URL.

Parameters

path
( string )
A path to a resource within an extension expressed relative to its install directory.

Returns

( string )
The fully-qualified URL to the resource.

getViews

array of DOMWindow chrome.extension.getViews(object fetchProperties)

Returns an array of the JavaScript 'window' objects for each of the pages running inside the current extension.

Parameters

fetchProperties
( optional object )
Undocumented.
type
( optional enumerated string ["tab", "infobar", "notification", "popup"] )
The type of view to get. If omitted, returns all views (including background pages and tabs). Valid values: 'tab', 'infobar', 'notification', 'popup'.
windowId
( optional integer )
The window to restrict the search to. If omitted, returns all views.

Returns

( array of DOMWindow )
Array of global objects

isAllowedFileSchemeAccess

chrome.extension.isAllowedFileSchemeAccess(function callback)

Retrieves the state of the extension's access to the 'file://' scheme (as determined by the user-controlled 'Allow access to File URLs' checkbox.

Parameters

callback
( function )
Undocumented.

Callback function

The callback parameter should specify a function that looks like this:

function(boolean isAllowedAccess) {...};
isAllowedAccess
( boolean )
True if the extension can access the 'file://' scheme, false otherwise.

This function was added in version 12.0.706.0. If you require this function, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

isAllowedIncognitoAccess

chrome.extension.isAllowedIncognitoAccess(function callback)

Retrieves the state of the extension's access to Incognito-mode (as determined by the user-controlled 'Allowed in Incognito' checkbox.

Parameters

callback
( function )
Undocumented.

Callback function

The callback parameter should specify a function that looks like this:

function(boolean isAllowedAccess) {...};
isAllowedAccess
( boolean )
True if the extension has access to Incognito mode, false otherwise.

This function was added in version 12.0.706.0. If you require this function, the manifest key minimum_chrome_version can ensure that your extension won't be run in an earlier browser version.

sendRequest

chrome.extension.sendRequest(string extensionId, any request, function responseCallback)

Sends a single request to other listeners within the extension. Similar to chrome.extension.connect, but only sends a single request with an optional response. The chrome.extension.onRequest event is fired in each page of the extension.

Parameters

extensionId
( optional string )
The extension ID of the extension you want to connect to. If omitted, default is your own extension.
request
( any )
Undocumented.
responseCallback
( optional function )
Undocumented.
Parameters
response
( any )
The JSON response object sent by the handler of the request. If an error occurs while connecting to the extension, the callback will be called with no arguments and chrome.extension.lastError will be set to the error message.

Callback function

If you specify the callback parameter, it should specify a function that looks like this:

function(any response) {...};
response
( any )
The JSON response object sent by the handler of the request. If an error occurs while connecting to the extension, the callback will be called with no arguments and chrome.extension.lastError will be set to the error message.

setUpdateUrlData

chrome.extension.setUpdateUrlData(string data)

Sets the value of the ap CGI parameter used in the extension's update URL. This value is ignored for extensions that are hosted in the Chrome Extension Gallery.

Parameters

data
( string )
Undocumented.

Events

onConnect

chrome.extension.onConnect.addListener(function(Port port) {...});

Fired when a connection is made from either an extension process or a content script.

Listener parameters

port
( Port )
Undocumented.

onConnectExternal

chrome.extension.onConnectExternal.addListener(function(Port port) {...});

Fired when a connection is made from another extension.

Listener parameters

port
( Port )
Undocumented.

onRequest

chrome.extension.onRequest.addListener(function(any request, MessageSender sender, function sendResponse) {...});

Fired when a request is sent from either an extension process or a content script.

Listener parameters

request
( any )
The request sent by the calling script.
sender
Undocumented.
sendResponse
( function )
Function to call (at most once) when you have a response. The argument should be any JSON-ifiable object, or undefined if there is no response. If you have more than one onRequest listener in the same document, then only one may send a response.

onRequestExternal

chrome.extension.onRequestExternal.addListener(function(any request, MessageSender sender, function sendResponse) {...});

Fired when a request is sent from another extension.

Listener parameters

request
( any )
The request sent by the calling script.
sender
Undocumented.
sendResponse
( function )
Function to call when you have a response. The argument should be any JSON-ifiable object, or undefined if there is no response.

Types

MessageSender

( object )
An object containing information about the script context that sent a message or request.
tab
( optional Tab )
This property will only be present when the connection was opened from a tab or content script.
id
( string )
The extension ID of the extension that opened the connection.

Event

( object )
An object which allows the addition and removal of listeners for a Chrome event.
addListener
( function )
Undocumented.
removeListener
( function )
Undocumented.
hasListener
( function )
Undocumented.
hasListeners
( function )
Undocumented.

Port

( object )
An object which allows two way communication with other pages.
name
( string )
Undocumented.
onDisconnect
( Event )
Undocumented.
onMessage
( Event )
Undocumented.
postMessage
( function )
Undocumented.
sender
( optional MessageSender )
This property will only be present on ports passed to onConnect/onConnectExternal listeners.