Android WebView禁用iframe加载

时间:2014-05-22 09:27:56

标签: java android html android-webview

在我的应用程序中,我有一个显示一些网页的WebView。 一些网页包含iframe。和一些iframe加载警告框,确认框,提示和弹出窗口。

我想阻止加载iframe而不加载它?或至少禁用这些弹出窗口(不禁用javascript)

我试图通过以下方法过滤来阻止这些页面

in the on page started method 
use the if condition to match the url with the url which gives popups
if matched restart the webview

然而我发现url总是主站点,而不是iframe加载的站点,所以if语句永远不会是真的

如何阻止iframe或弹出窗口?

任何帮助将不胜感激

三江源

1 个答案:

答案 0 :(得分:0)

您需要实现 WebViewClient 并覆盖 shouldOverrideUrlLoading

检查它是否是带有 if (!request.isForMainFrame()) 的 iframe。 然后您可以手动加载 url 的内容并对其进行清理。

@TargetApi(24)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    if (!request.isForMainFrame()) {
        if (shouldBlock(request.getUrl()))
            return true;
    }
    return super.shouldOverrideUrlLoading(view, request);
}
相关问题