Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I'm using a Chrome extension to autoclick the (print) "Confirm" button on
a web page, but this script loops the page, which refreshes indefinitely:

JavaScript
function autoClick() {
    labels = document.querySelectorAll('label');
    if (labels[1].innerText == "Modello di stampa") {
        document.getElementsByClassName("button confirm")[0].click();
    }
}

var first = true;
if(first) {
    first=false;
    autoClick();
}

Could you help me? Thanks very much!

What I have tried:

I also tried adding a condition:
JavaScript
const elMessage = document.querySelector('.messages.confirms');
if (elMessage && elMessage.textContent.trim() === 'La stampa è stata inoltrata') {
    document.querySelector('.button.confirm')?.click();
}
Posted
Updated 19-Dec-22 22:16pm
v5

If you click the button, the default behavior is submitting the page (if the button is within the form, etc.). We don't know the exact details of the button with class button confirm, how it is setup, what parent it has, etc. If you can share the HTML, we can further help you (note that we will not open the website, download an HTML file and then run it... Nope!).

One of the ways in which you can prevent the page from reloading is to handle the button click event and put event.preventDefault() inside the button click handler.

Secondly, if you want to submit the form, you can manually create the FormData object and submit the data to the server without the browser handling the form submission itself.

Read more here:
FormData - Web APIs | MDN[^]
Event.preventDefault() - Web APIs | MDN[^]
 
Share this answer
 
Comments
Parvares 11-Dec-22 13:23pm    
Thanks for the answer, Afzaal. The site is password protected, I can't manipulate the HTML page, I only want a script that autoclicks the button (I'm using a Chrome Autofill extension). The script of the "Conferma" button is this:



Conferma




Thanks again!
Move the variable declaration for first outside of the autoclick function.
This variable is always reset to true whenever your method is called, that is why it is called indefinitely.
 
Share this answer
 
Comments
Richard Deeming 20-Dec-22 4:15am    
The formatting of the code in the question is appalling. But if you count the curly braces, you'll see that the first variable is already outside of the autoClick function.
GKP1992 20-Dec-22 5:53am    
Silly I missed that, thank you!
Parvares 20-Dec-22 4:23am    
Tried this but loops the page:

labels = document.querySelectorAll('label');
if (labels[1].innerText == "Modello di stampa")
{document.getElementsByClassName("button confirm")[0].click();
}
GKP1992 20-Dec-22 5:56am    
You need to see what the getElementsByClassName returns, may be log it in the console.
Also check what is happening in the click event handler, if it is submitting the page, your code may be re-executing on the page load. May be due to document.ready.
Parvares 20-Dec-22 6:52am    
document.getElementsByClassName("button confirm")[0].click();

returns in console:
'undefined'

and correctly generates a banner on top of the page:
'La stampa è stata inoltrata' (that is 'print submitted')

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900