如何在扩展页面操作上添加更多链接

Wei*_*eon 6 google-chrome google-chrome-extension

我正在开发一个仅在特定域中有效的扩展页面操作,我可以添加多个链接到页面操作?我的background.js就是这个.

可以在background.html中为扩展页面操作添加更多链接吗?

//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() ]
}
]);
Run Code Online (Sandbox Code Playgroud)

Rob*_*b W 14

是的,您可以通过PageStateMatcher在列表中添加多个s来为多个站点注册页面操作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() ]
       }]);
    });
});
Run Code Online (Sandbox Code Playgroud)

注意:我替换urlContainshostSuffix因为您想在某些域上显示页面操作,而不是在URL包含网站主机的所有页面上显示(例如,您可能不想匹配http://localhost/path/containing/www.example.com).有关匹配页面的更多方法,请参阅UrlFilter类型的文档.