简体   繁体   English

Javascript chrome扩展内容脚本

[英]Javascript chrome extension content scripts

I am developing a chrome extension that involves many javascript scripts. 我正在开发一个涉及许多JavaScript脚本的chrome扩展程序。 In my chrome extension manifest, I am trying to set a script to run on a certain URL. 在我的chrome扩展清单中,我试图将脚本设置为在某个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. 我有一个可以使用脚本的URL,但是如果我尝试为另一个JavaScript文件添加另一个权限,则不会发生任何事情。 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://*/*" ] } 

Thank you! 谢谢!

Wrong syntax. 语法错误。 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. 请注意,不允许使用JSON格式的注释。 First snippet was just informational. 第一个代码段仅供参考。

It looks like you need to separate the two content script objects from each other. 看来您需要将两个内容脚本对象彼此分开。 Try this: 尝试这个:

{
  "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://*/*"
  ]
}

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

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