chandanmal00 / InstagramKeyboardShortcuts

// ==UserScript==
// @name        InstagramKeyboardShortcuts
// @description Helps you like/follow/browse in Instagram ultra-fast via keyboard; use keys A/D/Left/Right for next/previous picture, W/Up for Like/Unlike
// @icon        https://www.instagram.com/favicon.ico
// @include     https://www.instagram.com/*
// @version     1.0
// @grant       none
// @author chandanmal00
// @license      MIT
// @require     https://code.jquery.com/jquery-3.2.1.min.js
// ==/UserScript==

////////////// Credits ////////////
// Based on "Simple Instagram Like Bot" by JoelDare and dunkel1024
// https://www.joeldare.com/wiki/simple_instagram_like_bot
//////////////////////////////////////

// INSTRUCTIONS:
// Make sure you are on the Instagram Paginated Like-Page
// It looks like this: https://imgur.com/a/KB174
// Now Press:
// "w" or ArrowUp for Like/Unlike
// "a" or LeftArrow for previous picture
// "d" or RightArrow for next picture
////////////////////////////////////////////
/*
1. updated script from the original @dunkel1024 version as the previous tags do not work
2. added ability to like only when Post is not liked
3. added ability to only like when <30 likes
*/
////////////////////////////////////////////
var POSTS_WITH_MAX_LIKE_CNT = 30;

function getHeartElement() {
  var knownHeartElementNames = [
    'fr66n > button:nth-child(1)'
  ];
  var i = 0;
  // Loop through the known heart elements until one works
  for (i = 0; i < knownHeartElementNames.length; i++) {
    var heartElement = document.querySelector('.' + knownHeartElementNames[i]);
    if (heartElement != undefined) {
      break;
    }
  }
  return heartElement;
}

/**UTILITY FUNCTIONS**/
//get array object if we can find elements with a text
function getElementsByText(str, tag = 'a') {
  return Array.prototype.slice.call(document.getElementsByTagName(tag)).filter(el => el.textContent.trim().includes(str.trim()));
}

/**END of UTILITY FUNCTIONS**/

//get Like Count
function getLikeCount() {

  var knownLikeCntNames = [
    ["like", "button"],
    ["Liked by", "div"],
    ["views", "span"]
  ];
  var i = 0;
  for (i = 0; i < knownLikeCntNames.length; i++) {
    var data = getElementsByText(knownLikeCntNames[i][0], knownLikeCntNames[i][1]);
    var dataLen = data.length;
    if (dataLen >= 1) {
      //extract number of likes from the text
      var likeText = data[dataLen - 1].textContent;
      var likeCnt = parseInt(likeText.match(/[0-9]+/)[0], 10);
      return likeCnt;
    }
  }

  return -1
}

//only like if its not liked and cnt likes are less than POSTS_WITH_MAX_LIKE_CNT, plus get the next post
function doLike() {
  var nextElement = document.querySelector('.coreSpriteRightPaginationArrow');
  var previousElement = document.querySelector('.coreSpriteLeftPaginationArrow');
  var likeElement = getHeartElement();
  //likeElement.click();

  if (likeElement) {
    var label = likeElement.getElementsByTagName("svg")[0].getAttribute("aria-label");
    if (label == "Like") {
      var likeCnt = getLikeCount();
      if (likeCnt <= POSTS_WITH_MAX_LIKE_CNT) {
        likeElement.click();
      }
    }
    //If you do not want auto-next after like, comment this
    nextElement.click();
  }
}

$(document).on('keydown', function (e) {

  // Find pagination element for skipping to next pic
  var nextElement = document.querySelector('.coreSpriteRightPaginationArrow');
  // Find pagination element for skipping to previous pic
  var previousElement = document.querySelector('.coreSpriteLeftPaginationArrow');

  var buttonElement = $('span > button:contains("Follow")');
  console.log(buttonElement);

 
  // Adding key "w" and ArrowUp for like/unlike
  if ((e.which == 87) || (e.which == 38)) {
    doLike();
  }
  // Adding key "d" for next pic
  if (e.which == 68) {
    nextElement.click();
  }
  // Adding key "a" for prev pic
  if (e.which == 65) {
    previousElement.click();
  }

});