Welcome Getting started API Reference Samples
Welcome Getting started API Reference Samples

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

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

boolean optional

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

  • message
    string

    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

  • returns
    Window

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

  • windowId
    number

Returns

  • returns
    Window[]

    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

  • path
    string

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

Returns

  • returns
    string

    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

  • fetchProperties
    object
    • tabId
      number optional

      Find a view according to a tab id. If this field is omitted, returns all views.

    • type
      ViewType optional

      The type of view to get. If omitted, returns all views (including background pages and tabs). Valid values: 'tab', 'notification', 'popup'.

    • windowId
      number optional

      The window to restrict the search to. If omitted, returns all views.

Returns

  • returns
    Window[]

    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

  • callback
    function

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

    (isAllowedAccess: boolean) => {...}
    • isAllowedAccess
      boolean

      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

  • callback
    function

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

    (isAllowedAccess: boolean) => {...}
    • isAllowedAccess
      boolean

      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

  • extensionId
    string optional

    The extension ID of the extension you want to connect to. If omitted, default is your own extension.

  • request
    any
  • responseCallback
    function

    The responseCallback function looks like this:

    responseCallback(response: any) => {...}
    • 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 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

  • data
    string

Events

onRequest

chrome.extension.onRequest.addListener(listener: function)

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

Event

  • listener
    function

    Deprecated. Please use runtime.onMessage.

    The listener parameter should be a function that looks like this:

    (request: any, sender: runtime.MessageSender, sendResponse: function) => {...}
    • request
      any

      The request sent by the calling script.

    • sendResponse
      function

      The sendResponse function looks like this:

      sendResponse() => {...}

onRequestExternal

chrome.extension.onRequestExternal.addListener(listener: function)

Fired when a request is sent from another extension.

Event

  • listener
    function

    Deprecated. Please use runtime.onMessageExternal.

    The listener parameter should be a function that looks like this:

    (request: any, sender: runtime.MessageSender, sendResponse: function) => {...}
    • request
      any

      The request sent by the calling script.

    • sendResponse
      function

      The sendResponse function looks like this:

      sendResponse() => {...}
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.