hello,
I am writing a greasemonkey userscript to autosave a page. The below code works fine (no cross-browser dramas as the page will only ever be opened in firefox). This is what it's supposed to do:
- a certain time (10 seconds here) after page load, the save button is clicked programatically, unless it is disabled (it becomes disabled when it saves and is only enabled once changes are made to the page).
- The 10 second countdown starts again, either from the "else" in the clickIt function or from the onclick of the button.
- If the save button is clicked manually, the timeOut is cleared and the 10 second countdown starts again.
Anyway, like I say, the below code works fine - it just seems a little clunky. Can anybody see a better way of doing this?
thanks in advance...
Code:
<body>
<input type="button" value="save" id="store"/>
<script>
(function() {
var auto_save_timer=setTimeout(clickIt,10000);
function clickIt(){
if (!document.getElementById("store").disabled){
document.getElementById("store").click();
} else {
auto_save_timer=setTimeout(clickIt,10000);
}
}
document.getElementById("store").onclick=function() {
alert("saved");
if(auto_save_timer){
clearTimeout(auto_save_timer)
}
auto_save_timer=setTimeout(clickIt,10000);
};
})();
</script>
</body>