Skip to content

Instantly share code, notes, and snippets.

@lolney
Last active January 1, 2020 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lolney/d4cdb917e4ab28f2e16938a552204feb to your computer and use it in GitHub Desktop.
Save lolney/d4cdb917e4ab28f2e16938a552204feb to your computer and use it in GitHub Desktop.
Disable Messenger calls after making 3 in a single day
// ==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