Last active
January 22, 2018 17:19
-
-
Save MightyPork/f35be15eb44364c9111823003bab2fc9 to your computer and use it in GitHub Desktop.
MastoThrottle
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
// ==UserScript== | |
// @name MastoThrottle | |
// @namespace http://tampermonkey.net/ | |
// @version 1 | |
// @description Stops you from wasting unhealthy amounts of time on Mastodon | |
// @author @[email protected] | |
// @match https://dev.glitch.social/web/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const OPEN_TIME_MIN = 5 | |
const LOCKED_TIME_MIN = 30 | |
let time = () => Math.round(+new Date()/1000) | |
const now = time() | |
let locked = false | |
let t_opened = localStorage.getItem("mt_opentime") | |
if (t_opened === null) { | |
t_opened = time() | |
localStorage.setItem("mt_opentime", t_opened) | |
} else { | |
t_opened = +t_opened | |
} | |
let t_close = t_opened + OPEN_TIME_MIN*60 | |
locked = (now > t_close) | |
let isUnlockAllowed = () => { | |
return time() >= t_close + LOCKED_TIME_MIN*60 | |
} | |
/* build the overlay */ | |
let overlay; | |
{ | |
overlay = document.createElement('DIV') | |
overlay.style.display = 'none' | |
overlay.style.fontSize = '48pt' | |
overlay.style.background = 'white' | |
overlay.style.position = 'absolute' | |
overlay.style.left = '0' | |
overlay.style.top = '0' | |
overlay.style.right = '0' | |
overlay.style.bottom = '0' | |
overlay.style.zIndex = '999' | |
overlay.style.color = '#4AA5FF' | |
overlay.textContent = '429 Throttled' | |
let masto = document.getElementById('mastodon') | |
masto.appendChild(overlay) | |
} | |
let doUnlock = () => { | |
console.log("[MastoThrottle] Unlocked") | |
t_close = now + OPEN_TIME_MIN*60 | |
t_opened = now; | |
localStorage.setItem("mt_opentime", t_opened) | |
overlay.style.display = 'none' | |
locked = false | |
} | |
let doLock = () => { | |
console.log("[MastoThrottle] Opening overlay") | |
locked = true | |
updateLockedText() | |
overlay.style.display = 'flex' | |
} | |
let updateLockedText = () => { | |
if (isUnlockAllowed()) { | |
overlay.textContent = 'Click here to begin' | |
overlay.style.cursor = 'pointer' | |
} else { | |
let remain = t_close+LOCKED_TIME_MIN*60 - time() | |
let t = `${remain} s` | |
if (remain > 60) { | |
t = `${Math.floor(remain/60)}:${(''+(remain%60)).padStart(2, '0')}` | |
} | |
overlay.textContent = `429 Throttled | ${t}` | |
overlay.style.cursor = 'default' | |
} | |
} | |
overlay.addEventListener('click', () => { | |
if (isUnlockAllowed()) { | |
doUnlock(); | |
} | |
}) | |
console.log(`[MastoThrottle] Started, open time ${OPEN_TIME_MIN} min, lock time ${LOCKED_TIME_MIN} min`) | |
console.log(`[MastoThrottle] Last open ${Math.round((now-t_opened)/6)/10} min ago`) | |
// Apply the initial lock | |
if (locked) doLock() | |
// Uncomment this to auto unlock if timeout is expired | |
/* | |
if (isUnlockAllowed()) { | |
doUnlock(); | |
} | |
*/ | |
setInterval(function() { | |
if (locked) { | |
updateLockedText() | |
} | |
if (!locked && time() >= t_close) { | |
doLock() | |
} | |
}, 1000) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment