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
)
Undocumented.
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
(
optional
Type
array of
string
)
-
Undocumented.
- Description of the error that has taken place.
-
Methods
connect
Port
chrome.extension.connect(, string
extensionId, object
connectInfo)
Undocumented.
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
)
-
Undocumented.
- 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
)
-
Undocumented.
- Will be passed into onConnect for extension processes that are listening for the connection event.
-
Returns
-
paramName
(
optional
Port
array of
paramType
)
-
Undocumented.
- 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(,
)
Undocumented.
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
(
optional
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)
Undocumented.
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
(
optional
Type
array of
Type
array of
DOMWindow
paramType
)
-
Undocumented.
- 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) {...});
method name
void
chrome.module.methodName(,
)
Undocumented.
A description from the json schema def of the function goes here.
Parameters
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(Type param1, Type param2) {...});
getURL
string
chrome.extension.getURL(, string
path)
Undocumented.
Converts a relative path within an extension install directory to a fully-qualified URL.
Parameters
-
path
(
optional
Type
array of
string
)
-
Undocumented.
- A path to a resource within an extension expressed relative to it's install directory.
-
Returns
-
paramName
(
optional
Type
array of
string
)
-
Undocumented.
- 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(,
)
Undocumented.
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
(
optional
Type
array of
Type
array of
DOMWindow
paramType
)
-
Undocumented.
- 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)
Undocumented.
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
)
-
Undocumented.
- The extension ID of the extension you want to connect to. If omitted, default is your own extension.
-
-
request
(
optional
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
(
optional
Type
array of
any
)
-
Undocumented.
- The JSON response object sent by the handler of the request.
-
Events
onConnect
chrome.extension.onConnect.addListener(function(Port port) {...});
Undocumented.
Fired when a connection is made from either an extension process or a content script.
Parameters
-
port
(
optional
Port
array of
paramType
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
onConnectExternal
chrome.extension.onConnectExternal.addListener(function(Port port) {...});
Undocumented.
Fired when a connection is made from another extension.
Parameters
-
port
(
optional
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) {...});
Undocumented.
Fired when a request is sent from either an extension process or a content script.
Parameters
-
request
(
optional
Type
array of
any
)
-
Undocumented.
- The request sent by the calling script.
-
-
sender
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
sendResponse
(
optional
Type
array of
function
)
-
Undocumented.
- 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) {...});
Undocumented.
Fired when a request is sent from another extension.
Parameters
-
request
(
optional
Type
array of
any
)
-
Undocumented.
- The request sent by the calling script.
-
-
sender
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
sendResponse
(
optional
Type
array of
function
)
-
Undocumented.
- 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
(
optional
Type
array of
object
)
Undocumented.
An object which allows the addition and removal of listeners for a Chrome event.
-
addListener
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
removeListener
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
hasListener
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
hasListeners
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
Port
paramName
(
optional
Type
array of
object
)
Undocumented.
An object which allows two way communication with other pages.
-
name
(
optional
Type
array of
string
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
onDisconnect
(
optional
Event
array of
paramType
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
onMessage
(
optional
Event
array of
paramType
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
postMessage
(
optional
Type
array of
function
)
-
Undocumented.
-
Description of this parameter from the json schema.
-
-
sender
-
Undocumented.
- This property will only be present on ports passed to onConnect/onConnectExternal listeners.
-
MessageSender
paramName
(
optional
Type
array of
object
)
Undocumented.
An object containing information about the script context that sent a message or request.
-
tab
(
optional
Tab
array of
paramType
)
-
Undocumented.
- This property will only be present when the connection was opened from a tab or content script.
-
-
id
(
optional
Type
array of
string
)
-
Undocumented.
- The extension ID of the extension that opened the connection.
-