Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 343
PeterPam Avatar asked Nov 29 '25 10:11

PeterPam


1 Answers

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()
like image 98
gaetanoM Avatar answered Dec 02 '25 00:12

gaetanoM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!