首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AdBlock WebView?

AdBlock WebView?
EN

Stack Overflow用户
提问于 2016-02-15 18:25:34
回答 1查看 12.2K关注 0票数 4

有没有办法屏蔽Android WebView中的广告?我正在建设的应用程序,让用户浏览网页,但需要阻止广告。它基本上是一个自定义浏览器,但我需要摆脱广告。

我最好的选择是什么?

基于:https://github.com/adblockplus/adblockplusandroid

看到这个:https://gist.github.com/rjeschke/eb1bb76128c5e9a9e7bc

代码语言:javascript
复制
import java.io.File;
import java.util.regex.Pattern;

import org.adblockplus.android.ABPEngine;
import org.adblockplus.libadblockplus.FilterEngine.ContentType;

import android.content.Context;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class BlockingWebViewClient extends WebViewClient
{
  private ABPEngine engine;

  private static final Pattern RE_JS = Pattern.compile("\\.js$",   Pattern.CASE_INSENSITIVE);
  private static final Pattern RE_CSS = Pattern.compile("\\.css$", Pattern.CASE_INSENSITIVE);
  private static final Pattern RE_IMAGE = Pattern.compile("\\.(?:gif|png|jpe?g|bmp|ico)$", Pattern.CASE_INSENSITIVE);
  private static final Pattern RE_FONT = Pattern.compile("\\.(?:ttf|woff)$", Pattern.CASE_INSENSITIVE);
  private static final Pattern RE_HTML = Pattern.compile("\\.html?$", Pattern.CASE_INSENSITIVE);

  public void start(Context context)
 {
    final File basePath = context.getFilesDir();
    this.engine = ABPEngine.create(context, ABPEngine.generateAppInfo(context),
    basePath.getAbsolutePath());

// Additional steps may be required here, i.e. :
// - subscription selection or updating
// - maybe also setting other options (like AcceptableAds)
// It might also be a good idea to delay the first calls until
// everything is loaded, have a look at AndroidFilterChangeCallback
// and ABPEngine.create()
  }

  @Override
  public WebResourceResponse shouldInterceptRequest(WebView view, String url)
  {
// Determine the content
 ContentType contentType = null;
if (RE_JS.matcher(url).find())
{
  contentType = ContentType.SCRIPT;
}
else if (RE_CSS.matcher(url).find())
{
  contentType = ContentType.STYLESHEET;
}
else if (RE_IMAGE.matcher(url).find())
{
  contentType = ContentType.IMAGE;
}
else if (RE_FONT.matcher(url).find())
{
  contentType = ContentType.FONT;
}
else if (RE_HTML.matcher(url).find())
{
  contentType = ContentType.SUBDOCUMENT;
}
else
{
  contentType = ContentType.OTHER;
}
// Check if we should block ... we sadly do not have the referrer chain here,
// might also be hard to get as we do not have HTTP headers here
if (engine.matches(url, contentType, new String[0]))
{
  // If we should block, return empty response which results in a 404
  return new WebResourceResponse("text/plain", "UTF-8", null);
}
// Otherwise, continue by returning null
return null;
}

}

EN

回答 1

Stack Overflow用户

发布于 2018-06-29 23:49:24

它已经完成了:https://github.com/adblockplus/libadblockplus-android

您可以将其放在xml中:

代码语言:javascript
复制
<org.adblockplus.libadblockplus.android.webview.AdblockWebView
android:id="@+id/main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35406852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档