Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed additional jQuery plugins into Greasemonkey

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/).

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.

like image 352
Carl Avatar asked Jan 13 '09 15:01

Carl


People also ask

Can I use jQuery in Greasemonkey?

jQuery doesn't work in Greasemonkey at all. Is there other way to use jQuery in Greasemonkey? For all the people who have the same problem, you must upload the file to greasespot then install it from there.

What are jQuery plugins?

A jQuery plugin is a method that we use to extend jQuery's prototype object. It is a piece of code written in a JavaScript file, which enables all jQuery objects to inherit any methods that you add.


2 Answers

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 

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.

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

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

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

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'>"); 
like image 56
Steve Davis Avatar answered Sep 21 '22 21:09

Steve Davis


Also checkout the GreaseMonkeyWiki pages on using JQuery in a GreaseMonkey script and on the @require block.

like image 26
Michael Paulukonis Avatar answered Sep 19 '22 21:09

Michael Paulukonis