8

I'm developing an extension page action that works only in a specific domain, can I add more than one link to the page action? My background.js is this.

it is possible to add more links in background.html for the extension page action?

//background.js

chrome.runtime.onInstalled.addListener(function() {
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {

  chrome.declarativeContent.onPageChanged.addRules([ 
{
  conditions: [
    new chrome.declarativeContent.PageStateMatcher({
      pageUrl: { urlContains: 'www.exemple.com' },
 })
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
Omiod
  • 11,285
  • 11
  • 53
  • 59

2 Answers2

20

Yes, you can register a page action for multiple sites by adding multiple PageStateMatchers to the list of conditions.

chrome.runtime.onInstalled.addListener(function() {
    chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
        chrome.declarativeContent.onPageChanged.addRules([{
            conditions: [
                new chrome.declarativeContent.PageStateMatcher({
                    pageUrl: { hostSuffix: 'example.com' }
                }),
                new chrome.declarativeContent.PageStateMatcher({
                    pageUrl: { hostSuffix: 'example.net' }
                }),
            ],
            actions: [ new chrome.declarativeContent.ShowPageAction() ]
       }]);
    });
});

Note: I replaced urlContains with hostSuffix because you wanted to show the page action on certain domains, not on all pages whose URL contain the website's host (e.g. you probably don't want to match http://localhost/path/containing/www.example.com). See the documentation of the UrlFilter type for more ways to match pages.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Thank you helped a lot, sorry for my bad English I am Brazilian. – Weiller Jayceon Dec 02 '14 at 17:49
  • @WeillerJayceon No problem. As long as we can understand each other, you'll be fine. [If you think that an answer has solved your question, click on the green checkmark at the left of an answer to mark it as "accepted"](https://stackoverflow.com/help/accepted-answer). – Rob W Dec 02 '14 at 17:52
0

With regular expressions you can match multiple domains with a single rule.

For example the following matches any Google search page in every country.

pageUrl: { urlMatches: '^(www\.)?(google.).*\/search\?', schemes: ['https'] },
Omiod
  • 11,285
  • 11
  • 53
  • 59