首页 > 解决方案 > 仅为主机启用 chrome 扩展

问题描述

嗨,我正在创建我的第一个 chrome 扩展,这是我的 minifest 文件

{
  "name": "Get pages source",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Get pages source from a popup",
  "browser_action": {
    "default_icon": {
      "16": "images/get_started16.png",
      "32": "images/get_started32.png",
      "48": "images/get_started48.png",
      "128": "images/get_started128.png"
    },
    "default_popup": "popup.html"
  },
  "icons": {
    "16": "images/get_started16.png",
    "32": "images/get_started32.png",
    "48": "images/get_started48.png",
    "128": "images/get_started128.png"
  },
  "permissions": ["tabs", "<all_urls>"]
}

我想禁用除少数以外的所有 url 的扩展,可以说我只想为 yahoo.com 和 stackoverflow.com 启用我需要做的扩展。

标签: google-chromegoogle-chrome-extension

解决方案


您应该matches在清单中使用。

https://developer.chrome.com/extensions/content_scripts#declaratively

例如:

{
  "name": "Get pages source",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Get pages source from a popup",
  "browser_action": {
    "default_icon": {
      "16": "images/get_started16.png",
      "32": "images/get_started32.png",
      "48": "images/get_started48.png",
      "128": "images/get_started128.png"
    },
    "default_popup": "popup.html"
  },
  "icons": {
    "16": "images/get_started16.png",
    "32": "images/get_started32.png",
    "48": "images/get_started48.png",
    "128": "images/get_started128.png"
  },
  "content_scripts": [
   {
     "matches": ["http://*.yahoo.com*", "http://*.stackoverflow.com*"]
   }
  ],
  "permissions": ["tabs", "<all_urls>"]
}

顺便提一句。all_urls如果不需要,请避免许可。


推荐阅读