Skip to content

Instantly share code, notes, and snippets.

@kelleyvanevert
Last active May 3, 2018 16:41
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 kelleyvanevert/6270be896191030276e686fd7940d06d to your computer and use it in GitHub Desktop.
Save kelleyvanevert/6270be896191030276e686fd7940d06d to your computer and use it in GitHub Desktop.
A little chunk of code to cheat in a counting Whatsapp group
// config
var only_respond = true; // if false, responds to own messages as well
// state
var lastno = 34350; // the last known posted number
var mine = true; // was this last number my own post?
// The complication here, is that
// Whatsapp removes/replaces elements on-the-fly
// sometimes; e.g. when messages have been deleted, etc.
// And of course, number posts are intersperced with
// regular conversation.
// The most robust method to check for new numbers, then,
// is to just check a reasonable backlog of messages for
// new numbers, and to this often enough.
function get_most_recent () {
// var wrap = document.querySelector('._9tCEa');
var wrap = document.querySelector('#main header').nextSibling.lastChild.lastChild.lastChild;
for (var i = Math.max(0, wrap.children.length - 20); i < wrap.children.length; i++) {
var msg = wrap.children[i];
var el = msg.querySelector('._3zb-j.ZhF0n');
if (el) {
var text = el.innerText;
// Check for correct number formatting
// so as not to get tricked again. (Stijn !)
var m = text.match(/^([0-9]\.|[0-9]{2}\.)([0-9]{3}\.)*[0-9]{3}$/);
if (m) {
var n = parseInt(text.replace(/\./g, ''));
if (n == lastno + 1) {
lastno = n;
maybe_congratulate(lastno, true);
mine = !!msg.querySelector('.message-out');
console.log(`Found: ${lastno} ${mine ? ' (mine)' : ''}`);
}
}
}
}
}
// Enter number, and simulate an "input" event to
// get Whatsapp to show the "post message" icon
function enter (str) {
var input = document.querySelector('[contenteditable]');
input.innerText = str;
var e = new Event('input', { bubbles: true, cancelable: true });
input.dispatchEvent(e);
}
// Simulate button press
function post () {
var button = document.querySelector('button [data-icon="send"]').parentElement;
var e = document.createEvent('MouseEvent');
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null);
button.dispatchEvent(e);
}
var waiting_time = () => 2 + 8 * Math.random();
/*
function waiting_time (n1) {
if (n1 % 100 == 0) {
console.log(` >> multiple of 100 ! (I'll respond immediately)`);
return 0;
}
if ((n1+'') == (n1+'').split('').reverse().join('')) {
console.log(` >> palindrome ! (I'll respond immediately)`);
return 0;
}
var rand = Math.random();
if (rand <= .05) {
console.log(` >> very long wait (5 min, 5% chance)`);
return 5 * 60;
}
if (rand <= .1) {
console.log(` >> long wait (1 min, 5% chance)`);
return 60;
}
if (rand <= .8) {
console.log(` >> regular wait (±9s, 70% chance)`);
return 7 + 4 * Math.random();
}
console.log(` >> short wait (±2s, 20% chance)`);
return 1 + 2 * Math.random();
}
*/
var r;
function maybe_respond () {
var n = lastno;
console.log(`I may respond to: ${n}`);
var formatted = ((n+1)+'').split('').reduceRight((str, c) => c+(str.replace(/\./, '').length % 3 == 0 ? '.' : '')+str);
var wait = waiting_time(n+1);
console.log(`=> Waiting ${wait}s ...`);
r = {
responding_to: n,
h: setTimeout(() => {
if (n != lastno) {
console.log(`=> No longer timely! ${n} != ${lastno} (current)`);
} else {
enter(formatted);
post();
maybe_congratulate(lastno = n+1, false);
mine = true;
console.log(`=> Just responded ${formatted}`);
}
}, wait*1000),
};
}
function maybe_congratulate (n, other) {
if (n % 1000 == 0) {
enter(`🦑🦐🦀 Een duizendtal! ${other ? 'Gefeliciteerd!' : ''}`);
post();
return;
}
if (n % 100 == 0) {
enter(`☘🍀 Een honderdtal! ${other ? 'Gefeliciteerd!' : ''}`);
post();
return;
}
if ((n+'') == (n+'').split('').reverse().join('')) {
enter(`🐛🦋 Een palindroom! ${other ? 'Gefeliciteerd!' : ''}`);
post();
return;
}
}
function loop () {
get_most_recent();
// spin off a helper function that may respond to this number (after some artifical delay)
if (!mine) {
if (!r || r.responding_to != lastno) {
if (r) clearTimeout(r.h);
maybe_respond();
}
}
// ...but keep this loop running fast in order to keep track of the counting
requestAnimationFrame(loop);
};
/*
USAGE:
1. open web whatsapp and go to the group in question (don't navigate afterwards, because then the `wrap` element changes)
2. copy code into Firefox' (Chrome's?) devtools
3. set `lastno` and `mine` accordingly
4. call `loop()`
5. sit back and enjoy :)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment