javascriptgoogle-chrome-extension

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


Solution

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