I don't want to include jQuery on every page by listing it in the manifest
In the console, this works fine, but I can't dynamically include jQuery in a content script
No idea why
- Put these two files in a folder (content.js and manifest.json)
- Go to
chrome:extensions
in omnibox (url bar)- Load Unpacked Extension
- Select Folder
- Go to any page and CMD+Shift+R reload without cache
Check out the console and see jQuery is undefined
content.js
if (document.readyState === "complete") {
appendJQuery();
} else {
document.addEventListener("DOMContentLoaded", appendJQuery);
} function appendJQuery () {
var jq = document.createElement("script");
window.document.querySelector("head").appendChild(jq);
jq.onload = function () {
console.log(typeof $); // $ is not defined ?????
}
jq.src = "https://code.jquery.com/jquery-2.1.1.min.js";
}
manifest.json
{
"manifest_version": 2,
"name": "Sample",
"short_name": "Sample",
"version": "1.1",
"permissions": ["tabs", "http://*/*, https://*/*", "*://*/*", "<all_urls>"],
"content_scripts": [{
"matches": ["*://*/*", "http://*/*", "https://*/*", "file://*/*"],
"js": ["content.js"],
"run_at": "document_start"
}]
}
then jQuery is undefined......... wtf??? anyone know why??