我正在编写一个Chrome扩展,需要防止网页触发文档可见性更改事件。至少,我需要能够覆盖document.visibilityState (即使它是一个只读属性)。如果不可能,因为这个扩展只是为了我的目的,而且不会在Chrome扩展存储中,我有什么方法可以配置我的Chrome浏览器来实现我想要的吗?我只需要使用这个扩展,而Chrome“开发模式”是打开,没有其他时间。
我希望有人能想出一种创造性的方法来实现这一点。谢谢。
请注意!在4年前的一个答案中,有一个解决方案不再在更新版本的Chrome中生效: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浏览器代码可以实现这一点吗?
发布于 2018-02-06 11:14:15
这是我的解决方案:
for (event_name of ["visibilitychange", "webkitvisibilitychange", "blur"]) {
window.addEventListener(event_name, function(event) {
event.stopImmediatePropagation();
}, true);
}
我添加了blur
事件,因为我想跳过的视频(everfi.net)在切换窗口时使用它来检测。与visibilitychange
和webkitvisibilitychange
一起阻止该事件的技巧是:)
我还修改了扩展的清单,使其在iframes中工作。
完整代码(铬扩展名): 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
https://stackoverflow.com/questions/47660653
复制相似问题