Davin Timothy Ivander
Davin Timothy Ivander

Reputation: 45

Chrome Extention Only Available in Specific Website

I want my extension to only appear in specific website.

Details:

How can I achieve that?

My extention function is to run in specific website and highlight some div with saved ids.

My current manifest:

{
  "manifest_version": 2,

  "name": "Dv Extention",
  "description": "Highlight Selected",
  "version": "1.0",
  "background":{
    "scripts":["background.js"],
    "persistent": false
  },
  "page_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html",
    "default_title": "Click Here!"
  },
  "permissions": [
    "activeTab",
    "https://ajax.googleapis.com/",
    "webNavigation"
  ],
  "content_scripts": [
    {
      "matches": ["*://example.com/*"],
      "js": ["jquery-2.1.3.min.js","main.js"]
    }
  ]
}

background.js

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

Upvotes: 1

Views: 437

Answers (1)

Xan
Xan

Reputation: 77591

lossleader's comment is correct. Page Actions are specifically designed to provide a button only in certain circumstances:

Use the chrome.pageAction API to put icons inside the address bar. Page actions represent actions that can be taken on the current page, but that aren't applicable to all pages.

Your background code is correct, however, it does not work because you forgot a permission to run it.

The example you've based this on is using Declarative Content API. It comes with a permission, so you need to add it to the manifest:

  "permissions": [
    "activeTab",
    "https://ajax.googleapis.com/",
    "webNavigation",
    "declarativeContent"
  ],

Then your page action should appear like expected.

Upvotes: 2

Related Questions