我想将 jQuery 注入(inject)到 Chrome 扩展程序的网页(带有内容脚本)中。
我尝试过,但有些事情让我困惑。
这是我的manifest.json
:
{
"name": "test",
"description": "test content script.",
"version": "0.0.1",
"background": {
"scripts": ["jquery-1.10.2.js", "popup.js"]
},
"permissions": ["tabs", "notifications", "http://*/*", "https://*/*"],
"browser_action": {
"default_title": "test",
"default_icon": "icon.png"
},
"options_page": "manage.html",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["jquery-1.10.2.js"]
}
],
"manifest_version": 2
}
jquery-1.10.2.js:
/*jquery's orign code*/
console.log("end of jquery");
我可以看到控制台消息,但我仍然无法使用 jQuery。
我编写了一个简单的网站,并尝试通过内容脚本包含 jQuery,而不是使用 <script>
添加它。标签。
在开发工具控制台中,我输入 window.jQuery
或$("li").siblings()
.
如果在页面中包含 jQuery,它可以工作,但如果注入(inject)内容脚本,它会显示 jQuery
未定义。
为什么会发生这种情况?
最佳答案
我用过这个,效果很好。可能是你的jquery路径不正确。
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"js": ["js/jquery.min.js", "js/socket/socket.io.js","js/socket/client.js"],
"run_at": "document_idle"
}]
关于javascript - 无法使用注入(inject)的 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28197570/