jQuery in Greasemonkey 1.0 conflicts with websites using jQuery
Asked Answered
L

3

22

Ever since the new Greasemonkey 1.0 was released a few days ago, every site that has jQuery and where I use jQuery in my Greasemonkey script do not run my script properly. The jQuery I have in my GS script (using the @require metadata) conflicts with the page's jQuery. This is due to the new @grant code.

I've read the documentation but still don't know how to run GS scripts in a sandbox again; the only options seem to be to either grant access to a GS API or to grant it to none and run the script without any security limitations, which doesn't work at all for me when I've designed my dozens of GS scripts to run WITH security limitations and like it that way.

Lenee answered 27/8, 2012 at 17:15 Comment(6)
What would I enter into the @grant directive? Doesn't that only work for the Greasemonkey API? How would I get that to make @require <jQuery> work within a sandbox?Lenee
Just use @grant GM_getValue, that should do the trick. Even if you don't actually call GM_getValue(), the @grant directive has the side effect of restoring the sandbox; so @require should go back to working the way it should.Vaporizer
Ah yeah, I figured that was one option. Merely a workaround, but at least it's an option. I'll still stick with 0.9 for now though because I have too many scripts that would need to be changed, and I'd rather leave them unchanged until GS addresses this problem.Lenee
Well, now I'm getting support requests from several users of my scripts. Please file a bug report with GM devs and/or comment on this bug.Vaporizer
GS 1.0 was released on around Aug. 24. Since about Aug. 27, they've been getting swamped with GS 1.0 issues. Looks like something will finally be done about it. Looks like they also posted something about this on the GS blog.Lenee
Actually, the blog entry and the bug reports (so far) are no help at all. They essentially say, "Rewrite all your scripts", but -- unlike my answer above -- they only offer one (bad) option.Vaporizer
V
15

Greasemonkey 1.0, radically changed the way the sandbox works, busting thousands of scripts. This is a huge problem, and I hope you will join me in voicing your opinion/experiences on the principle bug report for this issue.

The Greasemonkey blog claims that you can workaround the issue with the following:

this.$ = this.jQuery = jQuery.noConflict(true);

... Which I'm not sure will work in all cases. And it is the exact wrong approach from a side-effects-avoiding, DRY-principle, atomic-coding philosophy.   In my opinion, the best strategy is to restore the sandbox.

Reactivate the sandbox by specifying a @grant value (other than none). Edit your Metadata Block to end with the following lines:

// @grant       GM_addStyle
// @grant       GM.getValue
// ==/UserScript==
/*- The @grant directive is needed to work around a design flaws introduced in GM 1.0
    and again in GM 4.0.
    It restores the sandbox.
*/

The sandbox will be restored and all conflicts will be resolved.
And the scripts will be compatible with superior engines like Tampermonkey and Violentmonkey.

Vaporizer answered 4/9, 2012 at 0:34 Comment(0)
S
7

Two years on and this "feature" still hasn't been sufficiently documented or addressed.

Scripts which require jQuery still conflict with some pages which use jQuery.

Potential solutions are:

  1. Add @grant GM_log or similar to sandbox the script
  2. Alias existing jQuery objects in the script by adding this block:

    var my_jquery = jQuery;

    jQuery.noConflict(true);

    var $ = my_jquery, jQuery = my_jquery;

  3. Don't use jQuery

All in all, a terrible decision from Greasemonkey.

Should answered 28/5, 2015 at 14:44 Comment(0)
B
5

Had the same problem.

Since GM version 1.0 , all my scripts that @require jQuery, running on sites that also uses jQuery stopped working.

I know, I could try something like $ = unsafeWindow.$ , but that is not the point.

The point here is that they used to work, and now they wont.

Adding @grant GM_log fixed them.

Bertabertasi answered 21/9, 2012 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.