Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause a youtube video from the chrome console after it reaches 3 minutes

I can pause the video with globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0].pause()

I can get the current time of the video with globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0].currentTime

How do I trigger the pause automatically when the video reaches currentTime > 180 ?

like image 342
Julien Reszka Avatar asked Jan 31 '26 13:01

Julien Reszka


1 Answers

An easy way is polling.

const player = globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0]
setTimeout(function polling() {
    if (player.currentTime < 180) 
        setTimeout(polling, 1000)
    else
        player.pause()
}, 1000)

An alternative way:

const player = globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0]
const timer = setInterval(() => { // no need of named function as no 'async recursion' is needed
    if (player.currentTime >= 180) {
        player.pause()
        clearInterval(timer)
    }
}, 1000)
like image 111
obfish Avatar answered Feb 03 '26 03:02

obfish



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!