Right now I'm using this line of code to at least try to block popups by JavaScript in webview:
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
1) I don't get it why I have to switch this to "true" that it's working
2) are there any other techniques for blocking popups in webview?
Help is much appreciated.
preamble
We are at the
WebViewsetup side of the equation.At first glance an obvious comment, but if you don't need
JavaScript, don't enableJavaScript, then you don't getJavaScriptpopup's. I'm assuming you DO needJavaScript(remember it may be XSS vulnerable) and want to do what you can to disable the popups that can inevitably follow.INFO:
WebViewClient. Override this behavior of your
WebView, e.g. so links open within yourWebView. WebChromeClient lets you handleJavascript's alert()and other functions.OP(1)
setJavaScriptCanOpenWindowsAutomatically(true)is usually blocked only when done outside of an event handler.OP = Original Post ;O).
Let's setup a senario
This is how I setup my normal
webview:OP(2) Let's block what we can
From @markproxy If you extend
WebChromeClient, you can override itsonJsAlert()method and block the built-in handler for alerts. While you're at it, you will probably want to block calls to theconfirm()andprompt():