I'm trying to add the "resizable"-functionality of jQuery UI to an existing webpage by using a Chrome extension. When using the following code, in Chrome console the error "Uncaught ReferenceError: jQuery is not defined" appears.
manifest.json:
{
"manifest_version": 2,
"name": "Resizeable",
"permissions": [
"tabs"
],
"content_scripts": [{
"all_frames": true,
"matches": ["https://abcde.net/Tools/*"],
"js": ["jquery-1.9.1.js","jquery-ui.js","contentscript.js"],
"run_at": "document_end"
}]
}
contentscript.js:
var jq = document.createElement('script');
jq.type = 'text/javascript';
jq.src = 'https://code.jquery.com/jquery-1.9.1.js';
document.head.appendChild(jq);
var jqUI = document.createElement('script');
jqUI.type = 'text/javascript';
jqUI.src = 'https://code.jquery.com/ui/1.10.4/jquery-ui.js';
document.head.appendChild(jqUI);
if (window.jQuery && jQuery.ui) {
alert('loaded');
} else {
alert('not loaded');
}
I can bypass the error message by pasting each of the three "paragraphs" individually into Chrome console. Therefore, I assume that the error appears because the jQuery is not loaded completely yet when jQuery UI is added. Next, I tried the following:
contentscript.js:
var jq = document.createElement('script');
jq.type = 'text/javascript';
jq.src = 'https://code.jquery.com/jquery-1.9.1.js';
document.head.appendChild(jq);
checkJquery();
function checkJquery() {
if(window.jQuery) {
var jq = document.createElement('script');
jq.type = 'text/javascript';
jq.src = 'https://code.jquery.com/ui/1.10.4/jquery-ui.js';
document.head.appendChild(jq);
alert('jQ is loaded');
checkJqueryUI();
} else {
setTimeout(function() {
checkJquery();
}, 1000);
}
}
function checkJqueryUI() {
if(jQuery.ui) {
alert('UI is loaded');
furtherCode();
} else {
setTimeout(function() {
checkJqueryUI();
}, 1000);
}
}
function furtherCode() {
var s = document.createElement('link');
s.rel = 'stylesheet';
s.href= 'https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css'
document.head.appendChild(s);
var s = document.createElement('style');
var css = '#resizable { width: 470px; height: 320px; padding: 0.5em; } #resizable h3 { text-align: center; margin: 0; }'
s.appendChild(document.createTextNode(css));
document.head.appendChild(s);
document.getElementById('bigDocFrame').parentNode.setAttribute("id", "resizable");
document.getElementById('bigDocFrame').parentNode.setAttribute("class", "ui-widget-content");
document.getElementById('bigDocFrame').height='100%';
var s = document.createElement('script');
s.type = 'text/javascript';
s.language = 'javascript';
var script = 'jQuery.noConflict();'
s.appendChild(document.createTextNode(script));
document.head.appendChild(s);
var s = document.createElement('script');
var script = 'jQuery(function() {jQuery( "#resizable" ).resizable();});'
s.appendChild(document.createTextNode(script));
document.head.appendChild(s);
}
But the condition "if(window.jQuery)" is always false. I expected it to be true after some time. Am I making a mistake? (Solved by adding jquery-1.9.1.js and jquery-ui.js to manifest.json)
Still, the error message " Uncaught ReferenceError: jQuery is not defined" appears. It is reffered to "VM5466:1", which consists of one line of code:
jQuery.noConflict();