Greasy Fork is available in English.
删除网页中的 blur 和 visibilitychange 事件监听器,防止视频暂停或网页限制后台播放。
// ==UserScript==
// @name 阻止网页视频自动暂停
// @namespace http://tampermonkey.net/
// @version 1.1
// @description 删除网页中的 blur 和 visibilitychange 事件监听器,防止视频暂停或网页限制后台播放。
// @author you
// @match *://*/*
// @run-at document-start
// @license MIT
// ==/UserScript==
(function() {
'use strict';
// 屏蔽 window/document 的 onblur/onvisibilitychange
Object.defineProperty(window, 'onblur', { set: () => {}, get: () => null });
Object.defineProperty(window, 'onvisibilitychange', { set: () => {}, get: () => null });
Object.defineProperty(document, 'onvisibilitychange', { set: () => {}, get: () => null });
// 劫持 addEventListener
const rawAddEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function(type, listener, options) {
if (type === 'blur' || type === 'visibilitychange') {
console.log('[Tampermonkey] 阻止事件监听:', type);
return; // 不注册这类事件
}
return rawAddEventListener.call(this, type, listener, options);
};
// 劫持 removeEventListener 防止报错
const rawRemoveEventListener = EventTarget.prototype.removeEventListener;
EventTarget.prototype.removeEventListener = function(type, listener, options) {
if (type === 'blur' || type === 'visibilitychange') {
return; // 假装已经移除了
}
return rawRemoveEventListener.call(this, type, listener, options);
};
// 清理已绑定的事件(如果脚本注入稍晚)
window.addEventListener('DOMContentLoaded', () => {
window.onblur = null;
document.onvisibilitychange = null;
});
})();