My userscript does not work

Asked

Viewed 85 times

-4

I’m making a greasemonkey script but my code doesn’t work, I don’t know if it’s my browser, but if you find any error please fix it.

// ==UserScript==
// @name        ---
// @namespace   ---
// @description ---
// @author      ---
// @match       http://agar.io/*
// @version     1
// @grant       none
// ==/UserScript==

document.getElementsByClassName("btn btn-play btn-primary btn-needs-server")[0].innerHTML = "Jogar";
document.getElementsByClassName("btn btn-primary btn-success btn-freecoins")[0].innerHTML = "Moedas Grátis";
document.getElementsByClassName("btn btn-primary btn-shop")[0].innerHTML = "Loja";
document.getElementsByClassName("btn btn-warning btn-spectate btn-needs-server")[0].innerHTML = "Assistir";
document.getElementsByClassName("btn btn-danger btn-logout")[0].innerHTML = "Desconectar";
document.getElementsByClassName("btn btn-play-guest btn-success btn-needs-server")[0].innerHTML = "Jogar sem Conta";
document.getElementsByClassName("btn btn-warning btn-login-play btn-needs-server")[0].innerHTML = "Conectar";
document.getElementsByClassName("btn-text")[0].innerHTML = "Conectar com Facebook";
document.getElementsByClassName("btn-text")[1].innerHTML = "Conectar com Google";
document.getElementsByClassName("outlined-text")[0].innerHTML = "Conectando...";
document.getElementsByClassName("btn btn-info btn-copy-token copy-party-token")[0].innerHTML = "Copiar";
document.getElementById("create-party-btn").innerHTML = "Criar";
document.getElementById("joinPartyToken").placeholder = "Token da Party";
document.getElementById("join-party-btn").innerHTML = "Entrar";
document.getElementsByClassName("btn-donate")[0].innerHTML = "Doar por clique [Anúncio]";
document.getElementById("skin").placeholder = "URL da Skin (link direto)";
document.getElementById("clantag").placeholder = "TAG";

setInterval(repeat, 500);
function repeat{
    if (document.getElementsByClassName("agario-profile-name")[0].innerHTML == "Guest"){
        document.getElementsByClassName("agario-profile-name")[0].innerHTML = "Convidado";
    }
}

It opens agar.io with the szymy script (Ogar.io) and translates into English, but it does not work

2 answers

0


You wrote the wrong function, missed relatives ():

function repeat{
    if (document.getElementsByClassName("agario-profile-name")[0].innerHTML == "Guest"){
        document.getElementsByClassName("agario-profile-name")[0].innerHTML = "Convidado";
    }
}

The correct is:

function repeat() {
    if (document.getElementsByClassName("agario-profile-name")[0].innerHTML == "Guest"){
        document.getElementsByClassName("agario-profile-name")[0].innerHTML = "Convidado";
    }
}
  • I noticed this before I saw your reply, but thanks anyway.

0

The main problem is because you are searching elements for multiple classes directly using document.getElementsByClassName. Use document.querySelector instead of this method.

And instead of using setInterval you can use the requestAnimFrame implemented in Agar.io code (yes, it is global):

requestAnimFrame(function repeat() {

    /* Mudanças nesse bloco */

    requestAnimFrame(repeat);

});

But I don’t know if updating the game’s HTML with an interval is the most efficient way, but the game’s code is a bit obfuscated and so it’s hard to figure that out...

  • querySelector only returns the first element, I should use querySelectorAll

  • But something more strange, is that I tested the code on the firefox console (the original code) and it worked, so it must be something to do with greasemonkey. I’ll try to fix it

  • querySelectorAll returns a list (array) of elements, while querySelector only returns at least one element. This is the difference. I would use querySelector in such a case, but for the sake of cross-browser, would not use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.