为某些URL启用Chrome扩展程序?

lh80um4z  于 2023-05-27  发布在  Go
关注(0)|答案(2)|浏览(170)

我正在构建我的第一个Chrome扩展程序,想知道是否有一种方法可以在访问Google Drive时仅在工具栏上启用/显示它?
阅读开发指南,看起来我需要在manifest.json文件中设置权限或content_scripts=>匹配如下:

"permissions" : {"https://drive.google.com/", "http://drive.google.com/"}

或者这个:

"content_scripts": [
  {
    "matches": ["https://drive.google.com/", "http://drive.google.com/"],
     ...
  }

这两个似乎都不起作用,因为我的扩展显示在我访问的每个页面上。有没有其他资源我可以看看?

6vl6ewon

6vl6ewon1#

“显示在工具栏上”听起来像是在谈论页面或浏览器操作。由于您只需要一些页面,这意味着您需要一个page action。如果你想与DOM交互,那么你需要一个内容脚本。你会想要"matches": ["*://drive.google.com/*"]。第一个*等于您列出的两个主机;第二个*表示“任何带有该前缀的URL”,可能是您最初的问题。请参阅match patterns了解更多详细信息。
如果你不需要内容脚本,你可以选择在事件脚本中使用declarativeContent来显示页面操作。和"permissions": ["activeTab", "declarativeContent"]

n8ghc7c1

n8ghc7c12#

对于任何使用v3清单查看此内容的人。

chrome.runtime.onInstalled.addListener(() => {
  chrome.action.disable();

  chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
    const rules = {
      conditions: [
        new chrome.declarativeContent.PageStateMatcher({
          pageUrl: { hostSuffix: ".example.com", schemes: ["https"] },
        }),
      ],
      actions: [new chrome.declarativeContent.ShowAction()],
    };

    chrome.declarativeContent.onPageChanged.addRules([rules]);
  });
});

[Example][1] in the chrome docs

  [1]: https://developer.chrome.com/docs/extensions/reference/action/#emulating-pageactions-with-declarativecontent

相关问题