jQuery in Greasemonkey 1.0 conflicts with websites using jQuery

前端 未结 3 2250
猫巷女王i
猫巷女王i 2020-11-22 05:07

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

相关标签:
3条回答
  • 2020-11-22 05:18

    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.

    0 讨论(0)
  • 2020-11-22 05:23

    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.

    0 讨论(0)
  • 2020-11-22 05:43

    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.

    0 讨论(0)
提交回复
热议问题