Last active
December 23, 2016 15:37
-
-
Save Alexnder/f541bb562bf7ec6957c757c9bea585cf to your computer and use it in GitHub Desktop.
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
(function() { | |
"use strict"; | |
const ACTION_INTERVAL = 250; | |
let christmasEmojiTypes = ['snowflake', 'christmas_tree', 'snowman', 'snow_cloud']; | |
function getEmogies(container, type, reacted = '') { | |
if (reacted) { | |
reacted = ':not(.user_reacted)'; | |
} | |
return $(container).find(`[data-emoji="${type}"]${reacted}`); | |
} | |
function modifyEmoji(container, type, isAddAction) { | |
let selectorModifier = '.user_reacted'; | |
if (isAddAction) { | |
selectorModifier = ':not(.user_reacted)'; | |
} | |
$(container).find(`[data-emoji="${type}"]${selectorModifier}`).click(); | |
return () => new Promise((resolve) => setTimeout(resolve, ACTION_INTERVAL)); | |
} | |
function addEmoji(container, type) { | |
return () => new Promise((resolve) => { | |
let $addReaction = $(container).find('.ts_icon_add_reaction'); | |
if (!$addReaction.length) { | |
return resolve(); | |
} | |
$addReaction.click(); | |
let tryCount = 0; | |
let interval = setInterval(() => { | |
tryCount++ | |
if (tryCount == 2) { | |
$(container).find('.ts_icon_add_reaction'); | |
} | |
if (tryCount > 4) { | |
clearInterval(interval); | |
return resolve(); | |
} | |
if ($('#emoji_menu').length && $(`[data-name=":${type}:"]`).length) { | |
clearInterval(interval); | |
$(`[data-name=":${type}:"]`).click(); | |
setTimeout(resolve, ACTION_INTERVAL); | |
} | |
}, 50); | |
}) | |
} | |
function getFunPromiseList() { | |
let funPromiseList = []; | |
christmasEmojiTypes.forEach((type) => { | |
$('ts-message').slice(-15).each((i, message) => { | |
let action; | |
let emogies = getEmogies(message, type); | |
if (!emogies.length) { | |
if (Math.random() < 0.7) { | |
action = addEmoji(message, type); | |
} | |
} else { | |
action = modifyEmoji(message, type, Math.random() < 0.2); | |
} | |
if (action) { | |
funPromiseList.push(action); | |
} | |
}); | |
}); | |
return funPromiseList; | |
} | |
function runChristmasFun() { | |
let chain = getFunPromiseList().reduce((chain, promise) => { | |
return chain = chain.then(promise); | |
}, Promise.resolve()); | |
chain.then(() => { setTimeout(runChristmasFun, ACTION_INTERVAL * 2); }); | |
} | |
if (!window.jQuery) { | |
let script = document.createElement("SCRIPT"); | |
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'; | |
script.type = 'text/javascript'; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
} | |
let checkReady = function(callback) { | |
if (window.jQuery) { | |
callback(jQuery); | |
} | |
else { | |
setTimeout(function() { checkReady(callback); }, ACTION_INTERVAL); | |
} | |
}; | |
checkReady(function($) { | |
runChristmasFun(); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment