blog/
Script for choosing a winner on Instagram (JavaScript Instagram comment picker)
Completely reworked the script for choosing winners in contests for instagram (logic and set of functions), which I wrote earlier. Here is the code:
//MOSTLY WRITTEN BY I. Tolstikov of DMTRVK.RU //THANKS TO GeorgeCrisan from https://forum.freecodecamp.org/t/how-to-make-math-random-not-repeat-same-numbers/417973/3 for the generateUniqueRandom function // here you can set it up: let myAccoutName = "dmrtvk.ru"; //your account name to exclude from selection let howManyToSelect = 5; //number of winners let numberOfCommentPagesToOpen = 100; // number times to click on the button let timeToWaitBetweenClicks = 500; // in milliseconds, set this according to your connection speed, so the browser would have time to load the button to expand the comments list. // end of setup, lets run it. let haveIt = []; (function myLoop(i) { setTimeout(function() { try { document.querySelector('.dCJp8.afkep').click(); console.log('>') } catch(e) { console.log('Can find any button to click, moving on.') mainProcess(howManyToSelect); //how many to select return; } console.log('>'); // your code here if (--i) myLoop(i); // decrement i and call myLoop again if i > 0 }, timeToWaitBetweenClicks) })(numberOfCommentPagesToOpen); // pass the number of iterations as an argument function generateUniqueRandom(maxNr) { //Generate random number let random = (Math.random() * maxNr).toFixed(); //Coerce to number by boxing random = Number(random); if(!haveIt.includes(random)) { haveIt.push(random); return random; } else { if(haveIt.length < maxNr) { //Recursively generate number return generateUniqueRandom(maxNr); } else { console.log('No more numbers available.') return false; } } } function mainProcess(howManyToSelect){ let userNameList = document.querySelectorAll(".sqdOP.yWX7d._8A5w5.ZIAjV "); // here goes the selector we search for console.log(userNameList.length); nameSet = new Set(); for (let i=0; i<userNameList.length; i++) { nameSet.add(userNameList[i].firstChild.nodeValue); console.log(userNameList[i].firstChild.nodeValue); } console.log(nameSet.size); let keys = Array.from(nameSet.keys()); console.log("!!!!!!!!!!!!!!!!!!!!!!!!"); console.log("!!!!!!!!!!!!!!!!!!!!!!!!"); console.log("!!!!!!!!!!!!!!!!!!!!!!!!"); console.log("!!!!!!!!!!!!!!!!!!!!!!!!"); console.log("Selected randomly:"); for (i=0; i<howManyToSelect; i++){ //CHANGE THIS TO THE NUMBER OF WINNERS //console.log(keys[Math.floor(Math.random() * keys.length)]); randomNumber = generateUniqueRandom(keys.length); if (keys[randomNumber] == myAccoutName | keys[randomNumber] == undefined){ //CHANGE THIS TO EXCLUDE YOUR ACCOUNT i--; } else { console.log("№",i+1," —", "@", keys[randomNumber]); } } }
Just paste into the browser console and hit enter. Pay attention to the comments in the code: there are several variables at the beginning of the program.
myAccoutName = “dmrtvk.ru”; // your account name to be excluded from selection
howManyToSelect = 5; // how many winners do you need to choose
numberOfCommentPagesToOpen = 100; // how many times to press the + button to load comments (depending on how many comments you have under the publication)
timeToWaitBetweenClicks = 500; // in milliseconds, set in accordance with the speed of your connection so that the browser has time to load the + button, which loads the list of comments.
Similar Posts:
- Script for choosing a winner on Instagram (JavaScript Instagram comment picker)
- Как разыграть бесплатные места на курсе вёрстки для архитекторов (JS+Instagram)
- Урок — Буквы от больших к маленьким в InDesign
- Calculate inclination of a roof valley with Python
- Задаём свойства объектам в ArchiCAD 23 через Python