Skip to content

Instantly share code, notes, and snippets.

@egorvinogradov
Last active December 20, 2015 14:38
Show Gist options
  • Select an option

  • Save egorvinogradov/6147628 to your computer and use it in GitHub Desktop.

Select an option

Save egorvinogradov/6147628 to your computer and use it in GitHub Desktop.
User script to struggle against procrastination
(function(){
var hosts = [
'vk.com',
'last.fm',
'lastfm.ru',
'wikipedia.org',
'lurkmore.to',
'habrahabr.ru',
'lenta.ru',
'facebook.com',
'youtube.com'
];
var message = 'Self-control';
var interval = 20 * 60 * 1000; // 20 minutes
var localStorageInitializedPrefix = '__selfControl__';
function getCurrentHost(){
var host = location.hostname.split('.');
return host[host.length-2] + '.' + host[host.length-1];
};
function initialize(){
function showMessage(){
alert(message);
localStorage.setItem(localStorageTimePrefix, +new Date());
}
var localStorageTimePrefix = '__selfControlTime__';
var startTime;
var currentTime = +new Date();
var previousTime = +localStorage.getItem(localStorageTimePrefix);
var timeShift = currentTime - previousTime;
if ( previousTime && timeShift < interval ) {
startTime = previousTime;
setTimeout(function(){
showMessage();
setInterval(showMessage, interval);
}, interval - timeShift)
}
else {
startTime = currentTime;
localStorage.setItem(localStorageTimePrefix, startTime);
setInterval(showMessage, interval);
}
onbeforeunload = function(){
localStorage.removeItem(localStorageInitializedPrefix);
};
localStorage.setItem(localStorageInitializedPrefix, true);
};
function isInitialized(){
return localStorage.getItem(localStorageInitializedPrefix);
};
if ( window.top === window.self ) {
var currentHost = getCurrentHost();
for ( var i = 0, l = hosts.length; i < l; i++ ) {
if ( hosts[i] === currentHost ) {
if ( isInitialized() ) {
var tempInterval = setInterval(function(){
if ( !isInitialized() ) {
clearInterval(tempInterval);
initialize();
}
}, 1000);
}
else {
initialize();
}
break;
}
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment