Skip to content

Instantly share code, notes, and snippets.

@msaglietto
Created August 24, 2017 20:55
Show Gist options
  • Save msaglietto/4c4621ee80abf1b175757ce73b198f30 to your computer and use it in GitHub Desktop.
Save msaglietto/4c4621ee80abf1b175757ce73b198f30 to your computer and use it in GitHub Desktop.
((global) => {
//
// GLOBAL VARS AND CONFIGS
//
let waitTime = 5;
let waitForAnswer = true;
const ignoreLastMsg = {};
const jokeList = [
"When Chuck Norris was born he drove his mom home from the hospital.",
"Chuck Norris has a diary. It's called the Guinness Book of World Records.",
"Chuck Norris threw a grenade and killed 50 people, then it exploded.",
"Chuck Norris doesn't worry about high gas prices. His vehicles run on fear.",
"Chuck Norris counted to infinity. Twice.",
"Chuck Norris can kill two stones with one bird.",
"When a zombie apocalypse starts, Chuck Norris doesn't try to survive. The zombies do.",
"There once was a street called Chuck Norris, but t…ty because nobody crosses Chuck Norris and lives.",
"Big foot claims he saw Chuck Norris.",
"Chuck Norris can hear sign language.",
"Chuck Norris can pick oranges from an apple tree and make the best lemonade youve ever tasted.",
"It is considered a great accomplishment to go down…orris can go up Niagara Falls in a cardboard box.",
"Once a cobra bit Chuck Norris' leg. After five days of excruciating pain, the cobra died.",
"Chuck Norris doesn't cheat death. He wins fair and square.",
"Chuck Norris beat the sun in a staring contest.",
"Giraffes were created when Chuck Norris uppercutted a horse.",
"When the Boogeyman goes to sleep every night he checks his closet for Chuck Norris.",
"Chuck Norris will never have a heart attack... eve…heart isnt foolish enough to attack Chuck Norris.",
"Chuck Norris once went to mars. Thats why there are no signs of life.",
"Chuck Norris is the reason Waldo is hiding.",
"Jack was nimble, Jack was quick, but Jack still couldn't dodge Chuck Norris' roundhouse kick.",
"Chuck Norris can strangle you with a cordless phone.",
"When Chuck Norris was in middle school, his Englis…ng in a blank page with only his name at the top.",
"There is no theory of evolution, just a list of creatures Chuck Norris allows to live.",
"Chuck Norris' calendar goes straight from March 31st to April 2nd. No one fools Chuck Norris.",
"Chuck Norris once fought superman. The loser had to wear his underwear over his pants.",
"Chuck Norris can kill your imaginary friends.",
"Chuck Norris's computer has no \"backspace\" button, Chuck Norris doesn't make mistakes.",
"Chuck can set ants on fire with a magnifying glass. At night.",
"Chuck Norris makes onions cry.",
"The reason the Holy Grail has never been recovered…Chuck Norris to give up his favourite coffee mug.",
"When Bruce Banner gets mad he turns into the Hulk.…to Chuck Norris. When Chuck Norris gets mad, run.",
"Chuck Norris is the only person that can punch a cyclops between the eye.",
"When Chuck Norris enters a room, he doesn't turn the lights on, he turns the dark off.",
"M.C. Hammer learned the hard way that Chuck Norris can touch this.",
"Chuck Norris plays russian roulette with a fully loded revolver... and wins.",
"Chuck Norris can build a snowman out of rain.",
"Chuck Norris was once charged with three attempted…s because Chuck Norris does not \"attempt\" murder.",
"Bill Gates lives in constant fear that Chuck Norris' PC will crash.",
"Chuck Norris is the only person on the planet that can kick you in the back of the face.",
"Chuck Norris was once on Celebrity Wheel of Fortun… around awkwardly, waiting for the wheel to stop.",
"Leading hand sanitizers claim they can kill 99.9 p…n kill 100 percent of whatever the hell he wants.",
"Chuck refers to himself in the fourth person.",
"Chuck Norris' tears cure cancer. Too bad he has never cried.",
"Chuck Norris doesn't have good aim. His bullets just know better than to miss.",
"Chuck Norris wins games of charades without moving.",
"Chuck Norris makes fire by rubbing 2 ice cubes together.",
"Chuck Norris's daughter lost her virginity, he got it back.",
"Chuck Norris tells Simon what to do.",
"Chuck Norris knows Victoria's secret.",
"Chuck Norris's Blood Type is AK-47.",
"Brett Favre can throw a football over 50 yards. Chuck Norris can throw Brett Favre even further.",
"Chuck Norris' dog is trained to pick up his own po…ause Chuck Norris will not take shit from anyone.",
"When Chuck Norris gives you the finger, he's telling you how many seconds you have left to live.",
"Chuck Norris can speak braille.",
"Chuck Norris can do a wheelie on a unicycle.",
"Cars look both ways before Chuck Norris crosses the street.",
"Chuck Norris will roundhouse kick you in the back of your head while clipping his toe nails.",
"Chuck Norris once bowled a perfect game with a marble.",
"Chuck Norris beat Halo 1, 2, and 3 on Legendary with a broken Guitar Hero controller.",
"Chuck Norris sleeps with a pillow under his gun.",
"Chuck Norris doesn't play \"hide-and-seek.\" He plays \"hide-and-pray-I-don't-find-you.\"",
"Death once had a near-Chuck-Norris experience.",
"If it looks like chicken, tastes like chicken, and…n but Chuck Norris says its beef, then it's beef.",
"Chuck Norris puts the \"laughter\" in \"manslaughter\".",
"Chuck actually died four years ago, but the Grim Reaper can't get up the courage to tell him.",
"Chuck Norris is the only man to ever defeat a brick wall in a game of tennis.",
"Chuck Norris can unscramble an egg.",
"Chuck Norris doesnt wear a watch. He decides what time it is.",
"The real reason Hitler killed himself is because he found out that Chuck Norris is Jewish.",
"Chuck Norris can hit you so hard your blood will bleed.",
"Chuck Norris CAN find the end of a circle.",
"Some kids piss their name in the snow. Chuck Norris can piss his name into concrete.",
"Obama said, \"Yes we can.\" Chuck Norris says, \"I already did.\".",
"Chuck Norris once robbed a gun store with a knife.",
"Chuck Norris doesn't lie. He simply changes the truth.",
"Chuck Norris recorded the making of the first video camera.",
"Chuck Norris can make a happy meal cry.",
"Tsunamis are created by Chuck Norris practicing round house kicks on the beach.",
"Chuck Norris was dropped twice as a baby. Once on Hiroshima and the other on Nagasaki.",
"The original title for Alien vs. Predator was Alie…ine dollars to see a movie fourteen seconds long.",
"The saddest moment for a child is not when he lear… isn't real, it's when he learns Chuck Norris is.",
"Chuck Norris can drown a fish.",
"The reason newborn babies cry is because they know they have just entered a world with Chuck Norris.",
"On a high school math test, Chuck Norris put down …uck Norris solves all his problems with Violence.",
"Police label anyone attacking Chuck Norris as a Code 45-11.... a suicide.",
"The Great Wall of China was originally created to keep Chuck Norris out. It failed miserably.",
"Chuck Norris can kill you 101 different ways with a roll of extra-soft toilet paper.",
"Every one of Chuck Norris blood cells have black belts.",
"Global warming will end as soon as Chuck Norris puts his shirt back on.",
"You can die from looking at Slenderman too much. S…n can die from thinking of Chuck Norris too much.",
"Chuck Norris uses hot sauce as eye drops.",
"Chuck Norris played tug of war with himself and won.",
"Chuck Norris brought a knife to a gunfight. He is still waiting for an opponent.",
"Chuck Norris never fails, he tells success to come back when its ready for him.",
"Chuck Norris beat a black hole in tug of war.",
"In space Chuck Norris can hear you scream.",
"Chuck Norris can follow you into a revolving door and come out ahead of you.",
"Elvis Presley, Richard Petty, Budweiser, and Michael Jackson all call Chuck Norris \"The King\".",
"The world did not have a tilt in its axis until Chuck Norris stubbed his toe on the North Pole."
];
//
// FUNCTIONS
//
// Get random value between a range
function rand(high, low = 0) {
return Math.floor(Math.random() * (high - low + 1) + low);
};
// Call the main function again
const goAgain = (fn, sec) => {
// const chat = global.document.querySelector('div.chat:not(.unread)')
// selectChat(chat)
setTimeout(fn, sec * 1000);
};
// Dispath an event (of click, por instance)
const eventFire = (el, etype) => {
let evObj = global.document.createEvent('Events');
evObj.initEvent(etype, true, false);
el.dispatchEvent(evObj);
};
// Select a chat to show the main box
const selectChat = (chat, cb) => {
const title = chat.querySelector('.emojitext').title;
eventFire(chat, 'mousedown');
if (!cb) return;
const loopFewTimes = () => {
setTimeout(() => {
const titleMain = global.document.querySelector('h2.chat-title').innerText;
if (titleMain != title) {
console.log('not yet');
return loopFewTimes();
}
return cb();
}, 1)
}
loopFewTimes();
};
// Send a message
const sendMessage = (chat, message, cb) => {
if(chat) {
//avoid duplicate sending
const title = chat.querySelector('.emojitext').title;
ignoreLastMsg[title] = message;
}
//add text into input field
global.document.querySelector('.input').innerHTML = message.replace(/ /gm,'');
//Force refresh
event = global.document.createEvent("UIEvents");
event.initUIEvent("input", true, true, window, 1);
global.document.querySelector('.input').dispatchEvent(event);
//Click at Send Button
eventFire(global.document.querySelector('.compose-btn-send'), 'click');
cb();
};
//
// MAIN LOGIC
//
const run = () => {
stop();
if(waitForAnswer) {
// get next unread chat
const chats = global.document.querySelectorAll('.unread.chat');
const chat = chats[cnt];
if (chats.length == 0 || !chat) {
console.log(new Date(), 'nothing to do now... (1)', chats.length, chat);
return start(start);
}
// get infos
const title = chat.querySelector('.emojitext').title + ''
const lastMsg = (chat.querySelector('.last-msg') || { innerText: '' }).innerText; //.last-msg returns null when some user is typing a message to me
// avoid sending duplicate messaegs
if ((ignoreLastMsg[title]) == lastMsg) {
console.log(new Date(), 'nothing to do now... (2)', title, lastMsg);
return start();
}
// what to answer back?
const sendText = jokeList[rand(jokeList.length - 1)];
// that's sad, there's not to send back...
if (!sendText) {
ignoreLastMsg[title] = lastMsg
console.log(new Date(), 'new message ignored -> ', title, lastMsg);
return start();
}
console.log(new Date(), 'new message to process, uhull -> ', title, lastMsg);
// select chat and send message
sendMessage(chat, sendText.trim(), () => {
start();
});
} else {
// what to answer back?
const sendText = jokeList[rand(jokeList.length - 1)];
// select chat and send message
sendMessage(null, sendText.trim(), () => {
start();
});
}
};
let loopTimeout;
const start = () => {
loopTimeout = global.setTimeout(run, waitTime);
}
const stop = () => {
global.clearTimeout(loopTimeout);
}
const wj = {
every: (seconds) => {
waitTime = seconds * 1000;
waitForAnswer = false;
return wj;
},
afterMsg: () => {
waitTime = 10 * 1000;
waitForAnswer = true;
return wj;
},
start: () => {
console.log('Lets the jokes begins...');
start();
return wj;
},
stop: () => {
console.log('Chop chop .. enough for now');
stop();
return wj;
}
};
global.WJ = wj;
})(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment