如何在Greasemonkey中嵌入额外的jQuery插件 [英] How to embed additional jQuery plugins into Greasemonkey

查看:89
本文介绍了如何在Greasemonkey中嵌入额外的jQuery插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经能够让Greasemonkey和jQuery 1.2.6一起工作而没有问题,但是,现在我想知道如何在我的Greasemonkey脚本中嵌入额外的jQuery插件,例如Eric Martin的SimpleModal插件( http://www.ericmmartin.com/projects/simplemodal/ )。

So I've been able to get Greasemonkey and jQuery 1.2.6 to work together without issue, but, now I'm wondering how to embed additional jQuery plugins into my Greasemonkey script, such as Eric Martin's SimpleModal plugin (http://www.ericmmartin.com/projects/simplemodal/).

以下代码加载了jQuery,但我不确定如何正确加载SimpleModal:

The following code gets jQuery loaded, but I'm not sure how to get SimpleModal loaded properly:

    var GM_JQ = document.createElement('script');
    GM_JQ.src = 'http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js';
    GM_JQ.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(GM_JQ);

    var GM_JQ_SM = document.createElement('script');
    GM_JQ_SM.src = 'http://simplemodal.googlecode.com/files/jquery.simplemodal-1.2.2.min.js';
    GM_JQ_SM.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(GM_JQ_SM);

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

    }
    GM_wait();

任何人都有任何想法?谢谢。

Anyone have any ideas? Thanks.

推荐答案

首先,如果你没有Firebug调试访问权限,那么包含jquery的最简单方法就是使用require设置:

First, if you are OK with not having Firebug debugging access the easiest way to include jquery is to use the require settings:

// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js
// @require        http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.2/jquery-ui.min.js 

在该行之后,您可以包含其他外部脚本。大多数jquery插件都不像jquery api那样可用,但是你可以自己托管它。

Following that line you can include other external scripts. Most of the jquery plugins are not available like the jquery api, but you can host it yourself.

使用require还允许你转储所有的加载代码然后去with:

Using the require also allows you to dump all the loading code and simply go with:

$(document).ready( function() { ... });

Firebug会报告错误,但你无法进入调试器。

Firebug will report bugs, but you will not be able to step into the debugger.

此外,一旦你加载了jquery,你可以加载其他项目,如下所示:

Additionally, once you have jquery loaded you can load other items to like so:

$('head').append("<link href='http://www.somewebsite.com/styles.css' type='text/css' rel='stylesheet'>"); 

这篇关于如何在Greasemonkey中嵌入额外的jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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