简体   繁体   中英

Hide page action for specific url

I am building a chrome extension for reddit.com and I am using page action for that. Now I want page_action icon to be visible only for a specific url format ie

http://www.reddit.com   [allowed]
http://www.reddit.com/r/*   [allowed]
http://www.reddit.com/r/books/comments/*   [not allowed]

So, as I have mentioned above that I don't want my extension page action icon to be visible for the 3rd case involving comments url of redddit .

Currently I am using the below code in my background.js to achieve this:

function check(tab_id, data, tab){
    if(tab.url.indexOf("reddit.com") > -1 && tab.url.indexOf("/comments/") == -1){
        chrome.pageAction.show(tab_id);
    }
};
chrome.tabs.onUpdated.addListener(check);

I have also added the below line in my manifest.json to disable the extension on the comment page

 "exclude_matches": ["http://www.reddit.com/r/*/comments/*"],

So, my question is this the correct/ideal way to disable & hide an extension from a specific page/url?

Why not Zoidb- I mean, Regular Expressions?

var displayPageAction = function (tabId, changeInfo, tab) {
    var regex = new RegExp(/.../); //Your regex goes here
    var match = regex.exec(tab.url); 
    // We only display the Page Action if we are inside a tab that matches
    if(match && changeInfo.status == 'complete') {
      chrome.pageAction.show(tabId);
    }
};

chrome.tabs.onUpdated.addListener(displayPageAction);

About the approach, I think using the onUpdated.addListener is the correct approach. As a good practice, try to show your page Action only when the tab has been loaded, unless your application requirements specify otherwise.

You can use this tool in order to generate your regular expression, and if you need help, feel free to ask again and we will help you assemble the regular expression you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM