简体   繁体   English

jQuery未加载到Google Chrome content_script扩展名中

[英]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在解压缩时向我发送了两个错误。

  1. Invalid value for 'content_script[0].matches[0]': Missing scheme seperator. “ content_script [0] .matches [0]”的值无效:缺少方案分隔符。
  2. 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.

相关问题 Google Chrome content_script 未在匹配的 URL 上加载 JS - Google Chrome content_script not loading JS on matching URL Chrome扩展程序content_script隔离 - Chrome extension content_script isolation chrome扩展执行脚本不适用于content_script - chrome extension execute script not working with content_script 在background和content_script之间打开一个用于chrome扩展的消息端口 - Opening a message port between background and content_script for a chrome extension AJAX被阻止来自chrome扩展content_script - AJAX Blocked from chrome extension content_script jQuery未在chrome扩展内容脚本中加载 - JQuery is not loading in chrome extension content script 尝试在chrome扩展(JavaScript)中从default_script到content_script进行通信无法正常工作 - Trying to communicate from default_script to content_script in chrome extension (JavaScript) not working chrome扩展通信网页-&gt; content_script-&gt; popup.js - chrome extension communication web page -> content_script -> popup.js Chrome扩展程序:从content_script访问页面的HTML5 localStorage - Chrome Extension: access page's HTML5 localStorage from content_script 无法将鼠标坐标从 content_script 传递到弹出窗口(chrome 扩展) - can't pass mouse coordinates from content_script to popup (chrome extension)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM