-1

I have this code, where it does specific things automatically when it finds a specific button "Subscribe". But the problem is, sometimes the "Subscribe" button doesn't appear on the page, and when that happens I wanted it to keep refreshing the page until the button appears. I've tried to do that but sadly it doesn't work, and I don't have much experience in JavaScript. I hope you can help me. Thanks!

$(document).ready(function() {
    if (document.domain === 'www.youtube.com') {
        if (window.opener && window.opener !== window) {
            setTimeout(function() {
                let obj122 = $("#subscribe-button").find('.ytd-subscribe-button-renderer')[0];

                if (typeof obj122 !== 'undefined') {
                    obj122.click();
                }

                setTimeout(function() {
                    window.close();

                }, getRandomSec222(5));
            }, getRandomSec222(3));
        }
    } else {
        execBtnTrigger();

        function execBtnTrigger() {
            setTimeout(function() {
                if (typeof $(".single_like_button.btn3-wrap")[0] !== 'undefined') {
                    $(".single_like_button.btn3-wrap")[0].click();
                } else {
                    location.reload();
                }
                execBtnTrigger();
            }, getRandomSec222(9));
        }
    }
});

function getRandomSec222(sum) {
    let sec222 = (Math.random() * 4) + sum;
    sec222 = parseFloat(sec222.toFixed(3)) * 1000;
    return sec222;
}
Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
user16672767
  • 53
  • 1
  • 4
  • You can just use location.reload() to refresh. https://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript So, if you find the button do whatever, else reload. – nrayburn-tech Aug 15 '21 at 15:50
  • Seems like this post title is backwards. Shouldn't it be refresh *until* something exists? – charlietfl Aug 15 '21 at 15:51

1 Answers1

0

You can use location.reload() as per your requirements.