Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么jQuery在我的GreaseMonkey脚本中加载两次_Javascript_Jquery_Greasemonkey - Fatal编程技术网

Javascript 为什么jQuery在我的GreaseMonkey脚本中加载两次

Javascript 为什么jQuery在我的GreaseMonkey脚本中加载两次,javascript,jquery,greasemonkey,Javascript,Jquery,Greasemonkey,出于某种原因,我的Firefox4+GreaseMonkey脚本加载jQuery两次。我复制粘贴了以下代码片段,“测试”警报显示两次 问候 var $; // Add jQuery (function(){ if (typeof unsafeWindow.jQuery == 'undefined') { var GM_Head = document.getElementsByTagName('head')[0] || document.documentElement,

出于某种原因,我的Firefox4+GreaseMonkey脚本加载jQuery两次。我复制粘贴了以下代码片段,“测试”警报显示两次

问候

var $;

// Add jQuery
(function(){
    if (typeof unsafeWindow.jQuery == 'undefined') {
        var GM_Head = document.getElementsByTagName('head')[0] || document.documentElement,
            GM_JQ = document.createElement('script');

        GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
        GM_JQ.type = 'text/javascript';
        GM_JQ.async = true;

        GM_Head.insertBefore(GM_JQ, GM_Head.firstChild);
    }
    GM_wait();
})();

// Check if jQuery's loaded
function GM_wait() {
    if (typeof unsafeWindow.jQuery == 'undefined') {
        window.setTimeout(GM_wait, 100);
    } else {
        $ = unsafeWindow.jQuery.noConflict(true);
        letsJQuery();
    }
}

// All your GM code must be inside this function
function letsJQuery() {
    alert("test");
}

这可能是因为目标页面正在加载帧或iframe

将以下行添加到脚本代码部分的顶部:

if (window.top != window.self)  //-- Don't run on frames or iframes
    return;

另外,如果您只是使用FF+GM,那么就不用为加载jQuery而费事了。GM现在可以使用更高版本的jQuery

只需添加一行,如:

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
进入脚本的元数据块。jQuery将被复制到您的本地计算机上一次并从那里运行——消除了脚本运行时有时可能出现的几秒钟延迟


如果你使用一个GM模拟扩展,比如。

很好,这样的脚本可以在Chrome上运行。万分感谢!天哪,没有你的回答,我永远也得不到答案。