Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
AutoUpdater Greasemonkey
// Checks if there is a new script version according to the version information in the script homepage
// The version information is in a line in the full description of the script: "<p>#[V:00000000]#</p>" (00000000 is the version number)
// If the request is successful and there is a new version available, a message to the user is displayed
// TODO
// Change scriptPage - Number on the URL in userscripts link page.
// Change scipptVersion - Version of the script in this file
// Also add <p>#[V:00000000]#</p> in the page as a description
function scriptCheckVersion() {
var scriptPage = " ";
var scriptVersion = "2";
var scriptHomepageURL = "http://userscripts.org/scripts/show/__SCRIPT__".replace(/__SCRIPT__/g,scriptPage);
var scriptFileURL = "http://userscripts.org/scripts/source/__SCRIPT__.user.js".replace(/__SCRIPT__/g,scriptPage);
GM_xmlhttpRequest({
method: "GET",
url: scriptHomepageURL,
onload: function(evt) {
if ((evt.readyState == 4) && (evt.status == 200)) {
var responseMatch = evt.responseText.match(/<p>#\[V:(\d+)]#<\/p>/);
var remoteVersion = (responseMatch === null) ? NaN : parseInt(responseMatch[1], 10);
if (isNaN(remoteVersion)) return;
if (remoteVersion <= scriptVersion) {
return;
}
var messageDiv = document.getElementById("gsscriptVersionMessage");
if (messageDiv) {
messageDiv.style.display = "";
}
else {
messageDiv = document.createElement("div");
messageDiv.id = "gsscriptVersionMessage";
messageDiv.style.border = "SOLID 1px BLACK";
messageDiv.style.backgroundColor = "RED";
messageDiv.style.fontSize = "1.2em";
messageDiv.style.padding = "10px";
messageDiv.style.left = 0;
messageDiv.style.top = 0;
messageDiv.style.zIndex = 100000;
messageDiv.style.position = "absolute"
messageDiv.innerHTML = "<center>A new version is available<br><br>" +
"<a id='gsscriptVersionMessageInstall' href='" + scriptFileURL + "' title='Install the script update'>Install</a>" +
"&nbsp;|&nbsp;<a href='" + scriptHomepageURL + "' target='_blank' title='Go to homepage'>Go to web page</a>" +
"&nbsp;|&nbsp;<a id='gsscriptVersionMessageHide' href='javascript:void(null)' title='Hide the notice for this session'>Hide</a></center>";
document.body.appendChild(messageDiv);
document.getElementById("gsscriptVersionMessageHide").addEventListener("click", function(evt) {
var messageDiv = document.getElementById("gsscriptVersionMessage");
if (messageDiv){
messageDiv.style.display = "none";
}
}, false);
}
}
}
});
}
scriptCheckVersion();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment