javascript - 无法使用注入(inject)的 jQuery

标签 javascript jquery google-chrome-extension

我想将 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/

相关文章:

尝试仅获取 <option> 元素时发生 jQuery 未定义错误

javascript - 如何获取所有 chrome 内容设置?

google-chrome - 驱动chrome来测量页面加载时间

javascript - 使用 Angular Formly 时,表单提交中不包含空字段

javascript - CSS 过渡提前中断

javascript - WebRTC 对于多人 HTML5 游戏是否防作弊?

javascript - 火狐/Chrome扩展程序: how to open link when new tab is created?

JavaScript:如何在用户选择注销时关闭所有子窗口?

jquery - Bootstrap Datetimepicker 类上的触发器 "dp.change"

javascript - css3/Jquery 动画/过渡/: How to make image move vertically and back?