Skip to content

Instantly share code, notes, and snippets.

@MarinaGherman
Created December 5, 2020 11:26
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 MarinaGherman/9e5025d72d749a7a3709dcc3f7ea897e to your computer and use it in GitHub Desktop.
Save MarinaGherman/9e5025d72d749a7a3709dcc3f7ea897e to your computer and use it in GitHub Desktop.
Instagram like bot to execute in browser console using js
/*
* To run this bot need to open web instagram, make login and search you interesting hashtag. For example: "#dog", "#travel" ... etc.
* Open first card. After open card you need to put the code into console. To open console look the documentation: https://developers.google.com/web/tools/chrome-devtools/open
* Don't forgot to replace text for const likeButtonText and nextButtonText in action function before paste it in console.
*/
function getNextButton(text) {
let aTags = document.getElementsByTagName("a");
let searchText = text;
let nextButton;
for (let i = 0; i < aTags.length; i++) {
if (aTags[i].textContent == searchText) {
nextButton = aTags[i];
break;
}
}
return nextButton;
}
function getLikeButton(ariaLabel) {
const svgButtons = document.querySelectorAll(`[aria-label="${ariaLabel}"]`);
let likeButton;
for (let i = 0; i < svgButtons.length; i++) {
// The like button have width 24. All other buttons have different width.
if (svgButtons[i].clientWidth == 24) {
likeButton = svgButtons[i].parentNode.parentNode.parentNode;
break;
}
}
return likeButton;
}
function sleep(seconds) {
return new Promise(function (resolve) {
setTimeout(function() {
resolve();
}, seconds * 1000);
});
}
async function actions () {
const likeButtonText = '__LIKE_SVG_ARIA_LABEL_TEXT__'; // To retreive text inspect like button and get attribute "aria-label". What is attribute? see documentation https://www.w3schools.com/html/html_attributes.asp
const nextButtonText = "__NEXT_BUTTON_TEXT__"; // To retreive text inspect next button and get text of a tag. Example: `<a ... >MY TEXT</a>
console.warn(`Waiting 5 seconds to render card`);
await sleep(5);
console.warn('Get like button');
const likeButton = getLikeButton(likeButtonText);
console.warn('Like button: ', likeButton);
console.warn('Get next button');
const nextButton = getNextButton(nextButtonText);
console.warn('Next button: ', nextButton);
let wait = 2;
if (likeButton) {
console.warn('Like action');
likeButton.click();
// If it's possible to like wait more time to prevent spamming
wait = 10;
}
console.warn(`Waiting ${wait} seconds`);
await sleep(wait);
console.warn('Next slide');
nextButton.click();
}
async function runInLoop() {
while (true) {
await actions();
console.warn('All actions completed');
}
}
runInLoop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment