madcapnmckay
madcapnmckay

Reputation: 15984

Display Page Action Icon on specific url

I'm trying to get my page action icon to show on a specific url. I've tried implementing the examples here but these require the trunk/dev release.

The current code I have is taken from a SO answer. But this doesn't seem to work because the tab object never has a url property in my testing to be able to restrict on.

// background.js

function checkURL(tabId, info, tab) {
    if (info.status === "complete") {
        if (tab.url) {
            // restrict here
            chrome.pageAction.show(tabId);
        }
    }
}
chrome.tabs.onUpdated.addListener(checkURL);

// manifest

{
    "manifest_version": 2,

    "name": "My first extension",
    "version": "1.0",

    "content_scripts": [
        {
            "matches": ["http://www.google.com/*"],
            "js": [
                "script.js"
            ],
            "run_at": "document_idle"
        }
    ],

    "background": {
       "page": "background.html",
       "persistent": false
    },

    "page_action": {
        "default_icon": "icon.png"
    }
}

What am I doing wrong?

Upvotes: 2

Views: 2252

Answers (2)

Daniel Garcia-Briseno
Daniel Garcia-Briseno

Reputation: 339

I'm late to answer this, but this may help anyone else having the same issue. I just spent about 20 minutes looking for it for my own extension.

Look here https://developer.chrome.com/extensions/declarativeContent

Add this to your manifest.json

"background" : {
    "scripts": ["background.js"]
}
"permissions" : [
    "declarativeContent"
]

Then in background.js

var rule1 = {
    conditions: [
      new chrome.declarativeContent.PageStateMatcher({
        // If I wanted my extension to work only on SO I would put
        // hostContains: 'stackoverflow.com'
        // You can check out the link above for more options for the rules
        pageUrl: { hostContains: 'some string' } 
      })
    ],
    actions: [ new chrome.declarativeContent.ShowPageAction() ]
};
chrome.runtime.onInstalled.addListener(function (details) {
    chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
        chrome.declarativeContent.onPageChanged.addRules([rule1])
    })
})

Upvotes: 2

Maciej Jankowski
Maciej Jankowski

Reputation: 2834

This works for me:

//background.js
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  if (~tab.url.indexOf('.pl')) {
    chrome.pageAction.show(tabId);
  }
});

//manifest.json
"permissions": [
"tabs"
]

and I'm not using persistent:false

Upvotes: 2

Related Questions