Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

365
Visualizações
Issues creating a global variable list in javascript (Tampermonkey)

I'd like to create a global variable list in javascript that will add new elements to the list. I need it global because I'm using Tampermonkey, and I want to keep old elements inside the list and able to add new elements to it.

How can I do this correctly?

I have tried something like this but it's not working :c

var check_empty = GM_getValue("arr_names");
if(Array.isArray(check_empty) && check_empty.length > 0){ // list of usernames exists and is not empty
    var user = "RandomUsername";
    GM_setValue("arr_names", check_empty.push(user));
    var arr = GM_getValue("arr_names");
    for(var i=0; i<arr.length; i++){
        window.alert("Length: " + arr.length + " Elements inside: " + arr[i]);
    }
}
else{ // create the list
    GM_setValue("arr_names",["Initialized"]);
}

So let's say I run this code 3 times. Then the list needs to look like this:

["Initialized", "RandomUsername", "RandomUsername"]

If 4 times excuted, then like this:

["Initialized", "RandomUsername", "RandomUsername", "RandomUsername"]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

GM_setValue("arr_names", check_empty.push(user));

As mentioned in the comments, check_empty.push(user) returns the length of the updated array, but you should pass the array here.

You can push the element in the previous line and use the mutated array:

check_empty.push(user);
GM_setValue('arr_names', check_empty);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda