Created
June 11, 2021 12:44
-
-
Save AidHamza/7383d956be6a997211b252a07dae3ca1 to your computer and use it in GitHub Desktop.
Modified in order to support multiple comment, originally developed by https://github.com/jportella93
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
try { | |
// Set the number of desired winners | |
const NumOfWinners = 10; | |
// Set value to true if you want to allow duplicated users | |
const rules = [{ | |
"key": "allowDuplicatedUsers", | |
"value": false, | |
"message": "Multiple comments per user are not allowed" | |
}]; | |
async function pickComment() { | |
console.clear(); | |
console.log('%c' + 'Starting comment picker', 'color: red;font-family: helvetica, sans-serif;font-size:20px;'); | |
const sleepTime = 2000; | |
function sleep(ms) { | |
if (ms < 0) ms = 0; | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
function next() { | |
return new Promise(async (resolve) => { | |
let btn = document.querySelector("#react-root > section > main > div > div > article > div > div > ul > li > div > button > span"); | |
while (btn) { | |
btn.click(); | |
await sleep(1000); | |
btn = document.querySelector("#react-root > section > main > div > div > article > div > div > ul > li > div > button > span"); | |
} | |
resolve(); | |
}); | |
} | |
console.log('⏳ Loading comments...'); | |
await next(); | |
const commentNodes = Array.from(document.querySelectorAll("#react-root > section > main > div > div > article > div.eo2As > div > ul > ul > div > li > div > div > div:nth-child(2)")); | |
const comments = commentNodes.map((comment) => ({ | |
name: comment.children[0].outerText, | |
comment: comment.children[1].outerText | |
})); | |
let filterByRulesFlag; | |
function filterCommentsByRules(arr, rulesToFilter) { | |
rulesToFilter = rulesToFilter.filter(rule => rule.key === 'keyword'); | |
if (rulesToFilter.length === 0) { | |
filterByRulesFlag = false; | |
return arr | |
} | |
filterByRulesFlag = true; | |
return arr.filter((el) => { | |
return rulesToFilter.every((rule) => { | |
const re = new RegExp(rule.word, 'gi'); | |
const match = el.comment.match(re); | |
if (!match) return false; | |
if (!rule.times) return true; | |
if (rule.times && match.length == rule.times) { | |
return true; | |
} | |
return false | |
}); | |
}); | |
} | |
await sleep(sleepTime); | |
console.log('✅ All comments loaded!'); | |
const filteredByRules = filterCommentsByRules(comments, rules); | |
await sleep(sleepTime); | |
console.log('Comment rules are:'); | |
for (let rule of rules) { | |
await sleep(sleepTime - sleepTime * 0.5); | |
console.log(rule.message); | |
} | |
if (filterByRulesFlag) { | |
await sleep(sleepTime); | |
console.log('Comments after filtering by rules: ' + filteredByRules.length); | |
} | |
if (filteredByRules.length === 0) { | |
console.log('There are no comments that match all the rules'); | |
return; | |
} | |
let filterDuplicate; | |
function filterDuplicateUsers(arr) { | |
const seen = {}; | |
const duplicateUsersRule = rules.find(rule => rule.key === 'allowDuplicatedUsers'); | |
filterDuplicate = duplicateUsersRule.value; | |
return filterDuplicate ? arr : arr.map((el) => { | |
const { | |
name | |
} = el; | |
if (seen[name]) return; | |
seen[name] = true; | |
return el; | |
}).filter((el) => el); | |
} | |
const filteredByDuplicate = filterDuplicateUsers(filteredByRules); | |
await sleep(sleepTime); | |
console.log('And the winners are...'); | |
await sleep(300); | |
console.log('🥁'); | |
await sleep(300); | |
console.log(' 🥁'); | |
await sleep(300); | |
console.log(' 🥁'); | |
await sleep(sleepTime + sleepTime * 0.5); | |
for (var i = 1; i <= NumOfWinners; i++) { | |
let rand = Math.floor(Math.random() * filteredByDuplicate.length + 1); | |
let winnerName = filteredByDuplicate[rand].name; | |
let winnerComment = filteredByDuplicate[rand].comment; | |
let winnerMessage = '@' + winnerName; | |
console.log('%c' + winnerMessage, 'color: red;font-family: impact, sans-serif;font-size:30px;'); | |
let winnerMessageComment = '"' + winnerComment + '"'; | |
console.log('%c' + winnerMessageComment, 'color: red;font-family: helvetica, sans-serif;font-size:20px;'); | |
let winnerNode = Array.from(document.querySelectorAll('li')).find(el => el.outerText.includes(winnerName) && el.outerText.includes(winnerComment)); | |
winnerNode.style.backgroundColor = 'orange'; | |
winnerNode.scrollIntoViewIfNeeded(); | |
} | |
} | |
pickComment(rules); | |
} catch (e) { | |
console.log('Something went wrong, please contact the administrator.') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment