Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
Why is Javascript Prompt only showing up when Youtube is refreshed?

I'm making a simple Chrome Extension that pops out a prompt when entering certain websites. My main one would be Youtube. Here's the code that I have so far:

if(window.location.hostname ==="www.youtube.com") {
   let why = prompt("Why are you on Youtube/Watching this video?", "");   
   }; 

This works fine when entering youtube for the first time, when refreshing a page, or when going back a page using the back arrow. However, I would like it to pop up whenever I click a video, or switch pages (for example, going from the Recommended tab to the Subscriptions tab). How can I do this?

EDIT: I found code that works on this question: Detect Youtube video change with injected javascript Here's my final code:

function run(){
    if(window.location.hostname ==="www.youtube.com") {
       let why = prompt("Why are you on Youtube/Watching this video?", "");
   
       };  
}
window.onload = run;
window.addEventListener('yt-navigate-start', run,   true);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Youtube is a Single Page Application (SPA), so the page does not reload when a video is clicked. You can read more about it here.

The code you posted is only executed when the page loads, and that happens, as you already noticed, when you first open youtube or refresh the page.

Perhaps you can try to listen to title changes, which change on each video and page, using this code snippet that I got from this thread.

    new MutationObserver(function(mutations) {
        console.log(mutations[0].target.nodeValue);
    }).observe(
        document.querySelector('title'),
        { subtree: true, characterData: true, childList: true }
    );
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda