gpt4 book ai didi

javascript - 如何在 Chrome 扩展程序中使用 JQuery?

转载 作者:行者123 更新时间:2023-12-03 11:55:11 24 4
gpt4 key购买 nike

我正在尝试学习如何进行扩展,并且我从非常基础的开始。我不断收到错误“Uncaught ReferenceError:$未定义”

这是我的 list .json:

{
"manifest_version" : 2,
"name": "My Extension",
"version": "1",
"description": "Testing",
"content_scripts": [
{
"matches": ["http://www.google.com/*"],
"js": ["jquery.min.js"]
}
],

"background": {
"scripts": ["run.js"]
}
}

JQuery 文件已正确命名并位于扩展文件夹内。这是“run.js”脚本:

$(document).ready(function() {
alert("document loaded");
})

如何解决这个问题以便我可以正确使用 JQuery?提前致谢。

最佳答案

使用 content_scripts/matches 属性,您可以将 jQuery 限制为仅在当前 URL 与 http://www.google.com/* 匹配时加载。更改它以匹配所有域:

{
"manifest_version" : 2,
"name": "My Extension",
"version": "1",
"description": "Testing",
"content_scripts": [
{
"matches": ["http://*"],
"js": ["jquery.min.js"]
}
],

"background": {
"scripts": ["run.js"]
}
}

来自 docs :

Required. Specifies which pages this content script will be injected into. See Match Patterns for more details on the syntax of these strings and Match patterns and globs for information on how to exclude URLs.

或者,您可以将其添加到现有的后台脚本中:

"background": {
"scripts": ["jquery.min.js", "run.js"]
}

关于javascript - 如何在 Chrome 扩展程序中使用 JQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25638698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com