gpt4 book ai didi

javascript - Chrome 扩展 : How to Disable Page Visibility API

转载 作者:太空狗 更新时间:2023-10-29 15:34:02 26 4
gpt4 key购买 nike

我正在编写一个 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 浏览器代码可以实现这一点?

最佳答案

这是我的解决方案:

for (event_name of ["visibilitychange", "webkitvisibilitychange", "blur"]) {
window.addEventListener(event_name, function(event) {
event.stopImmediatePropagation();
}, true);
}

我添加了 blur 事件,因为我想跳过的视频 (everfi.net) 使用它来检测我何时切换窗口。阻止该事件以及 visibilitychangewebkitvisibilitychange 就成功了:)

我还修改了扩展程序的 list ,以便它可以在 iframe 中运行。

完整代码(chrome 扩展):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

关于javascript - Chrome 扩展 : How to Disable Page Visibility API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47660653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com