I program user script and I have problem in Chrome (Firefox with Greasemonkey is OK):
I use Tampermonkey; here is the code:
// Header...
(function addjQuery() {
var head = document.getElementsByTagName('head')[0] ;
var jQuery = document.createElement('script');
jQuery.type = 'text/javascript';
jQuery.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
head.appendChild(jQuery);
waitjQuery() ;
})();
function waitjQuery(){
typeof jQuery == 'undefined' ? setTimeout(waitjQuery, 50) : main() ;
}
My script loop in function wait and the typeof jQuery (or $…) is always undefined. I have checked head of page HTML with console and jQuery is correctly loaded…
Google chrome extensions are sandboxed. Therefore, the jQuery you have appended in to the DOM is not available to your script. If you want to use jQuery within your extension you need to define it in the manifest file. If you want to run your script in the domain of the page then you should inject it after jQuery exists within the page.
You thus don’t want to check whether jQuery exists, as it never will, but instead if the script element containg jQuery has been appended.