How to prevent Google Chrome suppressing dialogs?

痞子三分冷 提交于 2019-12-23 09:17:28

问题


How can I prevent Chrome console giving the following error when I try to use the prompt() command?

A window.prompt() dialog generated by this page was suppressed because this page is not the active tab of the front window. Please make sure your dialogs are triggered by user interactions to avoid this situation. https://www.chromestatus.com/feature/5637107137642496

Up until 2-3 days ago the prompt() command worked fine and would open a dialog where I could input data, now it always gives me this error even if I am definitely on the active tab. I have checked chrome content settings and allow popups is checked.

Thanks!


回答1:


The buggy behaviour here is because, when debugging using the developer tools, the developer tools window is actually the active one. A workaround is to use the play or step buttons that render over the tab that is actually being debugged, as shown below.




回答2:


You have to dock the developer window in the same window. Don't undock into a separate window. This helped me. EDIT: Screenshot In Undocked mode That is no active window attached. And when It is docked to left || right || down the window It works Properly




回答3:


I wrote this line in child window, then the dialog popup in child, and everything works fine.

window.opener.confirm = window.confirm;



回答4:


Basically prompt() does still work, but it either must be part of the main code (scripts preloaded onload of the web page), as otherwise if this is let's say tried from the dev tools it simply won't work - because the output will be shown in a Separate/Distinct tab than the one you're coding in (console in dev tools).

I guess this is to prevent invasive behavior that we were getting up to few years ago, where you are browsing on tab-x (eg bbc news) whilst suddenly you get focused to tab y (eg pirate site) for more clickbait, and this was all thanks to prompt and alerts.

In order to confirm or understand exactly what I'm trying to explain here, open console/dev tools on any tab you'd like to, paste in:

setTimeout (function() {
    prompt("Comprende ?")
},5000);

and after hitting return key, swiftly switch focus by clicking anywhere which doesn't redirect you to a new page on the tab you opened the devtools from, and after few seconds prompt would work.

Basically prompt before executing checks that the tab it's about to execute in, is the active focused one from the gazillion open tabs one might have.



来源:https://stackoverflow.com/questions/43901822/how-to-prevent-google-chrome-suppressing-dialogs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!