5 years ago (2016-05-02)  Algorithm language |   First to comment  563 
post score 0 times, average 0.0

Most of the Android browsers now have ad filtering, and most websites have ads.The advertising industry is a hugely profitable industry. From my point of view, most of the Internet products nowadays generally have two kinds of profit models: free advertising and no advertising for members; most of them (including websites and APPs, etc.) are profitable advertising sources. .As a user, they are very disgusted with advertising, but the services on the Internet we can enjoy and support for these services for free to continue and grow but benefit from advertising. Implementation of Android webview ad filtering Baidu took a look. There was no Android browser to screen out the code for advertising, so the very guests could only do it by themselves.The advertisement on the webpage is generally the webmaster implanting a js code on the webpage. To block the advertisement, it is only necessary to shield the js.

WebViewClient several callback functions

To implement some advanced operations on WebView, we must first learn the usage of WebViewClient, which is a few commonly used Webview callback function

  • 1, public boolean shouldOverrideUrlLoading (WebView view, String url) : In the click of the request is the link is called, overriding this method returns true indicates that click on the page inside the link or jump in the current webview, do not jump to the browser there.
  • 2. public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error): Overriding this method allows webview to process https requests.
  • 3, public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event): override this method to be able to handle key events in the browser.
  • 4, public void onLoadResource (WebView view, String url): will be called when the page resource is loaded, each resource (such as a picture) will be loaded once.
  • Public void onPageStarted(WebView view, String url, Bitmap favicon) : Called when the page is loaded.
  • 6, public void onPageFinished (WebView view, String url): Called at the end of the page load.

At first glance the above callback function, the extremely guest discovered a huge pit, the interception advertisement is intercepts the loading advertisement js, above onLoadResource seems to be a very appropriate function, as long as judges whether the parameter url of onLoadResource is to load advertisement js, If the advertisement-related url is not loaded normally, it will not be loaded.But after using onLoadResource it was found that it was not at all. Here we refer to another callback function of WebViewClient: public WebResourceResponse shouldInterceptRequest(WebView view, String url) There should be two overloads of shouldInterceptRequest.

  • Public WebResourceResponse shouldInterceptRequest (WebView view, String url) Introduced from API 11, API 21 is deprecated
  • Public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request) introduced from API 21

Extreme visitors here temporarily use interceptInterceptRequest (WebView view, String url) to complete the interception of webview ads.

Block inventory URL

When the WebView loads the resource, it will callback the interceptInterceptRequest function. We can handle the webview resource request by rewriting the shouldInterceptRequest function.The data is returned after processing.If the data returned by the main program is null, WebView will itself request the network to load resources.Here's a pit: Not the shouldInterceptRequest function returns null to block the request!The correct way to block requests:

The following is the NoAdWebViewClient class that the extremely guest writes to block ads: Just use webview.setWebViewClient(NoAdWebViewClient webclient) to block the advertisement of the specified webview.

ADFilterTool class to determine whether the URL contains ads: This class determines whether the url is included in the ad blocking library


Advertising url resource file (ad blocker can be self Baidu update): AdUrlString.xml

The so-called advertisement interception library is actually a collection of urls for requesting advertisement resources. There are a large number of advertisement interception libraries on the network, and readers can regularly update the documents to achieve efficient filtering of advertisements.This article is more crudely shielded, and all domain names containing advertising resources are prohibited.To achieve more precise filtering, visitors can use wildcards to match urls. This is what PC-based browsers do.



This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!

Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/realization-of-android-webview-advertising-filtering-2.html

Post comment


No Comment


Forget password?