paramName
( optional Type array of paramType )
Undocumented.
Description of this parameter from the json schema.

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 Type array of 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
( Type array of string )
Description of the error that has taken place.

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 Type array of string )
The extension ID of the extension you want to connect to. If omitted, default is your own extension.
connectInfo
( optional Type array of object )
Undocumented.
Description of this parameter from the json schema.
name
( optional Type array of string )
Will be passed into onConnect for extension processes that are listening for the connection event.

Returns

paramName
( Port array of paramType )
Port through which messages can be sent and received with the extension.

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(Type param1, Type param2) {...});

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 backround page.

Parameters

Returns

paramName
( Type array of DOMWindow )
Undocumented.
Description of this parameter from the json schema.

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(Type param1, Type param2) {...});

getExtensionTabs

array of DOMWindow chrome.extension.getExtensionTabs(, integer windowId)

Returns an array of the JavaScript 'window' objects for each of the tabs running inside the current extension. If windowId is specified, returns only the 'window' objects of tabs attached to the specified window.

Parameters

windowId
( optional Type array of integer )
Undocumented.
Description of this parameter from the json schema.

Returns

paramName
( Type array of Type array of DOMWindow paramType )
Array of global window objects

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(Type param1, Type param2) {...});

getURL

string chrome.extension.getURL(, string path)

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

Parameters

path
( Type array of string )
A path to a resource within an extension expressed relative to it's install directory.

Returns

paramName
( Type array of string )
The fully-qualified URL to the resource.

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(Type param1, Type param2) {...});

getViews

array of DOMWindow chrome.extension.getViews(, )

Returns an array of the JavaScript 'window' objects for each of the pages running inside the current extension. This includes background pages and tabs.

Parameters

Returns

paramName
( Type array of Type array of DOMWindow paramType )
Array of global objects

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(Type param1, Type param2) {...});

sendRequest

void 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.

Parameters

extensionId
( optional Type array of string )
The extension ID of the extension you want to connect to. If omitted, default is your own extension.
request
( Type array of any )
Undocumented.
Description of this parameter from the json schema.
responseCallback
( 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(any response) {...});
response
( Type array of any )
The JSON response object sent by the handler of the request.

Events

onConnect

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

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

Parameters

port
( Port array of paramType )
Undocumented.
Description of this parameter from the json schema.

onConnectExternal

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

Fired when a connection is made from another extension.

Parameters

port
( Port array of paramType )
Undocumented.
Description of this parameter from the json schema.

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.

Parameters

request
( Type array of any )
The request sent by the calling script.
sender
( MessageSender array of paramType )
Undocumented.
Description of this parameter from the json schema.
sendResponse
( Type array of function )
Function to call when you have a response. The argument should be any JSON-ifiable object, or undefined if there is no response.

onRequestExternal

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

Fired when a request is sent from another extension.

Parameters

request
( Type array of any )
The request sent by the calling script.
sender
( MessageSender array of paramType )
Undocumented.
Description of this parameter from the json schema.
sendResponse
( Type array of 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

Event

paramName
( Type array of object )
An object which allows the addition and removal of listeners for a Chrome event.
addListener
( Type array of function )
Undocumented.
Description of this parameter from the json schema.
removeListener
( Type array of function )
Undocumented.
Description of this parameter from the json schema.
hasListener
( Type array of function )
Undocumented.
Description of this parameter from the json schema.
hasListeners
( Type array of function )
Undocumented.
Description of this parameter from the json schema.

Port

paramName
( Type array of object )
An object which allows two way communication with other pages.
name
( Type array of string )
Undocumented.
Description of this parameter from the json schema.
onDisconnect
( Event array of paramType )
Undocumented.
Description of this parameter from the json schema.
onMessage
( Event array of paramType )
Undocumented.
Description of this parameter from the json schema.
postMessage
( Type array of function )
Undocumented.
Description of this parameter from the json schema.
sender
( optional MessageSender array of paramType )
This property will only be present on ports passed to onConnect/onConnectExternal listeners.

MessageSender

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