我正在编写一个 Chrome 扩展程序,需要防止网页触发文档 VisibilityChange 事件。至少我需要能够覆盖 document.visibilityState (即使它是只读属性)。如果不可能,因为此扩展程序仅供我使用,不会出现在 Chrome 扩展程序商店中,有什么方法可以配置我的 Chrome 浏览器来实现我想要的功能?我只需要在 Chrome“开发者模式”打开时使用此扩展程序,其他时间都不需要。
我希望有人能想出一种创造性的方法来实现这一目标。谢谢。
请注意! 4年前的一个答案中有一个解决方案,在较新版本的Chrome中不再生效:欺骗或禁用页面可见性 API https://stackoverflow.com/questions/18928417/spoof-or-disable-the-page-visibility-api
自己测试一下:
// This codes worked 4 years ago but not anymore
var c='(function(){var a=Node.prototype.addEventListener;Node.prototype.addEventListener=function(e){if(e=="visibilitychange"||e=="webkitvisibilitychange"){}else a.apply(this,arguments)}})()'
, E=document.documentElement;
E.setAttribute('onreset', c);
E.dispatchEvent(new CustomEvent('reset'));
E.removeAttribute('onreset');
// THIS WILL STILL LOG THE STATES EVEN WITH THE ABOVE CODE RUNNING
document.addEventListener("visibilitychange", function() {
console.log( document.visibilityState );
});
如果在 Chrome 中不可能,是否有 Firefox/Safari/Opera 浏览器代码可以实现此目的?
这是我的解决方案:
for (event_name of ["visibilitychange", "webkitvisibilitychange", "blur"]) {
window.addEventListener(event_name, function(event) {
event.stopImmediatePropagation();
}, true);
}
我添加了blur
事件,因为我想跳过的视频(everfi.net)使用它来检测我何时切换窗口。阻止该事件以及visibilitychange
and webkitvisibilitychange
成功了:)
我还修改了扩展的清单,以便它可以在 iframe 内运行。
完整代码(chrome扩展):https://github.com/NavinF/dont https://github.com/NavinF/dont
已确认使用以下狗牌:
Google Chrome 63.0.3239.132 (Official Build) (64-bit)
Revision 2e6edcfee630baa3775f37cb11796b1603a64360-refs/branch-heads/3239@{#709}
OS Mac OS X
JavaScript V8 6.3.292.49
Command Line /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --flag-switches-begin --flag-switches-end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)