Use the browser.runtime API to retrieve the background page, return details about the manifest, and listen for and respond to events in the app or extension lifecycle. You can also use this API to convert the relative path of URLs to fully-qualified URLs.

_ConnectConnectInfo
_LastError
_OnInstalledDetails
_OnUpdateAvailableDetails
_RequestUpdateCheckReturnDetails
_SendMessageOptions
BrowserInfo
MessageSender
PlatformInfo
Port
DirectoryEntry
OnInstalledReason
OnRestartRequiredReason
PlatformArch
PlatformNaclArch
PlatformOs
RequestUpdateCheckStatus
id
lastError
onBrowserUpdateAvailable
onConnect
onConnectExternal
onInstalled
onMessage
onMessageExternal
onRestartRequired
onStartup
onSuspend
onSuspendCanceled
onUpdateAvailable
connect
connectNative
getBackgroundPage
getBrowserInfo
getFrameId
getManifest
getPackageDirectoryEntry
getPlatformInfo
getURL
openOptionsPage
reload
requestUpdateCheck
restart
sendMessage
sendNativeMessage
setUninstallURL
DirectoryEntry: any
OnInstalledReason: "install" | "update" | "browser_update"

The reason that this event is being dispatched.

OnRestartRequiredReason: "app_update" | "os_update" | "periodic"

The reason that the event is being dispatched. 'app_update' is used when the restart is needed because the application is updated to a newer version. 'os_update' is used when the restart is needed because the browser/OS is updated to a newer version. 'periodic' is used when the system runs for more than the permitted uptime set in the enterprise policy.

PlatformArch:
    | "aarch64"
    | "arm"
    | "ppc64"
    | "s390x"
    | "sparc64"
    | "x86-32"
    | "x86-64"
    | "noarch"

The machine's processor architecture.

PlatformNaclArch: "arm" | "x86-32" | "x86-64"
PlatformOs: "mac" | "win" | "android" | "cros" | "linux" | "openbsd"

The operating system the browser is running on.

RequestUpdateCheckStatus: "throttled" | "no_update" | "update_available"

Result of the update check.

id: string

The ID of the extension/app.

lastError: browser.runtime._LastError | undefined

This will be defined during an API method callback if there was an error

onBrowserUpdateAvailable: WebExtEvent<() => void> | undefined

Fired when an update for the browser is available, but isn't installed immediately because a browser restart is required.

Please use runtime.onRestartRequired.

onConnect: WebExtEvent<(port: Port) => void>

Fired when a connection is made from either an extension process or a content script.

onConnectExternal: WebExtEvent<(port: Port) => void>

Fired when a connection is made from another extension.

onInstalled: WebExtEvent<(details: _OnInstalledDetails) => void>

Fired when the extension is first installed, when the extension is updated to a new version, and when the browser is updated to a new version.

onMessage: WebExtEvent<
    (
        message: any,
        sender: MessageSender,
        sendResponse: (response?: any) => void,
    ) => boolean | Promise<any> | void,
>

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

The message sent by the calling script.

Function to call (at most once) when you have a response. The argument should be any JSON-ifiable object. If you have more than one onMessage listener in the same document, then only one may send a response. This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).

Return true from the event listener if you wish to call sendResponse after the event listener returns.

onMessageExternal: WebExtEvent<
    (
        message: any,
        sender: MessageSender,
        sendResponse: (response?: any) => void,
    ) => boolean | Promise<any> | void,
>

Fired when a message is sent from another extension/app. Cannot be used in a content script.

The message sent by the calling script.

Function to call (at most once) when you have a response. The argument should be any JSON-ifiable object. If you have more than one onMessage listener in the same document, then only one may send a response. This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).

Return true from the event listener if you wish to call sendResponse after the event listener returns.

onRestartRequired:
    | WebExtEvent<(reason: OnRestartRequiredReason) => void>
    | undefined

Fired when an app or the device that it runs on needs to be restarted. The app should close all its windows at its earliest convenient time to let the restart to happen. If the app does nothing, a restart will be enforced after a 24-hour grace period has passed. Currently, this event is only fired for Chrome OS kiosk apps.

The reason that the event is being dispatched.

Unsupported on Firefox at this time.

onStartup: WebExtEvent<() => void>

Fired when a profile that has this extension installed first starts up. This event is not fired for incognito profiles.

onSuspend: WebExtEvent<() => void>

Sent to the event page just before it is unloaded. This gives the extension opportunity to do some clean up. Note that since the page is unloading, any asynchronous operations started while handling this event are not guaranteed to complete. If more activity for the event page occurs before it gets unloaded the onSuspendCanceled event will be sent and the page won't be unloaded.

onSuspendCanceled: WebExtEvent<() => void>

Sent after onSuspend to indicate that the app won't be unloaded after all.

onUpdateAvailable: WebExtEvent<(details: _OnUpdateAvailableDetails) => void>

Fired when an update is available, but isn't installed immediately because the app is currently running. If you do nothing, the update will be installed the next time the background page gets unloaded, if you want it to be installed sooner you can explicitly call runtime.reload. If your extension is using a persistent background page, the background page of course never gets unloaded, so unless you call runtime.reload manually in response to this event the update will not get installed until the next time the browser itself restarts. If no handlers are listening for this event, and your extension has a persistent background page, it behaves as if runtime.reload is called in response to this event.

The manifest details of the available update.

  • Attempts to connect to connect listeners within an extension/app (such as the background page), or other extensions/apps. This is useful for content scripts connecting to their extension processes, inter-app/extension communication, and web messaging. Note that this does not connect to any listeners in a content script. Extensions may connect to content scripts embedded in tabs via tabs.connect.

    Returns Port

    Port through which messages can be sent and received. The port's runtime.Port onDisconnect event is fired if the extension/app does not exist.

  • Attempts to connect to connect listeners within an extension/app (such as the background page), or other extensions/apps. This is useful for content scripts connecting to their extension processes, inter-app/extension communication, and web messaging. Note that this does not connect to any listeners in a content script. Extensions may connect to content scripts embedded in tabs via tabs.connect.

    Parameters

    • extensionId: string

      The ID of the extension or app to connect to. If omitted, a connection will be attempted with your own extension. Required if sending messages from a web page for web messaging.

    • OptionalconnectInfo: browser.runtime._ConnectConnectInfo

    Returns Port

    Port through which messages can be sent and received. The port's runtime.Port onDisconnect event is fired if the extension/app does not exist.

  • Attempts to connect to connect listeners within an extension/app (such as the background page), or other extensions/apps. This is useful for content scripts connecting to their extension processes, inter-app/extension communication, and web messaging. Note that this does not connect to any listeners in a content script. Extensions may connect to content scripts embedded in tabs via tabs.connect.

    Parameters

    Returns Port

    Port through which messages can be sent and received. The port's runtime.Port onDisconnect event is fired if the extension/app does not exist.

  • Connects to a native application in the host machine.

    Not allowed in: Devtools pages

    Parameters

    • application: string

      The name of the registered application to connect to.

    Returns Port

    Port through which messages can be sent and received with the application

  • Retrieves the JavaScript 'window' object for the background page running inside the current extension/app. If the background page is an event page, the system will ensure it is loaded before calling the callback. If there is no background page, an error is set.

    Returns Promise<Window>

  • Returns information about the current browser.

    Returns Promise<BrowserInfo>

  • Get the frameId of any window global or frame element.

    Parameters

    • target: any

      A WindowProxy or a Browsing Context container element (IFrame, Frame, Embed, Object) for the target frame.

    Returns number

    The frameId of the target frame, or -1 if it doesn't exist.

  • Returns details about the app or extension from the manifest. The object returned is a serialization of the full manifest file.

    Returns WebExtensionManifest

  • Returns a DirectoryEntry for the package directory.

    Returns Promise<any>

    Unsupported on Firefox at this time.

  • Returns information about the current platform.

    Returns Promise<PlatformInfo>

  • Converts a relative path within an app/extension install directory to a fully-qualified URL.

    Parameters

    • path: string

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

    Returns string

    The fully-qualified URL to the resource.

  • Open your Extension's options page, if possible.

    The precise behavior may depend on your manifest's options_ui or options_page key, or what the browser happens to support at the time.

    If your Extension does not declare an options page, or the browser failed to create one for some other reason, the callback will set lastError.

    Returns Promise<void>

  • Reloads the app or extension.

    Returns void

  • Requests an update check for this app/extension.

    Returns Promise<object>

    Unsupported on Firefox at this time.

  • Restart the device when the app runs in kiosk mode. Otherwise, it's no-op.

    Returns void

    Unsupported on Firefox at this time.

  • Sends a single message to event listeners within your extension/app or a different extension/app. Similar to runtime.connect but only sends a single message, with an optional response. If sending to your extension, the runtime.onMessage event will be fired in each page, or runtime.onMessageExternal, if a different extension. Note that extensions cannot send messages to content scripts using this method. To send messages to content scripts, use tabs.sendMessage.

    Parameters

    Returns Promise<any>

  • Sends a single message to event listeners within your extension/app or a different extension/app. Similar to runtime.connect but only sends a single message, with an optional response. If sending to your extension, the runtime.onMessage event will be fired in each page, or runtime.onMessageExternal, if a different extension. Note that extensions cannot send messages to content scripts using this method. To send messages to content scripts, use tabs.sendMessage.

    Parameters

    • extensionId: string

      The ID of the extension/app to send the message to. If omitted, the message will be sent to your own extension/app. Required if sending messages from a web page for web messaging.

    • message: any
    • Optionaloptions: browser.runtime._SendMessageOptions

    Returns Promise<any>

  • Send a single message to a native application.

    Not allowed in: Devtools pages

    Parameters

    • application: string

      The name of the native messaging host.

    • message: any

      The message that will be passed to the native messaging host.

    Returns Promise<any>

  • Sets the URL to be visited upon uninstallation. This may be used to clean up server-side data, do analytics, and implement surveys. Maximum 1023 characters.

    Parameters

    • Optionalurl: string

      URL to be opened after the extension is uninstalled. This URL must have an http: or https: scheme. Set an empty string to not open a new tab upon uninstallation.

    Returns Promise<void>