Search code examples
javascripthtmlyoutube

getting current time of youtube videos while playing


When i play a Youtube video like this:

https://www.youtube.com/tv?#/watch/video/control?v=d1-VK12FZhs&resume&t=0m4s

i see the time playing and duration at bottom of video.

i can know current time playing looking for this in the page:

< span class="ytp-time-current">0:11

After a little time this info is hidden until i pass again the mouse pointer over the video.

When the info is shown, i can get ytp-time-current value updated every second. When the info is hidden, this value is not updated. How to take the current time while the info is hidden?

I use this inside a chrome extension with this code:

document.addEventListener("spfdone", process); 
document.addEventListener("DOMContentLoaded", process); 

function process(){ 
stop(); 
start(); 
} 
window.onbeforeunload = function () { 
stop(); 
}; 

function start(){ 

x2 = setInterval(function() { 

// here is the place to use the solution i'm looking for

},1000); 
} 

function stop(){ 
}

Thanks in advance.


Solution

  • According to YouTube iframe api you can use: getCurrentTime().

    It's enough to write in chrome console the following code:

    document.querySelector('.video-stream').getCurrentTime()