Auto insert a string text [closed]

-3

I have several words and would like them to be inserted in sequence with a 5 minute interval on a form. I've researched a lot but so far nothing. I hope someone helps me to create this automatic javascript. The code can not be changed:

<form id="procurar-form" method="get" action="Site">
  <input type="text" class="procurar-field" placeholder="Vamos Procurar?" name="q" value="verde"> 
  <button class="procurar-btn" id="button_procurar">Procurar</button>
</form>
    
asked by anonymous 12.09.2017 / 22:55

2 answers

1

First, the objects that will be responsible for manipulating the DOM and word list:

const form = document.getElementById("procurar-form");
const input = form.getElementsByTagName("input")[0];
const button = document.getElementById("button_procurar");

const palavras = ["Papel", "Vidro", "Lata", "Terra", "etc"];

Second, we need to write a function that generates a random value, update the word within the field and run the click of the button (the latter was asked in comments). For this, we do:

function novaPalavra() {
  // Gera um valor aleatório de 0 ao comprimento da lista de palavras:
  const index = Math.floor(Math.random() * palavras.length);

  // Atualiza o valor do campo para a palavra sorteada:
  input.value = palavras[index];

  // Executa o clique do botão:
  button.click();
}

This function does what you want, but you have also been asked to run this code every 5 minutes. To do this, we can use the tool setInterval , which receives the name of a function and the range that it will be executed in milliseconds. For a 5-minute interval, we need a range of 5 * 60 * 1000 milliseconds. So:

setInterval(novaPalavra, 5*60*1000);

In this way, every five minutes the word in the field will be changed to another at random and clicking the button will run.

See working:

const form = document.getElementById("procurar-form");
const input = form.getElementsByTagName("input")[0];
const button = document.getElementById("button_procurar");

const palavras = ["Papel", "Vidro", "Lata", "Terra", "etc"];

function novaPalavra() {
  const index = Math.floor(Math.random() * palavras.length);
  input.value = palavras[index];
  button.click();
}

setInterval(novaPalavra, 1000);
<form id="procurar-form" method="get" action="Site">
  <input type="text" class="procurar-field" placeholder="Vamos Procurar?" name="q" value="verde"> 
  <button class="procurar-btn" id="button_procurar">Procurar</button>
</form>
  

Since the click of the button will submit the form by refreshing the page - or redirecting the user - makes no sense to have a code that run every 5 minutes, as always in his first run it will stop due to submission the form. The answer was given in this way by the author's insistence on the comments.

    
12.09.2017 / 23:53
0

You can try this:

var input = document.getElementById("procurar-field");

var palavras = ["Lata", "Vidro", "Papel", "Terra"];

function inserePalavras(index) {
   if (index < palavras.length) {
    input.value = palavras[index];
    
    var botao = document.getElementById("button_procurar");
    botao.click();
    
    setTimeout(function() { inserePalavras(index+1); }, 1000); // Para 5 minutos basta mudar o 1000 para 300000
   }
}

setTimeout(function() { inserePalavras(0); }, 1000); // Para 5 minutos basta mudar o 1000 para 300000
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="procurar-form"  method="get" action="javascript:void(0)">
  <input type="text" class="procurar-field" id="procurar-field" placeholder="Vamos Procurar?" name="q" value="verde"> 
  <button class="procurar-btn" id="button_procurar">Procurar</button>
</form>

After all words are entered the code for running setTimeout is not going to be an infinite loop.

Once you change the word in input and run a click automático on the Browse button

Note: I changed the action="Site" of the form to action="javascript:void(0)" to not reload and can show here in stackoverflow when to use and only return the action action="Site"

    
13.09.2017 / 00:16