I tried the approach described in the accepted answer, from Kalel Wade, and by injecting JavaScript on every progress event I was able to block popups. However, I found what appears to be a much more elegant approach.
If you extend WebChromeClient, you can override its onJsAlert() method and block the built-in handler for alerts. While you're at it, you will probably want to block calls to confirm() and prompt():
WebChromeClient webChromeClient = new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
result.cancel();
return true;
}
@Override
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
result.cancel();
return true;
}
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
result.cancel();
return true;
}
};
webView.setWebChromeClient(webChromeClient);
Given that you're seeing JavaScript alerts, I believe you must have already assigned a WebChromeClient. (If you don't do so, alerts are not supported.) So it should just be a matter of adding the overrides above.
Be sure to call result.cancel() prior to returning. From my experience, if you don't do this, the JavaScript engine seems to hang; the button stays in the pressed state, and no subsequent interaction is registered.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…