Last active
December 20, 2015 14:38
-
-
Save egorvinogradov/6147628 to your computer and use it in GitHub Desktop.
User script to struggle against procrastination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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