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;
}