I'm writing my first Chrome Extension. I've used permission, but I'm seeing my button everywhere.
How can I only show the button on the addresses I'm writing the extension for?
I'm writing my first Chrome Extension. I've used permission, but I'm seeing my button everywhere.
How can I only show the button on the addresses I'm writing the extension for?
Although the answer from @Sorter works, it is not the best way to solve the problem.
First and foremost, it does not always work. If the page used
history.pushState
, the page action will disappear and not come back until you trigger theonUpdated
oronHighlighted
event again Chromium issue 231075.Secondly, the method is inefficient, because it's triggered for every update of tab state on all pages.
The most efficient and reliable way to get a page action to appear on certain domains is to use the
declarativeContent
API. This is only available since Chrome 33. Before that, thewebNavigation
API was the most suitable API. The advantage of these API over the method using thetabs
API is that you can safely use event pages, because you can declare URL filters. With these URL filters, the events will only be triggered if you navigate to a page that matches the URL filters. Consequently, your extension/event page will not be activated until really needed (= no wasted RAM or CPU).Here's a minimal example (
background.js
) using thewebNavigation
API:manifest.json
:If you target Chrome 33 and higher, then you can also use the declarativeContent API instead. Simply replace the "webNavigation" permission with "declarativeContent", and use the following background script (background.js):
In both examples, I used a UrlFilter that matches the
example.com
domain.Create background.js which checks for updated and highlighted tab.
Create popup.html and popup.js in the similar manner.
You can use the variables defined in background.js in content scripts (popup.js) with
chrome.extension.getBackgroundPage().variableName
Here's the example extention download link.
For your reference and ease, here's the sample manifest.json file
An Updated Way:
I use the following with great success: