@require-ing jQuery overwrites the page's $ variable

前端 未结 2 1694
夕颜
夕颜 2020-12-19 18:52

I\'m @require-ing jQuery for my Greasemonkey script with this line in my script file:

// @require https://ajax.googleapis.com/ajax/libs/jquery/1         


        
相关标签:
2条回答
  • 2020-12-19 19:39

    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 I hope you will join me in voicing your opinion/experiences on the principle bug report for this issue.

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

    0 讨论(0)
  • 2020-12-19 19:46

    See jQuery.noConflict();; it explains a lot.

    EDIT:
    Found "Greasemonkey 1.0 + jQuery: Broken, with Workaround" on searching.

    Possible solution:

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

    Doing that, probably will implicitly specify that $, jQuery will be available in your script's namespace.

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