Created
January 4, 2016 16:09
-
-
Save rjeschke/eb1bb76128c5e9a9e7bc to your computer and use it in GitHub Desktop.
Basic WebView blocking using libadblockplus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment