timer and threshold working, background time to not though

master
Fabien Benetou 13 years ago
parent c9ae718b8e
commit 12d9339ded
  1. 65
      greasemonkey/virtualblinders.user.js

@ -6,9 +6,12 @@
// ==/UserScript== // ==/UserScript==
/* TODO /* 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 failing so far
# improve the list of websites to include # improve the list of websites to include
## consider include * then refine pattern per task ## 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 # 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 ## 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 ## set 2 thresholds, the first as a warning, the second as an action
@ -26,17 +29,65 @@ GM_xmlhttpRequest({
url: "http://fabien.benetou.fr/pub/currenttask", url: "http://fabien.benetou.fr/pub/currenttask",
onload: function(response) { onload: function(response) {
task = response.responseText; task = response.responseText;
responseCheck(task);
}
});
function responseCheck(task) {
//task = 'Reading\n';
timeThreshold = 600;
aboveThreshold = ( timeThreshold < time_spent );
//aboveThreshold = true;
if (task == 'Reading\n' || task == 'Programming\n'){ if (task == 'Reading\n' || task == 'Programming\n'){
//document.body.innerHTML="You should be focusing on <a href=\"http://fabien.benetou.fr/CognitiveEnvironments/" + task + "\">"+task+"</a> instead,\nare you sure you need that information from that website?"; if ( !aboveThreshold ) {
document.body.innerHTML="You should be focusing on <a href=\"http://fabien.benetou.fr/CognitiveEnvironments/" + 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+"</span> seconds on that website so far!<hr/>"+document.body.innerHTML; //under threshold, just display a warning and the content
t=setInterval(mytimer,1000); document.body.innerHTML="You should be focusing on <a href=\"http://fabien.benetou.fr/CognitiveEnvironments/"
+ 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 + "</span> seconds on that website so far!"
+ "(which still is under the threshold of "+timeThreshold+" seconds)<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 //see http://commons.oreilly.com/wiki/index.php/Greasemonkey_Hacks/Getting_Started#Pitfall_.231:_Auto-eval_Strings
GM_log("register listeners");
window.addEventListener("onblur", stopTimer, true);
window.addEventListener("onfocus", restartTimer, true);
window.addEventListener("onunload", setTotalTimeSpent, true);
} else {
//above threshold, only display the warning without the content
document.body.innerHTML="You should be focusing on <a href=\"http://fabien.benetou.fr/CognitiveEnvironments/"
+ 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 + "</span> seconds on that website so far!<br/>"
+ "(which is above the allowed threshold of "+timeThreshold+" seconds)<hr/>";
}
} }
} }
});
function mytimer() { function mytimer() {
//GM_log("timer tick");
time_spent=time_spent+1; time_spent=time_spent+1;
document.getElementById('timespent').innerText=time_spent; //fail document.getElementById('timespent').innerText=time_spent;
GM_setValue(MODE,time_spent);
// this should only be un setTotalTimeSpent() but since the events do not work this will remain here for now
}
function stopTimer() {
GM_log("stop timer");
// never called!
window.clearInterval(t);
// tested and worked, t value is correct
}
function restartTimer() {
GM_log("restart timer");
t=window.setInterval(mytimer,1000);
}
function setTotalTimeSpent() {
GM_log("set total time spent");
GM_setValue(MODE,time_spent); GM_setValue(MODE,time_spent);
} }

Loading…
Cancel
Save