Contents
The chrome.extension
module
has utilities that can be used by any extension page.
Support for content scripts
Unlike the other chrome.* APIs,
parts of chrome.extension
can be used by content scripts:
-
connect()
andonConnect
- Communication with extension pages
-
getURL()
- Access to extension resources such as image files
For details, see Content Scripts.
API reference: chrome.extension
Methods
connect
Attempts to connect to other listeners within the extension (listeners may be toolstrips or 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.connectToTab.
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.
-
name
Returns
-
paramName
( Port )
- Port through which messages can be sent and received with the extension.
getBackgroundPage
Returns the global JavaScript object for the background page running inside the current extension. Returns null if the extension has no backround page.
Parameters
Returns
-
paramName
( object )
- Undocumented.
getExtensionTabs
Returns an array of the global JavaScript objects for each of the tab contents views running inside the current extension. If windowId is specified, returns only the tab contentses attached to the specified window.
Parameters
-
windowId
( optional integer )
- Undocumented.
Returns
-
paramName
( array of object )
- Array of global objects
getToolstrips
Returns an array of the global JavaScript objects for each of the toolstrip views running inside the current extension. If windowId is specified, returns only the toolstrips attached to the specified window.
Parameters
-
windowId
( optional integer )
- Undocumented.
Returns
-
paramName
( array of object )
- Array of global objects
getURL
Convert 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 it's install directory.
Returns
-
paramName
( string )
- The fully-qualified URL to the resource.
getViews
Returns an array of the global JavaScript objects for each of the views running inside the current extension. This includes toolstrips, background pages, and tabs.
Parameters
Returns
-
paramName
( array of object )
- Array of global objects
sendRequest
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 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.
Returns
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function(null) {...});
Events
onConnect
Fired when a connection is made from either an extension process or a content script.
Parameters
-
port
( Port )
- Undocumented.
onRequest
Fired when a request is sent from either an extension process or a content script.
Parameters
-
request
( any )
- The request sent by the calling script.
-
sender
( MessageSender )
- 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.
onRequestExternal
Fired when a request is sent from another extension.
Parameters
-
request
( any )
- The request sent by the calling script.
-
sender
( MessageSender )
- 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
Port
-
name
( string )
- Undocumented.
-
onDisconnect
( object )
- Undocumented.
-
onMessage
( object )
- Undocumented.
-
postMessage
( function )
- Undocumented.
-
sender
( optional MessageSender )
- This property will only be present on ports passed to onConnect/onConnectExternal listeners.
MessageSender
-
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.