-1

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...

Brock Adams
  • 90,639
  • 22
  • 233
  • 295

2 Answers2

2

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.

BeRecursive
  • 6,286
  • 1
  • 24
  • 41
0

It was just a fluke that your script worked in Greasemonkey, as soon as you try to use any of the Greasemonkey API, (GM_ functions) it will stop working. This is because Greasemonkey now foolishly turns off the sandbox in select conditions and that script code mixes page-scope and script scope (a dangerous practice).

Chrome wisely does not switch off its sandbox, so the code doesn't work there. But furthermore, for Tampermonkey you do not want to add jQuery that way!
You want to add jQuery using @require. This makes your script faster, more secure, and less likely to break something on the page or to be broken.

A good cross-browser technique for adding jQuery -- that uses @require when it's available is found in the second part of this answer.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • i test your proposition and my user script inject all script it's good ! but the console javascript said : Uncaught ReferenceError: jQuery is not defined Uncaught ReferenceError: $ is not defined my script.user.js call script who use jquery... – user2062125 Feb 11 '13 at 23:27
  • Yes, that's because jQuery remains in the sandboxed script scope, where it belongs, not in the page scope where the console will see it. ... If you want to play around in Chrome's console, switch the console context to Tampermonkey, [as shown in this answer](http://stackoverflow.com/a/14059557/331508), and you will see `$` and `jQuery` defined. – Brock Adams Feb 12 '13 at 00:08
  • yes, so my user script is a project, he use over ten external script ... I want to keep this decomposition into several scripts and for exemple script 5 need a function in script 2, how can I do ? (has any merge) – user2062125 Feb 12 '13 at 00:20
  • Huh? I'm afraid I don't understand what you're saying. You probably should open a new question with exact details. – Brock Adams Feb 12 '13 at 00:34
  • there is a link ! to work my script wait jquery but it uses another script that is injected in the same way is the function that I call in my script is undefined – user2062125 Feb 12 '13 at 00:44
  • Still not clear. Refer to that answer I linked and open a new question if necessary. Don't try to mix script scope and page scope. – Brock Adams Feb 12 '13 at 00:56