javascript - 达到 3 分钟后从 chrome 控制台暂停 YouTube 视频

标签 javascript google-chrome-devtools youtube-javascript-api google-chrome-console

我可以暂停视频 globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0].pause()

我可以使用 globalThis.ytPlayerUtilsVideoTagPoolInstance.l[0].currentTime 获取视频的当前时间

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

最佳答案

一种简单的方法是轮询。

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

另一种方式:

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)

关于javascript - 达到 3 分钟后从 chrome 控制台暂停 YouTube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53933106/

相关文章:

javascript - document.write 在 Youtube videobar 的 javascript 中表现异常

node.js - 如何在 YouTube API 中使用 ETag

javascript - 渲染所有可能的元素或根据要求渲染

javascript - 谷歌浏览器的 console.log() 打印 DOM 节点不一致

javascript - splice 从数组及其父数组中删除 - 奇怪的问题

google-chrome-devtools - Chrome DevTools 中长帧的含义

google-chrome - 使用 `inspect an element`工具创建全局Javascript变量

javascript - UIWebView 不使用方法 stringByEvaluatingJavaScriptFromString : 评估 java 脚本

javascript - 数组/对象排序

javascript - 进行 CSS transformY 时控制 Z-index 或 Z 位置