[英]jQuery not loading in Google Chrome content_script extension
I can't manage to load jQuery in a Google Chrome userscript. 我无法在Google Chrome用户脚本中加载jQuery。
This is how my manifest.json looks: 这是我的manifest.json的样子:
{
//...
"content_scripts" : [{
"js" : ["http://code.jquery.com/jquery-1.7.1.js", "code.js"],
//...
}],
//...
}
But in code.js
, $
is undefined
. 但是在code.js
, $
是undefined
。
code.js
otherwise works, and actually I have a bookmarklet working (that uses the site's version of jQuery). code.js
否则可以正常工作,实际上我有一个工作书签(使用站点的jQuery版本)。
I've tried minified and unminified versions of jQuery, hosted or local. 我尝试了jQuery的最小版本和最小版本(托管或本地)。
I've also tried embedding a minified jQuery at the top of my code.js
, but if I do that the code does not even run, actually the whole file does not run (even an alert before the chunk of minified jQuery). 我还尝试将code.js
jQuery嵌入到我的code.js
的顶部,但是如果我这样做,代码甚至都不会运行,实际上整个文件都不会运行(甚至在最小的jQuery块之前发出警报)。
If you want to look at it, here it is . 如果您想看一下,就在这里 。
From the manifest.json provided in your zip, Chrome sends me two errors when unpacking. 从您的zip文件中提供的manifest.json文件中,Chrome在解压缩时向我发送了两个错误。
- Invalid value for 'content_script[0].matches[0]': Missing scheme seperator. “ content_script [0] .matches [0]”的值无效:缺少方案分隔符。
- Could not load javascript ' http://code.jquery.com/jquery-1.7.1.js ' for content script. 无法为内容脚本加载javascript'http: //code.jquery.com/jquery-1.7.1.js '。
The first one says your matches
statement isn't correctly formatted. 第一个提示您的matches
语句格式不正确。 After your *
you should add ://
like so: 在*
之后,您应该添加://
如下所示:
"matches" : ["*://my.deviantart.com/messages/*"]
The second one is about you trying to load the jquery from an external recource. 第二个是关于您尝试从外部资源加载jquery的。 If you change the path to your local file it should work. 如果您将路径更改为本地文件,则它应该可以工作。
"js" : ["jquery.js", "code.js"],
When I console.log($)
in your code.js
file after these adjustments jquery seems to work just fine. 经过这些调整后,当我在您的code.js
文件中console.log($)
,jquery似乎正常工作。
EDIT: Note that you need to add to your html (in <head>
section) the following: 编辑:请注意,您需要将以下内容添加到html(在<head>
部分中):
<script src='./jquery.js'></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.