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

211
Vistas
I can't get the firstChild element on my youtube notifications popup

I am trying to make a chrome extension. I try to create a div and put it at the top of my notifications popup on youtube that we open by clicking on this button.

So I try to catch the first child of my container and make a insertBefore. But I have a problem : I can't get the firstChild element of my container, or it is always null.

I try this code, but I don't know if it's the good way :

const notificationsContainer = document.querySelector("#container > #sections");
notificationsContainer.style.backgroundColor = "blue"; //to view my container

/* Creation of my new div */
let newDiv = document.createElement("div");
newDiv.style.backgroundColor = "green";
newDiv.style.height = "50px";
newDiv.style.width = "50px";
newDiv.style.top = "0";

/* Insert my newDiv */
let parentElement = document.querySelector("#items");
let childElement = parentElement.firstChild;
parentElement.insertBefore(newDiv, childElement);

This is when I print my parentElement and childElement in the console (they are empty) : console print

Thank you for your help !

P.S. I'm trying to get my notifications so I scrap them with js, if you know a better way I'd be happy to listen to it.

Edit : Here an inspector screenshot focuses on the selected elements.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Are you sure the the element you're targetting has a child element? If it does not, .firstChild will return null.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There might be an issue with timing. The element is empty when you grab it and filled after that.

Maybe you need to wait for "some time".

Try to grab item after couple of seconds by setting the timeout (setTimeout).

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