网站的站长为了提高自己的收入往往会选择在网页中添加广告,有一些广告把内容覆盖住或者占据大部分位置,严重影响了用户体验。
1.思路
拦截我们先要获取到网页中所有请求链接,我们在哪里获取呢,通常网页的监听都是在WebViewClient
中去实现,通过查看WebViewClient
的源代码发现里面有个回调方法WebResourceResponse shouldInterceptRequest(WebView view, String url)
这个方法中的url
参数可以获取到所有的请求链接,我们需要一个规则文件里面包含大量广告host
,每次获取url
判断host
规则文件中是否包含次链接/js
名称/规则,对url
进行解析对比判断是否为广告,如果是就对其进行拦截。
2.实现
1.1 在assets
中创建一个host
文件,里面包含大量广告域名/规则,host
文件demo里面有的,也可以从服务器动态获取。我这里为了方便演示就放assets
里面了。
1.2 读取
host
文件按照行数进行读取添加到临时存储容器Set
集合中,
private static final String AD_HOSTS_FILE = "host.txt";
private static final Set<String> 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<String> AD_HOSTS = new HashSet<>();
public static void init(final Context context) {
new AsyncTask<Void, Void, Void>() {
@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<String, Boolean> 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.效果图
附上demo链接:https://github.com/Allyns/AllynWebView
5.扩展
有时候host
文件里面没有包含该域名怎么办?
我们需要手动标记为广告,然后把该广告隐藏掉。
网页加载完成
后隐藏广告图片可以使用js注入
的方式动态处理网页。
使用js代码
获取此ur
l的父元素
,然后隐藏掉父元素就可以实现这个需求
这里简单的附上代码供参考:
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广告拦截
还有很大的优化空间,欢迎大家可以给出更好的建议~