拦截请求android webview

时间:2015-07-29 04:32:59

标签: javascript android css webview

拦截webview应用程序中添加的多个css。

    mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public WebResourceResponse shouldInterceptRequest(final WebView view, String url) {
    if (url.contains(".css")) {
    return getCssWebResourceResponseFromAsset();
    } else {
    return super.shouldInterceptRequest(view, url);
    }
    }


    private WebResourceResponse getUtf8EncodedCssWebResourceResponse(InputStream data) {
    return new WebResourceResponse("text/css", "UTF-8", data);
    }
    private WebResourceResponse getCssWebResourceResponseFromAsset() {
    try {
    return getUtf8EncodedCssWebResourceResponse(getAssets().open("css/"));
   } catch (IOException e) {
    return null;
    }
   }
  }

我得到" css / "文件夹所有文件。它只获得单个文件只能得到多个 css 文件得到...

如何获取多个css文件获取webview拦截& " JS"如何获得...

2 个答案:

答案 0 :(得分:1)

mWebView.setWebViewClient(new WebViewClient() {

@Override
public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
    List myArrayList = new ArrayList();
    myArrayList.add("jquery.mobile-menu.css");
    myArrayList.add("jquery.validate.css");
    myArrayList.add("responsive.css");
    myArrayList.add("Style.css");
    for (Object str : myArrayList) {
        if (url.contains((CharSequence) str)) {
            try {
                return getUtf8EncodedCssWebResourceResponse(getAssets().open(String.valueOf("css/" + str)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    List newArray = new ArrayList();
    newArray.add("20px.png");newArray.add("30px.png");newArray.add("30px-01.png");newArray.add("30px-02.png");
    newArray.add("30px-03.png");newArray.add("30px-04.png");newArray.add("30px-05.png");newArray.add("30px-06.png");
    newArray.add("35px.png");newArray.add("arrow.png");newArray.add("arrow1.png");newArray.add("bank.png");
    newArray.add("cam.png");newArray.add("dex.png");newArray.add("footer-kk.png");newArray.add("logo1.png");
    newArray.add("load.png");newArray.add("logo2.png");newArray.add("logo2@2x.png");newArray.add("logo3.png");
    newArray.add("logo3@2x.png");newArray.add("logo-ico.png");newArray.add("logo-ico@2x.png");newArray.add("msf-ico.png");
    newArray.add("option-ico.png");newArray.add("option-ico1.png");newArray.add("profile-nav.png");newArray.add("profile-pic-1.png");
    newArray.add("profile-pic-2.png");newArray.add("reg-nav-act.png");newArray.add("reg-nav-bg.png");newArray.add("reg-nav-dot.png");
    newArray.add("shadow.png");newArray.add("tick.png");newArray.add("tick1.png");newArray.add("view-pro-line.png");
    for (Object image : newArray) {
        if (url.contains((CharSequence) image)) {
            try {
                return getimageWebResource(getAssets().open(String.valueOf("images/" + image)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    List jsArray = new ArrayList();
    jsArray.add("jquery.validate.js");
    jsArray.add("jquery-1.3.2.jpg");
    newArray.add("mc_reg.js");
    newArray.add("html5shiv.min.js");
    newArray.add("jquery.min.js");
    newArray.add("jquery.mobile-menu.js");
    newArray.add("jquery.mobile-menu.min.js");
    newArray.add("respond.min.js");
    for (Object js : jsArray) {
        if (url.contains((CharSequence) js)) {
            try {
                return getJsWebResourceResponse(getAssets().open(String.valueOf("js/" + js)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
private WebResourceResponse getUtf8EncodedCssWebResourceResponse(InputStream data) {
    return new WebResourceResponse("text/css", "UTF-8", data);
}
private WebResourceResponse getJsWebResourceResponse(InputStream data) {
    return new WebResourceResponse("text/javascript", "UTF-8", data);
}
private WebResourceResponse getimageWebResource(InputStream data) {
    return new WebResourceResponse("image/png", "UTF-8", data);
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
    super.onPageStarted(view, url, favicon);
    findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
}

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    mWebView.loadUrl("file:///android_asset/webpages.html");
}

@Override
public void onPageFinished(WebView view, String url) {
    findViewById(R.id.progressBar).setVisibility(View.GONE);
}

});

答案 1 :(得分:1)

谢谢泰米尔语,

根据你的回答我改进了这段代码。因此,您不必手动添加单个文件,但方法是从指定的文件夹中读取文件名。

        //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;
        }