1

I want my extension to only appear in specific website.

Details:

  • Let's say I set the url to example.com/*
  • So every time I open example.com/* my extension will appear
  • Otherwise, it should be invisible and not working

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()]
        }]);
    });
});
Xan
  • 74,770
  • 16
  • 179
  • 206
  • possible duplicate of [Limit Chrome Extension to certain URLs?](http://stackoverflow.com/questions/10504239/limit-chrome-extension-to-certain-urls) – Sebastian Paaske Tørholm Apr 26 '15 at 19:05
  • If by appear you mean that you are trying to use a standard extension UI that isn't always present, you most likely want a page action, i.e: http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/pageAction/pageaction_by_url/ – lossleader Apr 26 '15 at 19:53
  • You don't explain what you mean by "appear". How does your extension work now? – Xan Apr 26 '15 at 23:36
  • Seriously please read the documentation. Its clearly explained there. – Zig Mandel Apr 27 '15 at 00:35
  • @Xan, by appear i mean like this: - let say i set the website to google.com/* - so if i open google.com/* the extention appears - other than that they gone. – Davin Timothy Ivander Apr 27 '15 at 09:05
  • Edit your question instead of trying to fit it in comments. – Xan Apr 27 '15 at 09:06
  • @SebastianPaaskeTørholm, i did exactly like that link but it's gone and even with the right url, it wont appear at all. – Davin Timothy Ivander Apr 27 '15 at 09:08
  • _What_ appears? Your extension button? What is the current manifest of your extension, can you add that? – Xan Apr 27 '15 at 09:10
  • @Xan, done added there. yes i want my extention button to appear and work only to specific website. – Davin Timothy Ivander Apr 27 '15 at 09:13
  • 1
    You probably edited your manifest for the example, but do note that the correct match pattern for your content script is `*://google.com/*` (one less slash). I suspect it's just a typo. Edit: I edited it in the question, since it's not related to your problem. – Xan Apr 27 '15 at 09:21

1 Answers1

2

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.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • ah i see, thanks. i just re-read the developer.chrome.com when i post the question its already 3-4 am for me, maybe i just not focused enough. looks like i got a lot to learn :3. im currently code it, hope i didnt hit any more problem. – Davin Timothy Ivander Apr 27 '15 at 09:33
  • hey i got some simple problem. i create a page_action with popup.html as default_popup, and i attach a JS in there. In the JS i only put console.log("js popup loaded"). but cant see the console log. can you help me to fix it? – Davin Timothy Ivander Apr 27 '15 at 10:44
  • 1
    Where are you looking? The proper console is to be invoked with a right-click on the icon and selecting "Inspect popup". – Xan Apr 27 '15 at 10:46
  • i was wondering now. Let say i need a storage to store an array of ids and name, then my popup and my content_script need to load them both. the thing is my js in popup cannot call any function from js in my content_script, so what did you suggest i do? – Davin Timothy Ivander Apr 27 '15 at 14:32
  • what my ext do is like this: first user gonna press "add" in my popup, after that he can click 1 element in the page and after clicking it my ext gonna save the id of the element. after that everytime he open the site the element with that id gonna be highlighted(adding background color). im going to use storage with array in it and load it. – Davin Timothy Ivander Apr 27 '15 at 14:36
  • I suggest not continuing this discussion here. It's clearly a separate question. – Xan Apr 27 '15 at 14:50