达到3分钟后,从Chrome控制台暂停youtube视频

jul*_*ien 3 javascript google-chrome-devtools youtube-javascript-api google-chrome-console

我可以用 globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0].pause()

我可以通过以下方式获取视频的当前时间 globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0].currentTime

视频到达时如何自动触发暂停currentTime > 180

obf*_*ish 5

一种简单的方法是轮询。

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

另一种方法:

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)
Run Code Online (Sandbox Code Playgroud)