android WebView实践总结(四) WebView网页广告拦截(AdBlock)

网站的站长为了提高自己的收入往往会选择在网页中添加广告,有一些广告把内容覆盖住或者占据大部分位置,严重影响了用户体验。

1.思路

拦截我们先要获取到网页中所有请求链接,我们在哪里获取呢,通常网页的监听都是在WebViewClient中去实现,通过查看WebViewClient的源代码发现里面有个回调方法WebResourceResponse shouldInterceptRequest(WebView view, String url)这个方法中的url参数可以获取到所有的请求链接,我们需要一个规则文件里面包含大量广告host,每次获取url判断host规则文件中是否包含次链接/js名称/规则,对url进行解析对比判断是否为广告,如果是就对其进行拦截。

2.实现

1.1 在assets中创建一个host文件,里面包含大量广告域名/规则,host文件demo里面有的,也可以从服务器动态获取。我这里为了方便演示就放assets里面了。

LE%MU{9$30D%CA6_UH)H(N4.png

1.2 读取host文件按照行数进行读取添加到临时存储容器Set集合中,

    private static final String AD_HOSTS_FILE = "host.txt";
    private static final Set AD_HOSTS = new HashSet<>();
    InputStream stream = context.getAssets().open(AD_HOSTS_FILE);
        InputStreamReader inputStreamReader = new InputStreamReader(stream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) AD_HOSTS.add(line);
        bufferedReader.close();
        inputStreamReader.close();
        stream.close();

1.3 解析url判断是否为广告

 if (TextUtils.isEmpty(host)) {
            return false;
        }
        int index = host.indexOf(".");
        return index >= 0 && (AD_HOSTS.contains(host) ||
                index + 1 < host.length() && isAdHost(host.substring(index + 1)));

3.完整代码

解析类AdBlocker

public class AdBlocker {

    private static final String AD_HOSTS_FILE = "host.txt";
    private static final Set AD_HOSTS = new HashSet<>();

    public static void init(final Context context) {
        new AsyncTask() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    loadFromAssets(context);
                } catch (IOException e) {
                    // noop
                }
                return null;
            }
        }.execute();
    }

    @WorkerThread
    private static void loadFromAssets(Context context) throws IOException {
        InputStream stream = context.getAssets().open(AD_HOSTS_FILE);
        InputStreamReader inputStreamReader = new InputStreamReader(stream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) AD_HOSTS.add(line);
        bufferedReader.close();
        inputStreamReader.close();
        stream.close();
    }

    public static boolean isAd(String url) {
        try {
            return isAdHost(getHost(url))||AD_HOSTS.contains(Uri.parse(url).getLastPathSegment());
        } catch (MalformedURLException e) {
            Log.d("AmniX", e.toString());
            return false;
        }

    }

    private static boolean isAdHost(String host) {
        if (TextUtils.isEmpty(host)) {
            return false;
        }
        int index = host.indexOf(".");
        return index >= 0 && (AD_HOSTS.contains(host) ||
                index + 1 < host.length() && isAdHost(host.substring(index + 1)));
    }

    public static String getHost(String url) throws MalformedURLException {
        return new URL(url).getHost();
    }

    public static WebResourceResponse createEmptyResource() {
        return new WebResourceResponse("text/plain", "utf-8", new ByteArrayInputStream("".getBytes()));
    }


}

调用

      //帮助WebView处理各种通知、请求事件
        mWebView.setWebViewClient(new WebViewClient() {
            private Map loadedUrls = new HashMap<>();
            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                boolean ad;
                if (!loadedUrls.containsKey(url)) {
                    ad = AdBlocker.isAd(url);
                    loadedUrls.put(url, ad);
                } else {
                    ad = loadedUrls.get(url);
                }
                return ad ? AdBlocker.createEmptyResource() :
                        super.shouldInterceptRequest(view, url);
            }
        });

4.效果图

拦截前.jpg

拦截后.jpg

附上demo链接:https://github.com/Allyns/AllynWebView

5.扩展

有时候host文件里面没有包含该域名怎么办?
我们需要手动标记为广告,然后把该广告隐藏掉。
网页加载完成后隐藏广告图片可以使用js注入的方式动态处理网页。
使用js代码获取此url的父元素,然后隐藏掉父元素就可以实现这个需求
这里简单的附上代码供参考:
1.隐藏图片父元素

        String js = "javascript: (function () {\n" +
                "    var aList = document.getElementsByTagName(\"img\");\n" +
                "    var parentList = [];\n" +
                "    for (var i = 0; i < aList.length; i++) {\n" +
                "        parentList = parentList.concat([aList[i].parentElement]);\n" +
                "    }\n" +
                "    for (var i = 0; i < aList.length; i++) {\n" +
                "        if (aList[i].getAttribute(\"src\").indexOf(\"" + url + "\") != -1) {\n" +
                "            parentList[i].style.display = \"none\";\n" +
                "        }\n" +
                "    }\n" +
                "})();";
        getWebView().loadUrl(js);

2.隐藏a标签

        String js = "javascript: (function () {\n" +
                "    var aList = document.getElementsByTagName(\"a\");\n" +
                "    var parentList = [];\n" +
                "    for (var i = 0; i < aList.length; i++) {\n" +
                "        parentList = parentList.concat([aList[i].parentElement]);\n" +
                "    }\n" +
                "    for (var i = 0; i < aList.length; i++) {\n" +
                "        if (aList[i].getAttribute(\"href\").indexOf(\"" + url + "\") != -1) {\n" +
                "            parentList[i].style.display = \"none\";\n" +
                "        }\n" +
                "    }\n" +
                "})();";
        LogHelper.i("wessd", js);
        getWebView().loadUrl(js);

WwebView广告拦截还有很大的优化空间,欢迎大家可以给出更好的建议~

你可能感兴趣的:(android WebView实践总结(四) WebView网页广告拦截(AdBlock))