简体   繁体   English

为什么jQuery不加载到我的Chrome扩展程序中?

[英]Why isn't jQuery loading into my Chrome extension?

This is a Chrome extension which replaces the user's new tab screen. 这是一个Chrome扩展程序,可代替用户的新标签页。

My jQuery isn't loading because the 'hello' in my HTML isn't alerting. 我的jQuery无法加载,因为HTML中的“ hello”未发出警告。 Should I be loading jQuery in the manifest or the HTML? 我应该在清单或HTML中加载jQuery吗? Whichever it is, I presume the HTML should be in the same place. 无论是哪种方式,我都认为HTML应该位于同一位置。

manifest.json: manifest.json的:

{
  "manifest_version": 2,

  "name": "Some title",
  "description": "Some description.",
  "version": "1.0",

  "chrome_url_overrides": {
    "newtab": "index.html"
  },

"content_scripts": [
    {
      "matches": ["newtab"],
      "css": ["style.css"],
      "js": ["jquery.js"]
    }
  ]

}

index.html 的index.html

<html>
<head>
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            alert("hello");
        $(function(){
            $('#container').masonry({
            // options
            itemSelector : '.item',
            columnWidth : 300
        });
    });
    </script>
</head>
<body>
<!-- etc -->

You cannot load scripts from remote locations in chrome extensions without explicitly specifying permission in manifest.json , you can either specify permissions in manifest.json or download jquery and distribute your extension with jQuery file. 你不能加载在Chrome浏览器扩展远程脚本没有明确指定权限manifest.json ,您可以指定权限manifest.json或下载jQuery和分发使用jQuery文件您的扩展。 Second option (having local copy of jquery in extension folder) seems much more reasonable. 第二种选择(在扩展文件夹中有jquery的本地副本)似乎更合理。

If the index.html is your background page then I would add jquery to the extension folder and reference it relatively. 如果index.html是您的背景页面,那么我将jquery添加到扩展文件夹并相对引用它。

If its applied to pages then specify a content script as described right here . 如果将其应用于页面,请按照此处所述指定内容脚本。 Again, keep it local and relative. 同样,保持局部和相对。

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

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