Volomike
Volomike

Reputation: 24886

Detect Chrome Extension Pinned in Javascript

The latest Chrome browser now shows a puzzle icon and doesn't automatically pin your Chrome Extension. Is there an API to detect if a Chrome Extension has been pinned? Can we detect from Javascript from a web page, or do we have to do the API through the extension itself? (I'm already assuming the extension itself.)

Upvotes: 7

Views: 2493

Answers (1)

Ace
Ace

Reputation: 646

Here's some code you can use to check if your extension is pinned, and if not, send the user to a particular URL.

You can put this in your Background.js:

async function checkIsPinned(){
  let userSettings = await chrome.action.getUserSettings();
  if(userSettings.isOnToolbar == false){
    chrome.tabs.create({ url: 'https://example.com'});
  }
}
//Check if extension is pinned 
checkIsPinned();

This code is adapted from https://github.com/rustyzone/is-ext-pinned

Warnings:

  • It requires Manifest V3.
  • It only works when called from pop-up script (e.g. the one that runs when user clicks the extension icon on toolbar) or from service worker.
  • It doesn't work from content script. That is, it always returns { isOnToolbar: false } regardless of whether your extension's icon is pinned or not. You need to send a request to your service worker, run the code there, and then send the response back to content script. You can do that via chrome.runtime.sendMessage or similar chrome.runtime.* methods.
  • As of 2023-01-02, it's not supported in Safari (see details).

Upvotes: 10

Related Questions