为什么jQuery在我的GreaseMonkey脚本中加载两次 [英] Why does jQuery load twice in my GreaseMonkey Script

查看:87
本文介绍了为什么jQuery在我的GreaseMonkey脚本中加载两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我的Firefox4 + GreaseMonkey脚本加载了jQuery两次。我复制'n'pasted以下片段,测试警告显示两次。

for some reason my Firefox4+GreaseMonkey Script loads jQuery twice. I copy'n'pasted the following snippet, the "test" alert shows twice.

问候

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。

This is probably because the target page is loading frames or iframes.

将这些行添加到脚本代码部分的顶部:

Add these lines to the top of the code-portion of your script:

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



此外,如果您只是使用FF + GM,请不要打扰所有加载jQuery的rigamarole。 GM现在可以使用后来的jQuery版本。


Also, if you are just using FF + GM, don't bother with all that rigamarole to load jQuery. GM now works with the later jQuery versions.

只需添加如下行:

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js

进入脚本的元数据块。 jQuery将被一次复制到本地计算机上并从那里运行 - 消除了脚本运行时有时几秒钟的延迟。

into your script's metadata block. jQuery will be copied once onto your local machine and run from there -- eliminating what can sometimes be a few seconds delay in your script's runtime.

如果您使用其中一个GM模拟扩展程序,例如可以在Chrome中运行chrome.google.com/extensions/detail/dhdgffkkebhmkfjojejmpbldmpobfkfo\"rel =noreferrer> TamperMonkey

And such scripts can run in Chrome, if you use one of the GM-emulating extensions like TamperMonkey.

这篇关于为什么jQuery在我的GreaseMonkey脚本中加载两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆