Search code examples
javascriptgoogle-chrome-devtoolsyoutube-javascript-apigoogle-chrome-console

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 ?


Solution

  • 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)