2

Possible Duplicate:
jQuery in Greasemonkey 1.0 conflicts with websites using jQuery

It appears that if any of my greasemonkey scripts have the line like the below i get errors on sites that include jquery. I'm not sure why but i do notice some javascript events outright not firing. (For example on SO i cant add a comment).

I could exclude SO in the script but is there a nicer solution? Maybe i should dynamically add jquery after testing if jquery exist. How do I solve this problem and how do I add the jquery script tags to a body without using jquery?

// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
Community
  • 1
  • 1
  • I wonder if you should use noConflict() asap...http://api.jquery.com/jQuery.noConflict/ – mrk Oct 19 '12 at 22:20

2 Answers2

4

Greasemonkey 1.0, radically changed the way the sandbox works, busting thousands of scripts. See also, jQuery in Greasemonkey 1.0 conflicts with websites using jQuery.

This is a huge problem, and the lead developer of Greasemonkey has expressed a complete unwillingness to resolve it in a sensible way.

To work around it, restore the sandbox to your script, and resolve the $ conflict, by editing your Metadata Block to end with the following lines:

// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design change introduced in GM 1.0,
    It restores the sandbox.
*/


Specifying a @grant value (other than none) reactivates the sandbox.


You might also consider switching to Scriptish, which has offered superior features and performance in the past, and does not suffer from this new sandbox behavior.

Scriptish, so far, hasn't posted updates that destroy a large portion of its install base, either.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
2

You need to include jQuery only if it isn't already included. So you need to make the first line check. If it is included run the script if it isn't included add like line to include it and then loop the checking function. Something like this:

var wrote = false,
    runScript = function(){
    if(typeof(jQuery) === 'undefined'){
        if(!wrote){
            wrote = true;
            document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>');
        }
        setTimeout(runScript, 50);
    }else{
        //your script goes here
    }
};

runScript();

Only problem would be is if google's jquery isn't up.

Adam Merrifield
  • 10,307
  • 4
  • 41
  • 58