Create a gist now

Instantly share code, notes, and snippets.

Bookmarklet to auto-refresh any page at some point in the near future.
<!DOCTYPE html>
<html>
<head>
<title>Page Refresher</title>
</head>
<body>
<h1>Page Refresher</h1>
<p>Drag the link below to your toolbar to install the "page refresher" bookmarklet. Press the bookmarklet and you will be prompted for when you'd like the page you're on to be refreshed in your browser.</p>
<a href="javascript: (function () { var jsCode = document.createElement('script'); jsCode.setAttribute('src', 'https://gist.githubusercontent.com/johnfmorton/9238562/raw/b2a65b62c17569d3d275b9f2a755d14658e8d036/pagerefresher.js'); document.body.appendChild(jsCode); }());">Refresher</a>
</body>
</html>
// prevent IE errors when using console
if (typeof console === "undefined") {
window.console = {
log: function () {}
};
}
var inFuture = new Date(new Date().getTime() + 10*60000);
var inHour = inFuture.getHours();
var inMin = (inFuture.getMinutes()<10?'0':'') + inFuture.getMinutes();
var inputdata = prompt("What time should this page refresh?\nThis is a 24 clock. 0:00 = Midnight, 13:00 = 1pm\nThe default is for 10 minutes from now: "+inHour+":"+inMin,inHour+":"+inMin);
function refreshAt(hours, minutes, seconds) {
var now = new Date();
var then = new Date();
if(now.getHours() > hours ||
(now.getHours() == hours && now.getMinutes() > minutes) ||
now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {
then.setDate(now.getDate() + 1);
}
then.setHours(hours);
then.setMinutes(minutes);
then.setSeconds(seconds);
var timeout = (then.getTime() - now.getTime());
setTimeout(function() { window.location.reload(true); }, timeout);
var reportedHour = hours;
var ampm = 'am';
if (reportedHour > 12) {
reportedHour -= 12;
ampm = 'pm';
}
console.log('This page will refresh at ' + reportedHour + ":" + (minutes<10?'0':'')+minutes + ampm);
}
if (null !== inputdata) {
var newtime = inputdata.split(':');
console.log(newtime);
refreshAt(parseInt(newtime[0]),parseInt(newtime[1]),0);
} else {
console.log("No value submitted.")
}
@johnfmorton
Owner

I've only tested this in Chrome on a Mac. If there is any interest, I can see about other browsers and platforms. Feel free to fork as well. Cheers. -John

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment