Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Android WebView/WebViewClient Initiated favicon.ico Request

How can I disable the Android WebView/WebViewClient from sending out a request for favicon.ico when I call WebView.loadUrl()? I can see the call being made while profiling requests via CharlesProxy.

I do not own the HTML content that I am displaying in the WebView. My research has turned up a lot of results on workarounds from the server side but these won't work for me.

like image 427
Travis Yim Avatar asked Jan 19 '16 20:01

Travis Yim


People also ask

What is Webviewclient in Android?

Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.

Why do I get favicon ICO?

ico' and is typically fetched from website.com/favicon.ico. Your browser will automatically request it when browsing to different sites. If your browser receives a valid favicon. ico file, it will display this icon.

How do I close WebView on Android?

Add a close button and on its click set: webview. setVisibility(View. INVISIBLE); webview.


2 Answers

for me the complete solution was:

   @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

        if(url.toLowerCase().contains("/favicon.ico")) {
            try {
                return new WebResourceResponse("image/png", null, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    @SuppressLint("NewApi")
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {

        if(!request.isForMainFrame() && request.getUrl().getPath().endsWith("/favicon.ico")) {
            try {
                return new WebResourceResponse("image/png", null, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }
like image 178
ventura8 Avatar answered Sep 27 '22 18:09

ventura8


I achieved this by a little hack. First, I've created a fake 1x1 icon file and saved it to the assets folder. Then I overrode WebViewClient's shouldInterceptRequest() method, where I check the URL whether it is the request for favicon file and in that case return WebResourceResponse with InputStream which contains our fake icon:

    @Override
    @CallSuper
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        if(!request.isForMainFrame() && request.getUrl().getPath().equals("/favicon.ico")) {
            try {
                return new WebResourceResponse("image/png", null, new BufferedInputStream(view.getContext().getAssets().open("empty_favicon.ico")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

Note that the InputStream must not be closed in our code, because it is subsequently used by the WebView to read the icon. The WebviewClient must be set to the WebView via its setter:

mWebView.setWebViewClient(subclassedWebViewClient);
like image 39
Miloš Černilovský Avatar answered Sep 27 '22 18:09

Miloš Černilovský