64

I have a WebView in my application in which some site is opened (always the same, it is my own page). The site has some JS code that loads some images from the remote host.

I want to intercept requests to such images (by URL pattern) and give back my own content (i.e. another image), or leave request untouched depending on internal application logic.

Is it possible to do that?

EDIT: The current state of the question...

WebView has the ability to set a WebViewClient (as noted by Axarydax). WebViewClient have two useful methods

  • shouldOverrideUrlLoading
  • onLoadResource

shouldOverrideUrlLoading is able to intercept any URL loading, if loading is triggered by page interaction (i.e. link on a page is clicked, WebView.loadURL("") isn't triggering this method). It is also able to cancel URL loading by returning false. This approach isn't usable, cause' it is not able to intercept loading of page resources (and images, what I need to intercept, is such a page resource).

onLoadResource is triggering every time that page resource (and images! thx to Jessyjones) are loading, but there is no way to cancel that. That makes this method not suitable for my task also.

Olegas
  • 9,749
  • 7
  • 46
  • 70

7 Answers7

71

Try this, I've used it in a personal wiki-like app:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("foo://")) {
            // magic
            return true;
        }
        return false;
    }
});
Sergey Glotov
  • 19,479
  • 11
  • 78
  • 93
Axarydax
  • 15,785
  • 19
  • 85
  • 147
  • Ok, it is the way to intercept call (by the way, will it trigger if my page loads an image, not doing a full reload?), but how can i supply response for the intercepted call produced by my Java code? – Olegas Jan 26 '11 at 14:54
  • 2
    you can use loadData() method of WebView to push some html into it. Usage: webView.loadData("...", "text/html", "utf-8"); – Axarydax Jan 28 '11 at 13:57
  • Not suitable. Static content loaded through loadData haven't access to additional content loading throug network. As a workaround loadData("javascript:....") can be used (to interact with some "client" code). This can be a good and suitable aproach if shouldOverrideUrlLoading is called for every page resource (content images exactly). But it didn't. – Olegas Jan 29 '11 at 10:29
  • What about if you use `url.endsWith("jpg")`? You just have to use it to jpeg, gif and png... Then, the code Azarydax provided should work, no? – gnclmorais Feb 01 '11 at 19:49
  • @MEGA shouldOverrideUrlLoading is NOT called for page resources (images are among them) – Olegas Feb 02 '11 at 06:18
  • `The method ShouldOverrideUrlLoading(WebView, String) of type new WebViewClient(){} must override or implement a supertype method` – Francisco Corrales Morales Jun 02 '14 at 15:34
  • and what about inspection ? (http://stackoverflow.com/questions/24000805/how-to-inspect-http-request-made-by-webviews) – Francisco Corrales Morales Jun 02 '14 at 23:03
  • 2
    Since API 24, this version of `shouldOverrideUrlLoading` is deprecated. To deal with that, see this question: https://stackoverflow.com/q/36484074/56285 – Jonik Jun 07 '17 at 10:45
54

It looks like API level 11 has support for what you need. See WebViewClient.shouldInterceptRequest().

Sergey Glotov
  • 19,479
  • 11
  • 78
  • 93
dkneller
  • 927
  • 8
  • 10
  • 11
    A word of CAUTION! If you intercept an attempt by the browser to retrieve an image (or probably any resource) and then return `null` (meaning let the WebView continue retrieving the image), any future requests to this resource might just go to the cache and will NOT trigger `shouldInterceptRequest()`. If you want to intercept EVERY image request, you need to disable the cache or (what I did) call `WebView.clearCache(true)`. – ADDruid Apr 17 '14 at 23:10
  • 2
    I am trying to add custom cookies and additional headers. Any idea how that could be done? – Karoly May 31 '17 at 20:24
  • Since API 24, the version of `shouldInterceptRequest` that takes `String url` is deprecated. To deal with that, see this question: https://stackoverflow.com/q/36484074/56285 (With regards to the deprecation, `shouldInterceptRequest` is analogous to `shouldOverrideUrlLoading`.) – Jonik Jun 07 '17 at 10:46
  • This works for the specific case in the question, but is not universal for all network calls, because the shouldInterceptRequest() method does not provide POST requests' data. – Miloš Černilovský Jul 26 '19 at 06:40
11

As far as I know, shouldOverrideUrlLoading is not called for images but rather for hyperlinks... I think the appropriate method is

@Override public void onLoadResource(WebView view, String url)

This method is called for every resource (image, styleesheet, script) that's loaded by the webview, but since it's a void, I haven't found a way to change that url and replace it so that it loads a local resource ...

jessyjones
  • 158
  • 6
  • Yes, with this method i got all the urls loaded (a root one, and all resources there). But still the question is - how to override loading =( I have some ideas on how to replace image content, but still it isn't clear how to override original call. – Olegas Jan 27 '11 at 10:55
9

Here is my solution I use for my app.

I have several asset folder with css / js / img anf font files.

The application gets all filenames and looks if a file with this name is requested. If yes, it loads it from asset folder.

//get list of files of specific asset folder
        private ArrayList listAssetFiles(String path) {

            List myArrayList = new ArrayList();
            String [] list;
            try {
                list = getAssets().list(path);
                for(String f1 : list){
                    myArrayList.add(f1);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (ArrayList) myArrayList;
        }

        //get mime type by url
        public String getMimeType(String url) {
            String type = null;
            String extension = MimeTypeMap.getFileExtensionFromUrl(url);
            if (extension != null) {
                if (extension.equals("js")) {
                    return "text/javascript";
                }
                else if (extension.equals("woff")) {
                    return "application/font-woff";
                }
                else if (extension.equals("woff2")) {
                    return "application/font-woff2";
                }
                else if (extension.equals("ttf")) {
                    return "application/x-font-ttf";
                }
                else if (extension.equals("eot")) {
                    return "application/vnd.ms-fontobject";
                }
                else if (extension.equals("svg")) {
                    return "image/svg+xml";
                }
                type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            }
            return type;
        }

        //return webresourceresponse
        public WebResourceResponse loadFilesFromAssetFolder (String folder, String url) {
            List myArrayList = listAssetFiles(folder);
            for (Object str : myArrayList) {
                if (url.contains((CharSequence) str)) {
                    try {
                        Log.i(TAG2, "File:" + str);
                        Log.i(TAG2, "MIME:" + getMimeType(url));
                        return new WebResourceResponse(getMimeType(url), "UTF-8", getAssets().open(String.valueOf(folder+"/" + str)));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }

        //@TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @SuppressLint("NewApi")
        @Override
        public WebResourceResponse shouldInterceptRequest(final WebView view, String url) {
            //Log.i(TAG2, "SHOULD OVERRIDE INIT");
            //String url = webResourceRequest.getUrl().toString();
            String extension = MimeTypeMap.getFileExtensionFromUrl(url);
            //I have some folders for files with the same extension
            if (extension.equals("css") || extension.equals("js") || extension.equals("img")) {
                return loadFilesFromAssetFolder(extension, url);
            }
            //more possible extensions for font folder
            if (extension.equals("woff") || extension.equals("woff2") || extension.equals("ttf") || extension.equals("svg") || extension.equals("eot")) {
                return loadFilesFromAssetFolder("font", url);
            }

            return null;
        }
EscapeNetscape
  • 2,444
  • 1
  • 27
  • 29
4

You don't mention the API version, but since API 11 there's the method WebViewClient.shouldInterceptRequest

Maybe this could help?

3

This may helps:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    String url = request.getUrl().toString();
    WebResourceResponse response = super.shouldInterceptRequest(view, request);
    // load native js
    if (url != null && url.contains(INJECTION_TOKEN/* scheme define */)) {

        response = new WebResourceResponse(
                "text/javascript",
                "utf-8",
                loadJsInputStream(url, JsCache.getJsFilePath(path) /* InputStream */));
    }
    return response;
}
WCG
  • 71
  • 4
  • 1
    The "text/javascript" like mimeType reference here: http://stackoverflow.com/questions/8589645/how-to-determine-mime-type-of-file-in-android – WCG Mar 29 '16 at 06:09
3

An ultimate solution would be to embed a simple http server listening on your 'secret' port on loopback. Then you can substitute the matched image src URL with something like http://localhost:123/.../mypic.jpg