I started using mouse hunt a while ago... and really enjoy it, i loved when i got a new trap and i could buy more cheese, more traps... and craft more items, but as time passed along i got bored with that and I just wanted to catch as many mice as possible, and be fast in what i do, also i wanted to have a script that sounds the horn and let me know when a king's reward has appeared, so i decided to create my own mouse hunt script, the greatest mouse hunt script ever created...

I started working on ma spare time in this... was not easy because some things ware hard to test... bunt eventually i did it.

You can see the script here : http://userscripts.org/scripts/show/57446 the script has been around for about a month and already has 1200 installs and going up, and it's becoming "the mouse hunt script" :D .

Now i will explain some of the problems i encountered on creating the script.

1. Saving persistent data, grease monkey does not have a very good way for this, so i decided to create an object where i save all the data and then save the object as JSON in a cookie

2. Updates, i was thinking that it would be nice to notify users when an update is available, so i did this by creating a .php file in a server and store there the latest version number, the script checks that version with it's current version and if is different will show a message

3. Passing parameters on a function attached to an event, this was the most tricky one... was pretty complicated... here is the solution :

$.each($(".huntername"), function (i,o) {
 
			var id = 0;
			var childs = $(o).children("a");
			var link = $(childs[0]).attr("href");
			var name = $(childs[0]).html();
 
			id = link.substr(link.indexOf("=")+1);
 
			$(o).html($(o).html()+"<a id="gmpl_af_&quot;+i+&quot;" onclick="return false" href="#">(add to links)</a>");
			_e("gmpl_af_"+i+"").addEventListener("click",function () {gmpl_addFriend(id,name)},false);
		});

4. How to use jquery in a grese monkey script, actually for this i fond the solution somewhere on the internet... : http://joanpiedra.com/jquery/greasemonkey/

// Add jQuery
    var GM_JQ = document.createElement('script');
    GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
    GM_JQ.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(GM_JQ);
 
// Check if jQuery's loaded
    function GM_wait() {
        if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
    else { $ = unsafeWindow.jQuery; letsJQuery(); }
    }
    GM_wait();
 
// All your GM code must be inside this function
    function letsJQuery() {
        alert($); // check if the dollar (jquery) function works
    }

And all your code should go where the alert($) is. Only if you have some functions you should put them outside that function.

Related posts:

  1. Autoupdate on greasemonkey script, example from “mousehunt improvements” script.
  2. Greasemonkey, create a options panel using jQuery
  3. jQuery popups wizard, web 2.0 ideea and usability