javascript - 有什么解释为什么 $ 或 jQuery 在 chrome 扩展开发中不起作用?
问题描述
我已经将jquery compressed, production jQuery 3.5.1 slim build
文件下载到项目根目录中,并添加到清单文件中,然后尝试使用$
符号运行 jQuery 函数,我尝试在内容脚本和后台脚本中执行此操作,但出现错误Uncaught ReferenceError: $ is not defined
。
清单.json
"name": "Project",
"version": "1.0",
"description": "Lorem Ipsum is simply dummy text of the printing",
"permissions": ["activeTab", "declarativeContent", "storage","https://*/"],
"options_page": "options.html",
"background": {
"scripts": ["background.js","jquery.js"],
"persistent": true
},
"page_action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
}
},
"icons": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
},
"content_scripts":[{
"matches" : ["https://www.amazon.com/s?*"],
"js" : ["content.js","jquery.js"]
}],
"manifest_version": 2
}
内容.js
chrome.runtime.sendMessage({productLink: 'test', search_url: url.href }, function(response) {
console.log(`message from background: ${JSON.stringify(response)}`);
});
function isProductUrl(url){
let arr = url.split('/');
if(arr[4] == 'dp')
return true;
else
return false;
}
$.ajax({
url: "www.localhost.com/search",
context: document.body
}).done(function() {
$( this ).addClass( "done" );
});
解决方案
推荐阅读
- python - 根据分位数对 pandas 列进行分箱
- python - 在子图中调整 matplotlib 浮动轴的位置,并反转轴方向
- python - 结果错误
- javascript - 如何创建一个间隔为半小时的时间数组,而不是在变量中声明
- d3.js - 在 D3 中合并连续的翻译
- database - 将数据库更改复制并同步到实体框架模型
- javascript - 为什么 ng-click 没有在按钮单击时调用函数?
- apache-kafka - 在数据库和 Kafka 生产者之间同步事务
- excel - 用于 excel 的 Google API 距离矩阵宏工具
- google-slides - 如何在 Google 幻灯片中创建动态网址?