javascriptjsonmanifest

Why does my JSON work for one URL but not multiple URLs?


I am developing a Chrome extension that involves many JavaScript scripts. In my Chrome extension manifest, I am trying to set a script to run on a certain URL. I have gotten one URL to work with a script, but if I try and add another permission for a different JavaScript file nothing happens. Is my syntax or code wrong?

{
  "name": "test",
  "manifest_version": 2,
  "version": "1.5",
  "browser_action": {"default_icon": "icon_16.png"},
  "icons" : {
  "128": "icon_128.png", "16": "icon_16.png"},   
"description": "test.",
  "content_scripts": [
    {
      "matches": ["*://solecarts.com/monitor/run.html*", "*://www.solecarts.com/monitor/run.html*"],
      "js": ["query.js"],
      
    "matches": ["*://solecarts.com/monitor/shopify.html*", "*://www.solecarts.com/monitor/shopify.html*"],
      "js": ["shopify.js"]
    }
  ], "permissions": [
    "tabs", "*://solecarts.com/*", "*://solecarts.com/*",
      "http://*/*",
      "https://*/*"
  ]
}

Solution

  • You have the wrong syntax for the kind of content you're trying to add multiples of. Let me re-indent your code, with comments:

    "content_scripts": [ // An array
      {                  // An object inside this array
                         // Properties of that object
        "matches": [
           "*://solecarts.com/monitor/run.html*",
           "*://www.solecarts.com/monitor/run.html*"
        ],
        "js": ["query.js"],
                         // Same properties within the same object?
        "matches": [
           "*://solecarts.com/monitor/shopify.html*",
           "*://www.solecarts.com/monitor/shopify.html*"
        ],
        "js": ["shopify.js"]
      }
    ],
    

    Instead, it should be an array containing two separate objects:

    "content_scripts": [
      {
        "matches": [
           "*://solecarts.com/monitor/run.html*",
           "*://www.solecarts.com/monitor/run.html*"
        ],
        "js": ["query.js"]
      }, {
        "matches": [
           "*://solecarts.com/monitor/shopify.html*",
           "*://www.solecarts.com/monitor/shopify.html*"
        ],
        "js": ["shopify.js"]
      }
    ],
    

    Do note that comments are not allowed in JSON format. First snippet was just informational.