0

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();
stm
  • 160
  • 1
  • 11
  • See http://stackoverflow.com/questions/23044611/chrome-extension-trigger-keydown-js – guest271314 Mar 25 '16 at 19:48
  • Thanks for your reply, guest271314. Still, I can't figure it out. I'm not that experienced in JavaScript, and can not see a connection between my problem and your link. Can you please give me a further hint? – stm Mar 26 '16 at 19:14
  • 1
    Possible duplicate of [How to use jQuery in chrome extension?](http://stackoverflow.com/questions/21317476/how-to-use-jquery-in-chrome-extension) – Praveen Puglia Mar 28 '16 at 12:54
  • What file is using that javascript code? Can you also post your manifest? – Teepeemm Mar 28 '16 at 14:00
  • See changes above. The code is in "contentscript.js" which is declared as content script in manifest.json. – stm Mar 29 '16 at 17:25
  • I think Praveen has found the problem. You just need to have jQuery as a content script, so that it isn't isolated from the world of the extension. – Teepeemm Mar 31 '16 at 20:07
  • I added the to files to manifest.json (see changes for manifest.json above). Still, my script (contentscript.js) is not working. jQuery seems not to be implemented correctly because "Uncaught ReferenceError: jQuery is not defined" is thrown. See details added to the end of my initial question. – stm Apr 05 '16 at 20:21

0 Answers0