Axino.net

Correction of auto-updater for Greasemonkey scripts

by Arxleol on Tuesday 08.12.2009, under javascript, tutorial

In the auto-updater for userscripts there was small bug. It is now of course corrected.


//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();
 
}
 
}
});
 
}
 
}
 
}

This is corrected version of auto-updater. Now problem was that alert saying that new version is available would fire each 25 hours or depending how you set timer.

To solve this alert was moved into the loop when new version is confirmed.

Similar Posts:

:, , , ,
No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Archives