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

278
Vistas
bookmarklet: getting current time of embedded iframe youtube player?

I am novice javascript developer. I have a bookmarklet that can get the current time of a youtube video.

javascript:(function(){
    ytplayer = document.getElementById("movie_player");
    var secs = ytplayer.getCurrentTime();
    alert(`${secs}`);
})();

It works fine on youtube.com. However, when I apply the same logic to an iframe-based website (url here). According to youtube API reference, I use function onYouTubePlayerAPIReady(). but nothing happens when I run the bookmarklet. Here is the code

javascript:function onYouTubePlayerAPIReady() {
   var ytplayer = new YT.Player('widget2');
   var secs = ytplayer.getCurrentTime();
   alert(`${secs}`);
}

Does any youtube API expert can spot the issue?

Here is the iframe of the sample youtube from the website

<iframe frameborder="0" allowfullscreen="1" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" width="100%" height="100%" src="https://www.youtube.com/embed/eSWwIQzKsbY?autoplay=1&amp;mute=0&amp;controls=1&amp;start=0&amp;origin=https%3A%2F%2Fyou-tldr.com&amp;playsinline=1&amp;showinfo=0&amp;rel=0&amp;iv_load_policy=3&amp;modestbranding=1&amp;enablejsapi=1&amp;widgetid=1" id="widget2" data-gtm-yt-inspected-1_25="true"></iframe>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yes. I have a similar need. I don't think you called the onYouTubeIframeAPIReady() function. This is the working code.

function onYouTubeIframeAPIReady() {
        var player = new YT.Player('widget2', {//The id of the div containing the iframe. Should be changed based on the page you're at and the i they gave their player
            events: {
                'onStateChange': onPlayerStateChange//Event listener, fires onPlayerStateChange function whenever the state of the video changes, paused or played 
            }
        });

        function onPlayerStateChange() {
            currentTime = player.playerInfo.currentTime;//Current Time saved
            if (player.playerInfo.playerState === 2) {//If video paused, console.log the time
                console.log(currentTime);
            }
        }
    }
onYouTubeIframeAPIReady();//Initial call to the function
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