How to write script updater for Greasemonkey scripts
by Arxleol on Thursday 05.11.2009, under javascript, tutorial
I wrote script KingsageX and wanted to have auto-updater. So that anyone who uses script gets notified when new version is out. As I had many requests, emails and such that script doesn’t work but what in fact happened was that people didn’t updated their script to new version. If you have similar problems or just want that all of your users have script updated to the latest version keep reading.Also I would like to note that I wanted this solution to be as simple as possible.
So we will work here on the example of kingsageX but your script will not require many changes.
First thing to do is to add variable version to the script. At the time of writing I was working on kingsageX version 0.60 so here is example:
//Current scripts version var version = 0.60;
Second thing important for checking purposes is to check UserScript tag field it should resemble following:
// ==UserScript== // @name KingsAgeX // @namespace http://axino.net/code/ // @description script that extends functionallity of the free online game KingsAge // @version 0.60 // @author Arxleol // @copyright Axino.net (http://www.axino.net/) // @include http://*.kingsage.*/* // @include http://*.kingsage.*.*/* // ==/UserScript==
I suggest that version is fifth field but if you have version somewhere else I will instruct later what additional change needs to be done.
Now here is function that checks if there is new update available or as a matter of fact checks whether there is new version available.
//this function check if there is new version of script available window.checkVersion = function(){ var tmp = new Date().getTime(); //Obtains time in milliseconds var tm = GM_getValue("tcv", 0); // Obtains value when last check was performed, default value is 0 if(tm == 0){ //Checks if check was performed before GM_setValue("tcv", parseInt(tmp/1000)); //If check was not performed this means that user just installed latest version of script, and we therefore save time in seconds when check was performed }else{ if(tmp/1000 - parseInt(tm) >= 90000){ // This part checks whether check was performed in last 25 hours. You can set your own timing by chaining value 90000 to something else. Note that this value is in seconds. GM_setValue("tcv", parseInt(tmp/1000)); //Now we save time so that it was checked for new version. //this part connects to userscripts meta site and checks values for your script GM_xmlhttpRequest({ method: 'GET', url: 'http://userscripts.org/scripts/source/51469.meta.js', //Here you will change number 51469 to number of your script headers: { 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey', 'Accept': 'application/html,application/xml,text/xml', }, onload: function(responseDetails) { var text = responseDetails.responseText; //We obtain contents of the site vs=text.split("\n"); //We split content by rows vers = vs[4].substring(12); // Here we obtain version of the script if by some case you have your @version tag in some other row then fifth then change number 4 to some other value that responds to your code if(version != parseFloat(vers)){ // We check if new version is out alert("New version of KingsAgeX is out!");// This is notification to user that new version is available window.location = "http://userscripts.org/scripts/source/51469.user.js"; //If new version is out install new version of the script. NOTE: that you have to change number 51469 to the number of your script on userscripts. window.reload(); } } }); } } }
Now I now there are some changes but this updater is not as intrusive as some others and works for sure
The very last thing to do is to call function for updating. You can call it from anywhere in your code.
//check for new version window.checkVersion();
If you have any questions please do ask? If you liked it well then write it so I know ![]()
Sunday 15.11.2009 on 14:36
It would be possible to know where you should put these scripts even for those not very practical in this work, through your Fans xd
Sunday 15.11.2009 on 16:22
Which part you do not understand. First part is at the beggining of the script.
Second is at the beginning of the code.
You must put function before you call function.