Greasemonkey Jquery interference? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-01 09:19:20

问题


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

回答1:


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.




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



来源:https://stackoverflow.com/questions/12983202/greasemonkey-jquery-interference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!