Created
October 15, 2019 11:07
-
-
Save oriadam/eb426a74b06d2e7c25e79e489136e67a to your computer and use it in GitHub Desktop.
This file contains 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
// ==UserScript== | |
// @name The Facebook Limiter | |
// @namespace http://tampermonkey.net/ | |
// @version 4 | |
// @description Helps in your goals to limit the use of youtube/facebook to 1 hour. Edited by Oria. | |
// @author theKidOfArcrania | |
// @include http*://www.facebook.com* | |
// ==/UserScript== | |
/* jshint -W097 */ | |
'use strict'; | |
// based on this script https://greasyfork.org/en/scripts/24074-the-youtube-limiter | |
var maxEnergy = 3600; // max charge time in seconds | |
var rate = 6; // charge 1 second for every this amount of seconds | |
var energy = 0; //energy in seconds | |
var timerPopup = document.createElement("P"); | |
var error = document.createElement("P"); | |
var timer = document.createElement("SPAN"); | |
var updater; | |
var lastUpdate; | |
var armed = false; | |
var timerText = document.createTextNode("Time left: "); | |
var setValue(key,val) { | |
localStorage.setItem('userscript_limiter_'+key,val); | |
} | |
var getValue(key,default) { | |
let val = localStorage.getItem('userscript_limiter_'+key); | |
return val === null ? default : val; | |
} | |
timerPopup.style = "z-index: 2000000000; background: yellow; font-family: arial; position: fixed; font-size: 20px;"; | |
timer.style = "font-weight: bold"; | |
timerPopup.appendChild(timerText); | |
timerPopup.appendChild(timer); | |
error.style = "z-index=2000000000; background=yellow; font-family: arial; position: fixed; font-size: 20px; color: red"; | |
error.innerHTML = "We're sorry. But you have used up all your time. Please refresh this page later to see how much energy you recharged..."; | |
document.body.insertBefore(timerPopup, document.body.childNodes[0]); | |
lastUpdate = new Date(); | |
//Arming/ disarming of youtube | |
arm(); | |
document.addEventListener("visibilitychange", function() { | |
if (document.hidden) { | |
unArm(); | |
}else { | |
arm(); | |
} | |
}); | |
document.addEventListener("unload", unArm, true); | |
//Arms a youtube page with a timer | |
function arm() { | |
if (armed) return; | |
armed = true; | |
var now = new Date().getTime(); | |
var last = getValue("INACTIVE", now); | |
var elapsed = now - last; | |
//Recharge energy with exchange rate of 1 minute of no activity | |
//yielding 10 second of watching time. | |
energy = getValue("ENERGY", 4) + elapsed / (1000 * rate); | |
energy = Math.min(energy, maxEnergy); | |
lastUpdate = new Date(); | |
updater = setInterval(updateEnergy, 4); | |
} | |
//Unarms the limiter, recharge energy for next use. | |
//Called whenver the window is minimized or unfocused | |
function unArm() { | |
if (!armed) return; | |
armed = false; | |
setValue("INACTIVE", new Date().getTime()); | |
setValue("ENERGY", energy); | |
clearInterval(updater); | |
} | |
//Checks the amount of time left | |
function updateEnergy() { | |
var before = lastUpdate; | |
lastUpdate = new Date(); | |
var after = lastUpdate; | |
energy -= (after - before) / 1000; | |
energy = Math.max(energy, 0); | |
if (energy > (maxEnergy/2)) { | |
timer.style.color = "green"; | |
}else if (energy > (maxEnergy/4)){ | |
timer.style.color = "goldenrod"; | |
}else { | |
timer.style.color = "red"; | |
} | |
setValue("INACTIVE", new Date().getTime()); | |
setValue("ENERGY", energy); | |
if (energy <= 0) { | |
unArm(); | |
document.body.innerHTML = ""; | |
document.body.appendChild(error); | |
}else { | |
var secs = Math.floor(energy) % 60; | |
var mins = Math.floor(energy / 60) % 60; | |
var hrs = Math.floor(energy / 3600) % 24; | |
timer.innerHTML = padTime(hrs) + ":" + padTime(mins) + ":" + padTime(secs); | |
} | |
} | |
function padTime(t) { | |
var a = Math.abs(t); | |
if (a < 10) | |
return (t < 0 ? "-0" : "0") + a; | |
else | |
return (t < 0 ? "-" : "") + a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment