JS code injection into console freezes the website

Viewed 57

I have created very simple JS script to automate tasks on specific website (Clicking element when it appears etc..) It has 3 functions in which are 3 while true loops, basically each of those has a infinite loop which is asking if specific element is present on website, and if yes then I click it and call another function which is composed the same way, looking for a element and if it appears, it should click it. So I do have 3 functions Main(), Function2(), Function3() and I start by calling Main(), If element appears in Main() I call function2() then function3 from f2() and from f3() back to Main(), should be an infinite looping, but after I copy paste the code into console the website instantly becomes lagged and I need to restart browser, why does this happen? How do I fix it to be able to run the script correctly? (Iam very new to JS, this might be coded very wrongly)

function function3(){
    while (true) {
        let BTN = document.querySelector("....")
        if (BTN != null){
            BTN.click()
            main()
        }
    }
}

function function2(){

    while (true){
        let anotherElementButton = document.querySelector("...")
        if (anotherElementButton != null){
            anotherElementButton.click()
            function3()
        }
    }
}

function main(){

    while (true){
        let elements = document.getElementsByTagName('tag-of-the-elements')
        if (elements.length > 0){
            Array.from(elements).forEach(btn => btn.click())
            function2()
        }
    }
}
main()
1 Answers
Related