Last active
January 1, 2020 00:56
-
-
Save lolney/d4cdb917e4ab28f2e16938a552204feb to your computer and use it in GitHub Desktop.
Disable Messenger calls after making 3 in a single day
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 Limit Messenger Calls | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Disable Messenger calls after making 3 in a single day | |
// @author You | |
// @match https://www.messenger.com/t/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
const text = { | |
voice: "Start a voice call", | |
video: "Start a video chat" | |
}; | |
const CALLS_THRESHOLD = 3; | |
class CallButtons { | |
constructor() { | |
const array = Array.from(document.getElementsByTagName('div')); | |
this.array = array.filter(elem => elem.title && elem.title.contains(text.voice) || elem.title.contains(text.video)); | |
} | |
elements() { | |
return this.array; | |
} | |
remove() { | |
this.array.forEach(elem => elem.parentNode.remove()); | |
}; | |
} | |
class CallsCounter { | |
static dateIsToday(date) { | |
const now = new Date(); | |
const isToday = now.getDate() === date.getDate() && now.getMonth() === date.getMonth() && now.getYear() === date.getYear(); | |
return isToday; | |
} | |
static reset() { | |
localStorage.setItem('timesCalledToday', 0); | |
localStorage.setItem('calledOnDay', new Date()); | |
} | |
static get() { | |
let lastSet = localStorage.getItem('calledOnDay'); | |
if(!lastSet || !CallsCounter.dateIsToday(new Date(lastSet))) | |
CallsCounter.reset(); | |
let calledToday = localStorage.getItem('timesCalledToday'); | |
return calledToday ? parseInt(calledToday) : 0; | |
} | |
static increment() { | |
const calls = CallsCounter.get() + 1; | |
localStorage.setItem('timesCalledToday', calls); | |
console.log('called ' + calls); | |
return calls; | |
} | |
} | |
const hideButtonsIfMatchingThreshold = (buttons) => { | |
const timesCalled = CallsCounter.get(); | |
if(timesCalled === CALLS_THRESHOLD) { | |
buttons = buttons || new CallButtons(); | |
buttons.remove(); | |
} | |
} | |
const handleClickCallButtons = () => { | |
const calls = CallsCounter.increment(); | |
if(calls === CALLS_THRESHOLD) | |
alert('This is your last call today!'); | |
hideButtonsIfMatchingThreshold(); | |
} | |
const observer = new MutationObserver(() => { | |
const buttons = new CallButtons(); | |
hideButtonsIfMatchingThreshold(buttons); | |
buttons.elements().forEach(elem => { | |
elem.onclick = handleClickCallButtons; | |
}) | |
}); | |
const main = async () => { | |
observer.observe(document, {attributes: false, childList: true, characterData: false, subtree:true}); | |
} | |
main(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment