yhu420
yhu420

Reputation: 607

Browser action to activate only on "content_script" matches

I thought this would be a regularly asked question, but I can not find the answer for it. Is there a way for a chrome extension to have it's browser action popup available only on content_scripts matching pages?

Eg: I make an extension for a site. I want the popup to only be available when browsing the site (on the current tab). How do I do?

For me the browser action is always enabled by default, whatever page I'm browsing, and page action never enabled.

Thanks for your help

EDIT: Here is part of the manifest, I changed with pageAction with No success. I want my icon to activate only on tabs browsing LDLC.

{
  "content_scripts": [ 
    {
        "js": [ "jquery.min.js", "scrooge.js" ],
        "matches": [ "*://www.ldlc.com/fiche/*", "*://ldlc.com/fiche/*" ]
      }],

   "manifest_version": 2,

   "permissions": [ "storage", "activeTab","*://ldlc.com/*", "*://scroogealpha.esy.es/*" ],

   "page_action": {
          "default_title": "Worth buying?",      // optional; shown in tooltip
          "default_popup": "popup.html"        // optional
    },

   "web_accessible_resources": [ 
    ],
   "background":{
    "scripts" : ["eventPage.js"],
    "persistent": false
   }
}

Upvotes: 3

Views: 1208

Answers (1)

Forivin
Forivin

Reputation: 15498

Use a page action and in your background script listen for tab changes. When a tab url changes, check if you want to show the page action.

In your background script do something like this:

let matches = ["://www.ldlc.com/fiche/", "://ldlc.com/fiche/" ]
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    for (let i in matches) {
        if (tab.url.includes(matches[i])) {
            chrome.pageAction.show(tabId)
            break
        }
    }
})

Also add the tabs permission in your manifest.

Upvotes: 2

Related Questions