You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
misc/greasemonkey/virtualblinders.user.js

93 lines
3.5 KiB

// ==UserScript==
// @name VirtualBlinders
// @namespace Utopiah
// @description Stay focus, damnit!
// @include *youtube.com*
// ==/UserScript==
/* TODO
# handle background tab
## stop timer when focus lost window.onunload or window.onblur
## restart timer when focus is back window.load or window.onfocus
## XXX what is happening when 2 background tabs are increasing the timer?!
# improve the list of websites to include
## consider include * then refine window.location pattern per task
# make timers (default to 0 sec, value defined via the script or a GM_Value) to be more flexible
## reset timer either manually or per day (i.e.) reset if last set date <5AM
## set 2 thresholds, the first as a warning, the second as an action
# allow to check the timer while the page is loaded
## else one could just arrive before the threshold and pass it unknowingly
### e.g. not with Wikipedia but with YouTube
Note that it does work even when Flash has focus.
*/
var t;
var MODE="video";
var time_spent = GM_getValue(MODE,0);
var cognitiveEnvironmentURL = "http://fabien.benetou.fr/CognitiveEnvironments/";
GM_xmlhttpRequest({
method: "GET",
url: "http://fabien.benetou.fr/pub/currenttask",
onload: function(response) {
task = response.responseText;
responseCheck(task);
}
});
function responseCheck(task) {
//task = 'Reading\n';
timeThreshold = 600;
aboveThreshold = ( timeThreshold < time_spent );
aboveThreshold = true; //kept until actived to maintain previous behavior
if (task == 'Reading\n' || task == 'Programming\n'){
if ( !aboveThreshold ) {
//under threshold, just display a warning and the content
document.body.innerHTML="You should be focusing on <a href=\""+cognitiveEnvironmentURL+task+"\">"
+ task + "</a> instead,\nare you sure you need that information from that website?"
+ "<br/>Note that you have already spent <span id='timespent'>"
+ time_spent/60 + "</span> minute(s) on that website so far!<br/>"
+ "(which still is under the threshold of "+timeThreshold/60+" minutes)<hr/>"
+document.body.innerHTML;
t=window.setInterval(mytimer,1000);
//regarding the specific syntax
//see http://commons.oreilly.com/wiki/index.php/Greasemonkey_Hacks/Getting_Started#Pitfall_.231:_Auto-eval_Strings
GM_log("register listeners");
window.addEventListener("blur", stopTimer, true);
window.addEventListener("focus", restartTimer, true);
window.addEventListener("unload", setTotalTimeSpent, true);
} else {
//above threshold, only display the warning without the content
document.body.innerHTML="You should be focusing on <a href=\""+cognitiveEnvironmentURL+task+"\">"
+ task + "</a> instead,\nare you sure you need that information from that website?"
+ "<br/>Note that you have already spent <span id='timespent'>"
+ time_spent/60 + "</span> minute(s) on that website so far!<br/>"
+ "(which is above the allowed threshold of "+timeThreshold/60+" minutes)<hr/>";
}
}
}
function mytimer() {
//GM_log("timer tick");
time_spent=time_spent+1;
//fail document.getElementById('timespent').innerText=time_spent;
}
function stopTimer() {
GM_log("stop timer");
window.clearInterval(t);
}
function restartTimer() {
GM_log("restart timer");
t=window.setInterval(mytimer,1000);
}
function setTotalTimeSpent() {
GM_log("set total time spent");
GM_setValue(MODE,time_spent);
// probably better to re-read the value then add the time ellapsed since
}