chrome.extension
- Description
The
chrome.extension
API 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.
Summary
- Types
- Properties
- Methods
chrome.extension.getBackgroundPage(): Window
chrome.extension.getExtensionTabs(windowId: number): Window[]
chrome.extension.getURL(path: string): string
chrome.extension.getViews(fetchProperties: object): Window[]
chrome.extension.isAllowedFileSchemeAccess(callback: function)
chrome.extension.isAllowedIncognitoAccess(callback: function)
chrome.extension.sendRequest(extensionId?: string, request: any, responseCallback: function)
chrome.extension.setUpdateUrlData(data: string)
- Events
Types
ViewType
The type of extension view.
Enum
"tab"
, or "popup"
Properties
inIncognitoContext
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.
Type
lastError
Deprecated. Please use runtime.lastError
.
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.
Properties
- messagestring
Description of the error that has taken place.
Methods
getBackgroundPage
chrome.extension.getBackgroundPage(): Window
Returns the JavaScript 'window' object for the background page running inside the current extension. Returns null if the extension has no background page.
Returns
- returnsWindow
getExtensionTabs
chrome.extension.getExtensionTabs(windowId: number): Window[]
Deprecated. Please use getViews
{type: "tab"}
.
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
- windowIdnumber
Returns
- returnsWindow[]
Array of global window objects
getURL
chrome.extension.getURL(path: string): string
Deprecated. Please use runtime.getURL
.
Converts a relative path within an extension install directory to a fully-qualified URL.
Parameters
- pathstring
A path to a resource within an extension expressed relative to its install directory.
Returns
- returnsstring
The fully-qualified URL to the resource.
getViews
chrome.extension.getViews(fetchProperties: object): Window[]
Returns an array of the JavaScript 'window' objects for each of the pages running inside the current extension.
Parameters
- fetchPropertiesobject
- tabIdnumber optional
Find a view according to a tab id. If this field is omitted, returns all views.
- typeViewType optional
The type of view to get. If omitted, returns all views (including background pages and tabs). Valid values: 'tab', 'notification', 'popup'.
- windowIdnumber optional
The window to restrict the search to. If omitted, returns all views.
Returns
- returnsWindow[]
Array of global objects
isAllowedFileSchemeAccess
chrome.extension.isAllowedFileSchemeAccess(callback: function)
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
- callbackfunction
The callback parameter should be a function that looks like this:
(isAllowedAccess: boolean) => {...}
- isAllowedAccessboolean
True if the extension can access the 'file://' scheme, false otherwise.
isAllowedIncognitoAccess
chrome.extension.isAllowedIncognitoAccess(callback: function)
Retrieves the state of the extension's access to Incognito-mode (as determined by the user-controlled 'Allowed in Incognito' checkbox.
Parameters
- callbackfunction
The callback parameter should be a function that looks like this:
(isAllowedAccess: boolean) => {...}
- isAllowedAccessboolean
True if the extension has access to Incognito mode, false otherwise.
sendRequest
chrome.extension.sendRequest(extensionId?: string, request: any, responseCallback: function)
Deprecated. Please use runtime.sendMessage
.
Sends a single request to other listeners within the extension. Similar to runtime.connect
, but only sends a single request with an optional response. The onRequest
event is fired in each page of the extension.
Parameters
- extensionIdstring optional
The extension ID of the extension you want to connect to. If omitted, default is your own extension.
- requestany
- responseCallbackfunction
The responseCallback function looks like this:
responseCallback(response: any) => {...}
- responseany
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
runtime.lastError
will be set to the error message.
setUpdateUrlData
chrome.extension.setUpdateUrlData(data: string)
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
- datastring
Events
onRequest
chrome.extension.onRequest.addListener(listener: function)
Fired when a request is sent from either an extension process or a content script.
Event
- listenerfunction
Deprecated. Please use
runtime.onMessage
.The listener parameter should be a function that looks like this:
(request: any, sender: runtime.MessageSender, sendResponse: function) => {...}
- requestany
The request sent by the calling script.
- sender
- sendResponsefunction
The sendResponse function looks like this:
sendResponse() => {...}
onRequestExternal
chrome.extension.onRequestExternal.addListener(listener: function)
Fired when a request is sent from another extension.
Event
- listenerfunction
Deprecated. Please use
runtime.onMessageExternal
.The listener parameter should be a function that looks like this:
(request: any, sender: runtime.MessageSender, sendResponse: function) => {...}
- requestany
The request sent by the calling script.
- sender
- sendResponsefunction
The sendResponse function looks like this:
sendResponse() => {...}