Skip to content

Instantly share code, notes, and snippets.

@johntynan
Forked from johnfmorton/CreateBookmarklet.html
Last active March 3, 2017 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johntynan/fb858830754b424b222a28c536f8505e to your computer and use it in GitHub Desktop.
Save johntynan/fb858830754b424b222a28c536f8505e to your computer and use it in GitHub Desktop.
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/johntynan/fb858830754b424b222a28c536f8505e/raw/f80b30fcb17daf6452a4292f2e2b88eb478174b2/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.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment