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

189
Visualizações
Is there any way to listen for changes in the URL and trigger a JS function?

So, I have been working on this chrome extension that fetches the dislikes on a video from the youtube API and updates it on the dom.

let DISLIKE_COUNT = 0;

function convertToInternationalCurrencySystem (labelValue) {

    // Nine Zeroes for Billions
    return Math.abs(Number(labelValue)) >= 1.0e+9

    ? (Math.abs(Number(labelValue)) / 1.0e+9).toFixed(2) + "B"
    // Six Zeroes for Millions
    : Math.abs(Number(labelValue)) >= 1.0e+6

    ? (Math.abs(Number(labelValue)) / 1.0e+6).toFixed(2) + "M"
    // Three Zeroes for Thousands
    : Math.abs(Number(labelValue)) >= 1.0e+3

    ? (Math.abs(Number(labelValue)) / 1.0e+3).toFixed(2) + "K"

    : Math.abs(Number(labelValue));

}

const getDislikes = (dc) => {
    let val;

    for(let i = 1; i <= 99; i++){
        if(document.getElementsByTagName("yt-formatted-string")[i]?.innerText === "DISLIKE" || 
        document.getElementsByTagName("yt-formatted-string")[i]?.innerText === "Dislike"){
            console.log("true : " + i)
            val = i;
            break;
        }
        else{
            console.log("false");
        }
    }



    document.getElementsByTagName("yt-formatted-string")[val].innerText =  convertToInternationalCurrencySystem (dc); 
}


window.addEventListener("load", function() {
    const params = new URLSearchParams(window.location.search);
    const videoId = params.get('v');

    fetch(`https://www.googleapis.com/youtube/v3/videos?part=statistics&id=${videoId}&key=<yourAPIkey>`)
    .then(response => response.json())
    .then(data => 
        {
            DISLIKE_COUNT =data.items[0].statistics.dislikeCount;
            getDislikes(DISLIKE_COUNT);
        });

    

 });

Now this works pretty well but whenever I click on another video, I have to refresh the page to execute the JS code again and get the dislikes count from the API.

So, is there any way that will listen to the changes inside the URL and re-trigger the JS srcipt when the changes have been made?

about 4 years ago · Juan Pablo Isaza
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