简体   繁体   English

在chrome扩展的内容脚本中包含Jquery

[英]Include Jquery in a content script of a chrome extension

The error message I am receiving is "$ is not defined" 我收到的错误消息是“未定义$”

Part of my manifest file: 我的清单文件的一部分:

     "content_scripts": [
     {
       "matches": ["http://www.google.com/calendar/render*",                                          
       "https://www.google.com/calendar/render/*"],
       "js": ["jquery.min.js", "jquery-ui.js", "script.js"],
       "css": ["jquery.datepick.css", "jquery-ui.css"],
       "run_at": "document_start"
     }],
     "background": {"scripts": ["background.js"]},
     "web_accessible_resources": ["jquery.min.js", "script.js", "jquery-ui.js",  "jquery.datepick.css", "jquery-ui.css"]

In my background.js file I am trying to execute the needed scripts: 在我的background.js文件中,我试图执行所需的脚本:

chrome.tabs.executeScript(tabId, {file: "jquery.min.js"}, function(){
chrome.tabs.executeScript(tabId, {file: "jquery-ui.js"}, function() {
    chrome.tabs.executeScript(tabId, { file: 'script.js' });
});
});

Still, in script.js I can't use any Jquery function (error: $ is not defined). 不过,在script.js中,我无法使用任何Jquery函数(错误:未定义$)。

Can anyone please explain me why it's failing, or how it should be done? 谁能解释我为什么失败,或者应该怎么做?

Thanks in advance. 提前致谢。

Your manifest.json and background.js look fine. 您的manifest.json和background.js看起来不错。 If you have the jquery-file in the root of your extension folder/package, there are only two things I can think of: 如果扩展文件夹/软件包的根目录中有jquery文件,那么我只能想到两件事:

  1. You are unsetting the $-symbol somewhere in script.js 您正在script.js中的某个位置取消设置$符号
  2. You are not reloading the extension after you made changes: 进行更改后,您不会重新加载扩展程序:

    Go to chrome://extensions/, find your extension, press 'Reload' (and make sure it's enabled). 转到chrome:// extensions /,找到您的扩展程序,按“重新加载”(并确保已启用)。

    If you are not using the 'Load Unpacked extension' function, you should entirely re-install your extension after you've made changes. 如果您没有使用“加载解压缩的扩展程序”功能,则应在进行更改后完全重新安装扩展程序。

To be very sure that the jQuery file is loaded, open jquery.min.js and add console.log('jQuery file loaded') to jquery.min.js . 为确保已加载jQuery文件,请打开jquery.min.js并将console.log('jQuery file loaded')jquery.min.js If you are not seeing the console message, then maybe jquery.min.js is corrupted (try downloading it again). 如果您没有看到控制台消息,则可能是jquery.min.js已损坏(尝试再次下载)。

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

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