Greasemonkey Script Updater

This is a script that will allow script developers to have a way to make sure that users of their scripts are using the latest version.

The syntax is simple and minimizes requests made so that the user's internet is not being used up every time the script runs. The default check time is every 7 days. I'm using a primitive date checking system, so if you can offer any improvements, let me know.

Use

To use this code, copy the function below into your script, best to have it at the far bottom so it doesn't slow down your script.

The syntax is this:

GM_update( [title], [version], [updateUrl], [versionUrl] );

This works best if you have a host where you can keep a text file containing the current version, and the current version only. It should contain a string such as 1.2.3 so that the script may work. If you have nothing, try Google Pages.

var GM_update = function(title, version, updateUrl, versionUrl) {
		var title = title;
		var today = new Date();
		    today = today.getDate();
		var last = GM_getValue(title);
		var current;
		var answer;
		var updateUrl = updateUrl;
		var versionUrl = versionUrl;
		this.init = function() {
			if(last !== undefined) {
				if(today - last >= 7 || today - last <= -24) {
					GM_setValue(title, today);
					this.check();
				}
			} else {
				GM_setValue(title, today);
				this.check();
			}				
		}
		this.check = function() {
			GM_xmlhttpRequest({
				method:"GET",
				url:versionUrl,
				onreadystatechange:this.finish
			});
		}
		this.finish = function(o) {
			if(o.readyState == 4) {
				current = o.responseText;
				current = current.split(".");
				version = version.split(".");
				if(version[0] < current[0]) {
					answer = confirm("Update " + title + " to version " + current.join(".") + "?");
					if(answer) {	GM_openInTab(updateUrl); }
				} else if(version[1] < current[1]) {
					answer = confirm("Update " + title + " to version " + current.join(".") + "?");
					if(answer) {	GM_openInTab(updateUrl); }
				} else if(version[2] < current[2]) {
					answer = confirm("Update " + title + " to version " + current.join(".") + "?");
					if(answer) {	GM_openInTab(updateUrl); }
				} else {
					// up to date
				}
			}
		}
		//start
		this.init();
	}
GM_update('sandbox2', '1.1.1', 'http://jeremy.tymes.name', 'http://test.jtymes.net/js/version.txt');