Welcome What's new in Chrome extensions API reference Samples
Welcome What's new in Chrome extensions 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

Chrome 44+

The type of extension view.

Enum

"tab"

"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

lastError

≤MV2 Deprecated since Chrome 58

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.

Type

object

Properties

  • message

    string

    Description of the error that has taken place.

Methods

getBackgroundPage

chrome.extension.getBackgroundPage()
Foreground only

Returns the JavaScript 'window' object for the background page running inside the current extension. Returns null if the extension has no background page.

Returns

  • Window | undefined

getExtensionTabs

chrome.extension.getExtensionTabs(
  windowId?: number,
)
≤MV2 Foreground only Deprecated

Please use extension.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 optional

Returns

  • Window[]

    Array of global window objects

getURL

chrome.extension.getURL(
  path: string,
)
≤MV2 Deprecated since Chrome 58

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

  • string

    The fully-qualified URL to the resource.

getViews

chrome.extension.getViews(
  fetchProperties?: object,
)
Foreground only

Returns an array of the JavaScript 'window' objects for each of the pages running inside the current extension.

Parameters

  • fetchProperties

    object optional

    • tabId

      number optional

      Chrome 54+

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

    • windowId

      number optional

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

Returns

  • Window[]

    Array of global objects

isAllowedFileSchemeAccess

chrome.extension.isAllowedFileSchemeAccess(
  callback?: function,
)
Promise

Retrieves the state of the extension's access to the 'file://' scheme. This corresponds to the user-controlled per-extension 'Allow access to File URLs' setting accessible via the chrome://extensions page.

Parameters

  • callback

    function optional

    The callback parameter looks like: (isAllowedAccess: boolean) => void

    • isAllowedAccess

      boolean

      True if the extension can access the 'file://' scheme, false otherwise.

Returns

  • Promise<boolean>

    Chrome 99+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

isAllowedIncognitoAccess

chrome.extension.isAllowedIncognitoAccess(
  callback?: function,
)
Promise

Retrieves the state of the extension's access to Incognito-mode. This corresponds to the user-controlled per-extension 'Allowed in Incognito' setting accessible via the chrome://extensions page.

Parameters

  • callback

    function optional

    The callback parameter looks like: (isAllowedAccess: boolean) => void

    • isAllowedAccess

      boolean

      True if the extension has access to Incognito mode, false otherwise.

Returns

  • Promise<boolean>

    Chrome 99+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

sendRequest

chrome.extension.sendRequest(
  extensionId?: string,
  request: any,
  callback?: function,
)
Promise ≤MV2 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 extension.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

  • callback

    function optional

    Chrome 99+

    The callback parameter looks like: (response: any) => void

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

Returns

  • Promise<any>

    Chrome 99+

    Promises are supported in Manifest V3 and later, but callbacks are provided for backward compatibility. You cannot use both on the same function call. The promise resolves with the same type that is passed to the callback.

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(
  callback: function,
)
≤MV2 Deprecated

Please use runtime.onMessage.

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

Parameters

  • callback

    function

    The callback parameter looks like: (request: any, sender: runtime.MessageSender, sendResponse: function) => void

    • request

      any

    • sendResponse

      function

      The sendResponse parameter looks like: () => void

onRequestExternal

chrome.extension.onRequestExternal.addListener(
  callback: function,
)
≤MV2 Deprecated

Please use runtime.onMessageExternal.

Fired when a request is sent from another extension.

Parameters

  • callback

    function

    The callback parameter looks like: (request: any, sender: runtime.MessageSender, sendResponse: function) => void

    • request

      any

    • sendResponse

      function

      The sendResponse parameter looks like: () => void

This site uses cookies to deliver and enhance the quality of its services and to analyze traffic. If you agree, cookies are also used to serve advertising and to personalize the content and advertisements that you see. Learn more about our use of cookies.