"view replies" auto expander

click on "view replies (500)" and it will click on the same button until all replies open.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         "view replies" auto expander
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  click on "view replies (500)" and it will click on the same button until all replies open.
// @author       goodwin64
// @match        *://instagram.com/*
// @match        *://www.instagram.com/*
// @namespace    instagram.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const markedElements = new Set();

    function listenToExpandClicks() {
      [...document.getElementsByTagName('span')]
        .filter(el => el.innerText.toLowerCase().includes('view'))
        .forEach(el => {
          if (!markedElements.has(el)) {
            el.addEventListener('click', handleViewRepliesClick);
            markedElements.add(el);
          }
        });
    }
    
    function howMuchRepliesLeft(str) {
      const repliesCountMatch = str.match(/.*\((\d*)\)/)
      return repliesCountMatch ? Number(repliesCountMatch[1]) : 0;
    }
    
    function handleViewRepliesClick() {
      clickIfExpandable(this);
    }
    
    function clickIfExpandable(el) {
      const repliesLeft = howMuchRepliesLeft(el.innerText);
      if (repliesLeft > 0) {
        el.click();
        setTimeout(() => {
          clickIfExpandable(el);
        }, 1000);
      } else {
        el.removeEventListener('click', handleViewRepliesClick);
        markedElements.delete(el);
      }
    }
    
    setInterval(listenToExpandClicks, 3000);
})();