Chrome 页面操作不会在点击时打开弹出窗口

Chrome page action not open popup on click

我有此代码,仅在访问某个网站时才激活扩展程序。我注意到如果 url 与设置的条件不匹配,并且当访问所需的网站并且 url 匹配时,如果用户点击扩展图标,弹出窗口将不会打开。我该如何解决?


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

chrome.pageAction.onClicked.addListener( () => {
  chrome.windows.create({
    url: chrome.runtime.getURL('popup.html'),
    width: 500,
    height: 295,
    type: 'popup'
  });
});

您的 hostEquals 规则永远不会匹配任何内容,因为根据 the documentation 它与 URL 的主机部分进行比较,例如只是 www.example.com 所以它不能有 /*。请注意 chrome.declarativeContent 使用自己的过滤系统,它不支持 content_scripts 或 webRequest 使用的任何常用匹配模式。

解决方案 1:

{ hostEquals: 'www.example.com', pathPrefix: '/video/', schemes: ['https'] }

解决方案 2:

{ urlPrefix: 'https://www.example.com/video/' }