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() ]
}
]);
Yes, you can register a page action for multiple sites by adding multiple PageStateMatcher
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With