SKG SKG - 1 month ago 8
JSON Question

chrome.pageAction.show() isn't working in tabs.onCreated listener

background.js:

chrome.tabs.onCreated.addListener(function(tab) {

chrome.pageAction.show(tab.id);//Doesn't work...icon is still grey

});


This is part of the code from my extension. I want every new tab to show page action whenever it's created. Unfortunately,
chrome.pageAction.show()
doesn't work. Everytime I create a new tab, its icon is grayed out.

However to my surprise, if I call
chrome.pageAction.show()
from console, it works perfectly.

manifest.json

{
"name": "show Page Action",
"description": "show icon in all tabs",
"version": "1.0",
"background": {
"scripts": ["jquery-3.1.1.min.js", "background.js"]
},
"page_action": {
"default_icon": "icon_true.png"
},
"manifest_version": 2
}

Answer

pageAction API is URL-based so you need to use tabs.onUpdated, which is executed after onCreated and has different parameters:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (changeInfo.url) {
        chrome.pageAction.show(tabId);
    }
});

Don't forget to add "tabs" in manifest.json's "permissions".

Comments