如何跳过网页不可跳过视频?现有脚本失效问题求解
解决视频跳转脚本失效的问题及替代方案
常见失效原因及对应解决办法
视频元素被嵌套在iframe或Shadow DOM中
很多网站会把视频放在iframe里隔离,或者用Shadow DOM封装组件,直接document.querySelector('video')找不到目标元素。- 处理iframe:先定位iframe,再操作内部文档的视频元素
const iframe = document.querySelector('iframe'); if (iframe) { iframe.onload = () => { const video = iframe.contentDocument.querySelector('video'); if (video && !isNaN(video.duration)) { video.currentTime = video.duration; } }; } - 处理Shadow DOM:递归遍历所有带Shadow根的元素,找到内部的视频
function findVideo(root) { let video = root.querySelector('video'); if (video) return video; for (const el of root.querySelectorAll('*')) { if (el.shadowRoot) { video = findVideo(el.shadowRoot); if (video) return video; } } return null; } const video = findVideo(document); if (video && !isNaN(video.duration)) { video.currentTime = video.duration; }
- 处理iframe:先定位iframe,再操作内部文档的视频元素
网站监听视频时间变化并强制重置
不少培训网站会监听timeupdate事件,一旦检测到进度被手动修改,就把进度拉回原位。可以先移除监听再设置进度:const video = document.querySelector('video'); if (video) { // 移除所有timeupdate监听事件 const timeupdateListeners = getEventListeners(video).timeupdate || []; timeupdateListeners.forEach(item => { video.removeEventListener('timeupdate', item.listener); }); // 设置进度到结尾并触发结束事件 video.currentTime = video.duration; video.dispatchEvent(new Event('ended')); }视频未加载完成,duration为NaN
如果视频元数据还没加载,video.duration会是NaN,导致设置失效。需要等元数据加载完成再操作:const video = document.querySelector('video'); if (video) { if (!isNaN(video.duration)) { video.currentTime = video.duration; } else { video.addEventListener('loadedmetadata', () => { video.currentTime = video.duration; }); } }网站用自定义播放器而非原生video标签
有些网站完全封装了播放器,原生video被隐藏或嵌套极深,此时可以尝试模拟点击播放器的“完成”按钮:// 注意:选择器需要根据网站实际DOM结构调整 const finishBtn = document.querySelector('.finish-btn, .complete-video, .player-end'); if (finishBtn) finishBtn.click();
关于通用脚本的说明
不存在能适配所有网页的通用脚本,因为不同网站的视频播放实现差异太大:有的用原生组件,有的自定义播放器,还有的加了各种防篡改逻辑。只能针对具体网站的DOM结构和逻辑调整脚本。
内容的提问来源于stack exchange,提问作者Prakerja
相关产品推荐
相关产品推荐

