Chrome页面操作在点击时无法打开弹出窗口

时间:2020-09-15 14:32:25

标签: javascript google-chrome-extension

只有访问特定网站时,我才具有此代码来激活扩展程序。我注意到,如果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'
  });
});

1 个答案:

答案 0 :(得分:0)

您的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/' }