中文说明:网页后台防暂停脚本
这是一个通用 Tampermonkey / Violentmonkey 油猴脚本,用来尽量阻止网页在切换标签页、窗口失焦、页面隐藏或页面冻结时强制暂停正在播放的视频和音频。
为什么会暂停
不少网站会在播放器脚本中监听 blur、visibilitychange、pagehide、freeze 等事件,然后主动调用 video.pause() 或 audio.pause()。
脚本怎么处理
脚本会在 document-start 尽早注入,观察后台相关事件但不阻断页面自己的事件监听器。在媒体正在播放或刚播放过时,它会临时把页面报告为可见状态,并且只拦截由后台事件触发的媒体暂停调用。
直播弹幕兼容
从 0.2.1 起,脚本不再吞掉 visibilitychange、blur 等页面事件。斗鱼、B 站直播等网站的弹幕、直播聊天和播放器 UI 仍然可以正常收到事件。
用户主动暂停兼容
从 0.2.2 起,脚本会识别近期真实的鼠标、触摸和播放器快捷键操作。拖动进度条或操作播放器触发 blur 后,用户主动点击暂停仍会被放行。
安装
- 安装 Tampermonkey 或 Violentmonkey。
- 点击页面顶部的 立即安装 / Install Now 按钮。
- 在脚本管理器中确认安装。
- 刷新需要后台播放的网站。
搜索关键词
视频后台播放油猴脚本、网页视频切标签暂停、切换标签页不暂停、页面隐藏不暂停、网页视频不暂停、bilibili 后台播放、youtube background play userscript、picture-in-picture background、Tampermonkey、Violentmonkey、visibilitychange、blur、pagehide、freeze。
限制
它不能绕过 DRM、付费权限、浏览器自动播放规则、网络限制或服务端限制。如果某个页面依赖真实可见性状态完成正常逻辑,启用脚本后可能影响该页面行为。
window.__backgroundMediaPlaybackKeepalive,可以查看已观察到的后台事件、已拦截的后台暂停和已放行的用户主动暂停次数。
English Guide: Prevent Video Pause on Tab Switch
This is a general Tampermonkey / Violentmonkey userscript that tries to keep video and audio playing when websites force playback to pause after tab switching, window blur, page hiding, or page freeze events.
Why Playback Stops
Many sites listen for blur, visibilitychange, pagehide, and freeze inside their player code, then actively call video.pause() or audio.pause().
How The Script Works
The script injects at document-start, observes background-related events without blocking the page's own listeners, temporarily reports the page as visible while media is active or recently active, and blocks only media pause calls triggered near those background events.
Live Danmaku Compatibility
Since 0.2.1, the script no longer swallows visibilitychange, blur, or related page events. Danmaku overlays, live chat, and player UI on sites such as Douyu and Bilibili Live can still receive those events normally.
Intentional Pause Compatibility
Since 0.2.2, recent trusted mouse, touch, and player-keyboard interactions allow intentional pause requests even if seeking or operating player controls caused a blur.
Install
- Install Tampermonkey or Violentmonkey.
- Click the prominent Install Now button at the top of this page.
- Confirm installation in your userscript manager.
- Refresh the site where you want background playback.
Search Keywords
Tampermonkey userscript, Violentmonkey, background playback, background video playback, keep video playing in background, prevent video pause on tab switch, hidden tab video playback, visibilitychange pause blocker, blur event pause blocker, pagehide, freeze, picture-in-picture background, youtube background play userscript, bilibili background playback.
Limits
It does not bypass DRM, paid access, browser autoplay rules, network limits, or server-side restrictions. On pages that require real visibility state for normal behavior, the script may change behavior while media is active.
window.__backgroundMediaPlaybackKeepalive in the console to inspect observed background events, blocked background pauses, and allowed user-requested pauses.