Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

javascript - Can't use injected jQuery

I want to inject jQuery into a web page (with a content script) in a Chrome extension.

I try, but something confuses me.

here is my 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");

I can see the console message but I still can't use jQuery.

I wrote a simple website and tried to include jQuery through the content script instead of adding it with a <script> tag.

In the Dev Tool console, I enter window.jQuery or $("li").siblings().

If include jQuery in the page, it works, but if it's injected with a content script, it shows jQuery as undefined.

Why does this happen?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I used this and it's working fine. maybe the path of your jquery is not correct.

"content_scripts": [{
    "matches": ["http://*/*", "https://*/*"],
    "js": ["js/jquery.min.js", "js/socket/socket.io.js","js/socket/client.js"],
    "run_at": "document_idle"
}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...