L
L
Lems302021-08-09 16:46:05
JavaScript
Lems30, 2021-08-09 16:46:05

How to write a script using recursion?

The bottom line is:

I need to write a javascript script that will interact with DOM elements (click on a button, fill in fields, etc.).

I say right away - I know about Selenium and similar frameworks. It is necessary to use a pure javascript script.

The problem is that when I run the script in the browser console, it will be executed exactly until there is a transition to another page / page refresh. After that, the browser console is reset and the rest of the script is not executed.

I heard that you can make the script recursive and then switching to another page will no longer be a problem and the entire script will be executed, even after switching to a new page and will continue to be executed on a new page.

Can you please tell me if this is possible and how to do it?

Preferably with examples or approximate script organization schemes.

An example for clarity (it is of course wrong):

function test() {
    function test1(){
document.querySelector('[id="username"]').value='test' 
document.querySelector('[id="password"]').value='test'
document.querySelector('[type="submit"]').click() /* заполняем форму на странице и отправляем её, переход на новую страницу*/
test()
}
document.querySelectorAll('.btn')[0].click() /* уже на новой странице кликаем по кнопке */
test1()
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexandroppolus, 2021-08-09
@Lems30

make a userscript.
for Chrome, use the Tampermonkey plugin, for other browsers - the same.

D
Dmitry Kuznetsov, 2021-08-09
@dima9595

Recursion is generally irrelevant and out of place. Since this is a call to itself in an endless loop. You also need the script to run after the page restart/change. Accordingly, there should be a call to the method / function needed when loading the script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question