Welcome What's new in Chrome extensions Getting started API Reference Samples
Welcome What's new in Chrome extensions Getting started API Reference Samples

chrome.action

  • Description

    Use the chrome.action API to control the extension's icon in the Google Chrome toolbar.

  • Availability
    Since Chrome 64. Manifest V3 and above.

You can use the chrome.action API to control the toolbar button for your extension in Chrome's UI. The action icons are displayed in the browser toolbar, to the right of the omnibox (on left-to-right devices). After installation, by default, these appear in the extensions menu (the puzzle piece). Users can choose to pin your extension icon to the toolbar.

Note that every extension will have an icon in the toolbar in Chrome, even if the action key is not specified.

Manifest #

In order to use the chrome.action API, you need to specify a "manifest_version" of 3 or higher and include the action key in your manifest file.

{
"name": "Action Extension",
...
"action": {
"default_icon": { // optional
"16": "images/icon16.png", // optional
"24": "images/icon24.png", // optional
"32": "images/icon32.png" // optional
},
"default_title": "Click Me", // optional, shown in tooltip
"default_popup": "popup.html" // optional
},
...
}

Each of these values is optional; an empty dictionary is technically allowed.

These properties are described more below.

Parts of the UI #

Icon #

The icon is the main image used in the toolbar button. Icons are 16 DIPs (device-independent pixels) wide and tall. The icon is initially set by the default_icon key in the action entry in the manifest.json file. This key is a dictionary of sizes to image paths. Chrome will use these icons to choose which image scale to use. If an exact match is not found, Chrome will select the closest available and scale it to fit the image. However, this scaling can cause the icon to lose detail or look fuzzy.

Since devices with less-common scale factors like 1.5x or 1.2x are becoming more common, you are encouraged to provide multiple sizes for your icons. This also ensures that if the icon display size is ever changed, you don't need to do any more work to provide different icons.

The icon can also be set programmatically using the action.setIcon() method. This can be used to specify a different image path or to provide a dynamically-generated icon using the HTML canvas element, or, if setting from an extension service worker, the offscreen canvas API.

const canvas = new OffscreenCanvas(16, 16);
const context = canvas.getContext('2d');
context.clearRect(0, 0, 16, 16);
context.fillStyle = '#00FF00'; // Green
context.fillRect(0, 0, 16, 16);
const imageData = context.getImageData(0, 0, 16, 16);
chrome.action.setIcon({imageData: imageData}, () => { ... });

The action.setIcon() API is intended to set a static image. It should not be used to simulate animation.

Formats #

For packed extensions (installed from a .crx file), images can be in most formats that the Blink rendering engine can display, including PNG, JPEG, BMP, ICO, and others (SVG is not supported). Unpacked extensions must use images in the PNG format.

Tooltip (title) #

The tooltip, or title, appears when the user hovers the mouse of the extension's icon in the toolbar. It is also included in the accessible text spoken by screenreaders when the button gets focus.

The default tooltip is set from the default_title field in the action manifest.json file. You can also set it programmatically with the action.setTitle() method.

Badge #

Actions can optionally display a "badge" — a bit of text layered over the icon. This makes it easy to update the action to display a small amount of information about the state of the extension, such as a counter. The badge has a text component and a background color.

Note that the badge has limited space, and should typically use four characters or fewer.

The badge does not have a default taken from the manifest; you can set it programmatically with action.setBadgeBackgroundColor() and action.setBadgeText(). When setting the color, the values can be either an array of four integers in the range of 0 - 255 that make up the RGBA color of the badge or a string with a CSS hex color value.

chrome.action.setBadgeBackgroundColor(
{color: [0, 255, 0, 0]}, // Green
() => { ... });

chrome.action.setBadgeBackgroundColor(
{color: '#00FF00'} // Also green
() => { ... });

An action's popup will be shown when the user clicks on the extension's action button in the toolbar. The popup can contain any HTML contents you like, and will be automatically sized to fit its contents. The popup cannot be smaller than 25x25 and cannot be larger than 800x600.

The popup is initially set from the default_popup property in the action key in the manifest.json file. If present, this should point to a relative path within the extension directory. It can also be updated dynamically to point to a different relative path using the action.setPopup() method.

The action.onClicked event will not be dispatched if the extension action has specified a popup to show on click on the current tab.

Per-tab state #

Extension actions can have different states for each tab. For instance, you could set the badge text to be different on each tab (to show tab-specific state). You can set the value for an individual tab using the tabId property in the various setting methods on the action API. For instance, to set the badge text on a specific tab, you would do something like the following:

const tabId = getTabId();
const tabMessage = getTabMessage(tabId);
chrome.action.setBadgeText(
{text: tabMessage, tabId: tabId},
() => { ... });

If the tabId property is omitted, the setting is treated as a global setting. Tab-specific settings take priority over any global settings.

Enabled state #

By default, toolbar actions are enabled (clickable) on every tab. You can control this using the action.enable() and action.disable() methods. This only affects whether the popup (if any) or action.onClicked event is dispatched to your extension; it does not affect the action's presence in the toolbar.

Summary

Types

TabDetails

Since Chrome 88.

Properties

  • tabId
    number optional

    The ID of the tab to query state for. If no tab is specified, the non-tab-specific state is returned.

UserSettings

The collection of user-specified settings relating to an extension's action.

Properties

  • isOnToolbar
    boolean

    Whether the extension's action icon is visible on browser windows' top-level toolbar (i.e., whether the extension has been 'pinned' by the user).

Methods

disable

action.disable(tabId: number, callback: function)

Disables the action for a tab.

Parameters

  • tabId
    number

    The id of the tab for which you want to modify the action.

  • callback
    function

    Since Chrome 67.

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

    () => {...}

enable

action.enable(tabId: number, callback: function)

Enables the action for a tab. By default, actions are enabled.

Parameters

  • tabId
    number

    The id of the tab for which you want to modify the action.

  • callback
    function

    Since Chrome 67.

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

    () => {...}

getBadgeBackgroundColor

action.getBadgeBackgroundColor(details: TabDetails, callback: function)

Gets the background color of the action.

Parameters

getBadgeText

action.getBadgeText(details: TabDetails, callback: function)

Gets the badge text of the action. If no tab is specified, the non-tab-specific badge text is returned. If displayActionCountAsBadgeText is enabled, a placeholder text will be returned unless the declarativeNetRequestFeedback permission is present or tab-specific badge text was provided.

Parameters

  • details
  • callback
    function

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

    (result: string) => {...}
    • result
      string

getPopup

action.getPopup(details: TabDetails, callback: function)

Gets the html document set as the popup for this action.

Parameters

  • details
  • callback
    function

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

    (result: string) => {...}
    • result
      string

getTitle

action.getTitle(details: TabDetails, callback: function)

Gets the title of the action.

Parameters

  • details
  • callback
    function

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

    (result: string) => {...}
    • result
      string

getUserSettings

action.getUserSettings(callback: function)

Returns the user-specified settings relating to an extension's action.

Parameters

  • callback
    function

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

    (userSettings: UserSettings) => {...}

setBadgeBackgroundColor

action.setBadgeBackgroundColor(details: object, callback: function)

Sets the background color for the badge.

Parameters

  • details
    object
    • An array of four integers in the range [0,255] that make up the RGBA color of the badge. For example, opaque red is [255, 0, 0, 255]. Can also be a string with a CSS value, with opaque red being #FF0000 or #F00.

    • tabId
      number optional

      Limits the change to when a particular tab is selected. Automatically resets when the tab is closed.

  • callback
    function

    Since Chrome 67.

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

    () => {...}

setBadgeText

action.setBadgeText(details: object, callback: function)

Sets the badge text for the action. The badge is displayed on top of the icon.

Parameters

  • details
    object
    • tabId
      number optional

      Limits the change to when a particular tab is selected. Automatically resets when the tab is closed.

    • text
      string

      Any number of characters can be passed, but only about four can fit in the space.

  • callback
    function

    Since Chrome 67.

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

    () => {...}

setIcon

action.setIcon(details: object, callback: function)

Sets the icon for the action. The icon can be specified either as the path to an image file or as the pixel data from a canvas element, or as dictionary of either one of those. Either the path or the imageData property must be specified.

Parameters

  • details
    object
    • imageData
      browserAction.ImageDataType | object optional

      Either an ImageData object or a dictionary {size -> ImageData} representing icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals scale, then image with size scale * n will be selected, where n is the size of the icon in the UI. At least one image must be specified. Note that 'details.imageData = foo' is equivalent to 'details.imageData = {'16': foo}'

    • path
      string | object optional

      Either a relative image path or a dictionary {size -> relative image path} pointing to icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals scale, then image with size scale * n will be selected, where n is the size of the icon in the UI. At least one image must be specified. Note that 'details.path = foo' is equivalent to 'details.path = {'16': foo}'

    • tabId
      number optional

      Limits the change to when a particular tab is selected. Automatically resets when the tab is closed.

  • callback
    function

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

    () => {...}

setPopup

action.setPopup(details: object, callback: function)

Sets the html document to be opened as a popup when the user clicks on the action's icon.

Parameters

  • details
    object
    • popup
      string

      The html file to show in a popup. If set to the empty string (''), no popup is shown.

    • tabId
      number optional

      Limits the change to when a particular tab is selected. Automatically resets when the tab is closed.

  • callback
    function

    Since Chrome 67.

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

    () => {...}

setTitle

action.setTitle(details: object, callback: function)

Sets the title of the action. This shows up in the tooltip.

Parameters

  • details
    object
    • tabId
      number optional

      Limits the change to when a particular tab is selected. Automatically resets when the tab is closed.

    • title
      string

      The string the action should display when moused over.

  • callback
    function

    Since Chrome 67.

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

    () => {...}

Events

onClicked

action.onClicked.addListener(listener: function)

Fired when an action icon is clicked. This event will not fire if the action has a popup.

Event

  • listener
    function

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

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