简体   繁体   English

将CSS文件添加到Chrome扩展程序清单不起作用

[英]Adding CSS file to Chrome extension manifest not working

I'm attempting to get an external CSS file to load in Chrome. 我正在尝试获取外部CSS文件以在Chrome中加载。 To do this, it would seem I have to edit an existing Chrome extension's manifest.json file and minimally add the following to the content_scripts node: 为此,似乎我必须编辑一个现有的Chrome扩展程序的manifest.json文件,并将以下内容最少添加到content_scripts节点:

{
  "css": [ "Theme.css" ]
}

From what I understand, this will add the Theme.css file (which is in the same root directory as the manifest.json file) to all pages accessed in Chrome. 据我了解,这会将Theme.css文件(与manifest.json文件位于同一根目录中)添加到Chrome中访问的所有页面。 I saw that I could additionally qualify the json block by including a matches key-value pair, but I've omitted that here. 我看到我可以通过包含matches键值对来进一步限定json块,但在此省略了。

This doesn't appear to be working because I'm not seeing the styles. 这似乎没有用,因为我没有看到样式。 To start, my Theme.css file contains this: 首先,我的Theme.css文件包含以下内容:

span {
    color: green !important;
}

Additionally, I've confirmed that the extension I added it to (AngularJS Batarang FWIW) is recognized as "Enabled" by Chrome. 此外,我已经确认添加到(AngularJS Batarang FWIW)的扩展名被Chrome识别为“启用”。

I also tried the solution explained here where you load the CSS as an accessible resource via a JS file, but that doesn't work either. 我还尝试了此处介绍的解决方案, 该解决方案中,您可以通过JS文件将CSS加载为可访问的资源,但这也不起作用。

What am I missing? 我想念什么? Or is there an easier way to do this? 还是有更简单的方法来做到这一点?

FWIW: here is the entire manifiest: FWIW:这是整个清单:

{
   "background": {
      "scripts": [ "background.js" ]
   },
   "content_scripts": [ {
      "js": [ "inject.js" ],
      "matches": [ "<all_urls>" ],
      "run_at": "document_start"
   }, {
      "matches": [ "http://*/*", "https://*/*"],
      "css": [ "Theme.css" ]
   } ],
   "description": "Extends the Developer Tools, adding tools for debugging and profiling AngularJS applications.",
   "devtools_page": "devtoolsBackground.html",
   "icons": {
      "128": "img/webstore-icon.png",
      "16": "img/webstore-icon.png",
      "48": "img/webstore-icon.png"
   },
   "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9hsXx3+F75DyGto3mkm0FB2sycQzyMqXQAySn2Qj67vIHFMSrVZ0ItPzGnWJwoRoaDI7cQF9c/WLDpLJQwGe5CV5z84MueOME3e45JJEwN+YsW5ufEavmp+pk1c9h/Wyi8bMoSWJGIrOG72wCTFOdnyN6nocA0dm4w7UWsxLLEQIDAQAB",
   "manifest_version": 2,
   "minimum_chrome_version": "21.0.1180.57",
   "name": "AngularJS Batarang",
   "page_action": {
      "default_icon": {
         "19": "img/icon19.png",
         "38": "img/icon38.png"
      },
      "default_title": "AngularJS Super-Powered"
   },
   "permissions": [ "tabs", "<all_urls>" ],
   "update_url": "https://clients2.google.com/service/update2/crx",
   "version": "0.10.6",
   "web_accessible_resources": [ "dist/hint.js" ]
}

You did not register any pages to apply the CSS file to. 您没有注册任何页面来应用CSS文件。 The matches section is required . 必须包含“ 比赛”部分

If you want to apply it to all pages, use "matches": ["<all_urls>"] 如果要将其应用于所有页面,请使用"matches": ["<all_urls>"]

You have a problem with your content_scripts key, you should wrap your matches , css and js together under content_scripts key, like this: 您的content_scripts键有问题,应将matchescssjs包装在content_scripts键下,如下所示:

"content_scripts": [
    {
        "matches": ["http://*/*"],
        "css": ["./Theme.css"],
        "js": ["./inject.js"]
    }
],

Also, don't forget to write ./ before your CSS/JS File to point to your root directory! 另外,不要忘记在CSS / JS文件之前写./指向根目录!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM