Chrome扩展程序:如何禁用页面可见性API [英] Chrome Extension: How to Disable Page Visibility API
问题描述
我正在撰写Chrome扩展程序,该扩展程序需要防止网页触发文档visibilitychange事件。至少我需要能够覆盖document.visibilityState(即使它是只读属性)。如果不可能,由于此扩展程序仅供我使用,并且不会在Chrome扩展程序商店中使用,因此我可以通过配置Chrome浏览器来实现我想要的功能吗?我只需要在Chrome开发者模式开启的情况下使用此扩展程序,无需其他时间。
我希望有人能够想出一种创造性的方式来实现这一点。谢谢。
请注意!在4年前的答案中有一个解决方案,在新版Chrome中不再起作用:
欺骗或禁用页面可见性API
自己测试一下:
//此代码在4年前工作,但不再是
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'));
EremoveAttribute('onreset');
//这将仍然记录状态,即使上面的代码运行
document.addEventListener(visibilitychange,function(){
console.log(document.visibilityState) ;
});
如果在Chrome中不可行,是否有Firefox / Safari / Opera浏览器代码可以实现此目的?
以下是我的解决方案:
<$ c (); $ b $ window.addEventListener(event_name,function(event){
event.stopImmediatePropagation();
},true);
}
我添加了 blur
事件,因为我想跳过的视频(everfi.net)用它来检测我何时切换窗口。阻止该事件与 visibilitychange
和 webkitvisibilitychange
一起使用:)
我也修改了扩展的清单,以便它在iframe中工作。
完整代码: https://github.com/ NavinF / dont
证实可以使用这些狗标签:
Google Chrome 63.0.3239.132(官方版本)(64位)
版本2e6edcfee630baa3775f37cb11796b1603a64360-refs / branch-heads / 3239 @ {#709}
操作系统Mac OS X
JavaScript V8 6.3.292.49
Command Line / Applications / Google Chrome.app/Contents/MacOS/Google Chrome --flag-switches-begin --flag-switch-end
I am writing a Chrome extension that needs to prevent webpages from triggering the document visibilitychange event. At the very least I need to be able to overwrite the document.visibilityState (even though it is a read-only property). If not possible, since this extension is for my purposes only and will not be on the Chrome extension store, is there a way I can config my Chrome Browser to achieve what I want? I only need to use this extension while Chrome "Developer Mode" is on, no other time.
I hope someone can think of a creative way to achieve this. Thank you.
Please note! There was a solution in an answer 4 years ago that no longer takes effect in newer versions of Chrome: Spoof or disable the Page Visibility API
Test it out yourself:
// 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 );
});
If its not possible in Chrome, is there Firefox/Safari/Opera Browser code that can achieve this?
Here's my solution:
for (event_name of ["visibilitychange", "webkitvisibilitychange", "blur"]) {
window.addEventListener(event_name, function(event) {
event.stopImmediatePropagation();
}, true);
}
I added the blur
event because the video I wanted to skip (everfi.net) used it to detect when I switched windows. Blocking that event along with visibilitychange
and webkitvisibilitychange
did the trick :)
I also modified the extension's manifest so that it works inside iframes.
Full code: https://github.com/NavinF/dont
Confirmed working with these dog tags:
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
这篇关于Chrome扩展程序:如何禁用页面可见性API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!