Search code examples
javaandroidiframewebviewx-frame-options

Android(4.4+) WebView ignore x-frame-options


I'm trying to show page with iframes in the app. it shows simple pages, but can't show googlemap iframe.

i've tried: hardvre accelleration - turned on (as i know it related only for video), java script - enabled, use web chrome client, loadurl (because of loading from internal memory), ect

the code is:

        wewview = new WebView(getActivity().getApplicationContext());
        wewview.getSettings().setJavaScriptEnabled(true);
        wewview.getSettings().setLoadWithOverviewMode(true);  
        wewview.getSettings().setUseWideViewPort(true);
        wewview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,  
                LayoutParams.MATCH_PARENT));
        wewview.setWebChromeClient(new WebChromeClient());
        if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
            wewview.getSettings().setAllowUniversalAccessFromFileURLs(true);
        }
        wewview.getSettings().setPluginState(WebSettings.PluginState.ON);
        wewview.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND); 

        linearMain.addView(wewview,0);  

        wewview.loadUrl("file://path... /frames.html");

could somebody advice how can i switch on "ignore x-frame-options" programmatically for my webview?

UPDATE 1

found another message in log

05-19 10:01:49.404: I/chromium(4027): [INFO:CONSOLE(0)] "Refused to display 'https://www.google.ru/maps/@?nogmmr=1' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.", source: about:blank (0)

looking for ignore x-frame-options


Solution

  • found the way. there is no full interaction on loaded frame, but i need only to display it. so, Jsoup is used to load page content, but keep loading scripts and images by standatr way, thats all the trick:

       private class ignoreXHeaderWebClient extends WebViewClient{  
       private static final String URL_MASK       =  "http";
    
    
        //api < 21
        @Override
        public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
            WebResourceResponse content = getsPageContent(url);
            if (content != null) {
                return content;
            } else {
                return super.shouldInterceptRequest(view, url);
            }
        }
    
       // api >= 21
        @Override  
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            WebResourceResponse content = getsPageContent(request.getUrl().toString());
            if (content != null) {
                return content;
            } else {
                return super.shouldInterceptRequest(view, request);  
            }
        }   
    
        private WebResourceResponse getsPageContent(String url){
            if (( url.contains(URL_MASK)) // guess is better to create for ex map to check all extensions
                    && !(( url.contains(".js")) 
                          ||( url.contains(".css"))
                          ||( url.contains(".tiff"))
                          ||( url.contains(".jpg"))
                          ||( url.contains(".png"))
                          ||( url.contains(".gif")))){    
                try {  
                    return new WebResourceResponse(
                                  "text/html",    
                                  "UTF-8",   
                                  new ByteArrayInputStream( 
                                       Jsoup.connect(url)
                                               .get()  
                                               .toString()
                                               .getBytes()));  
                } catch (IOException e) {
                    e.printStackTrace();    
                }   
            }
            return null;
        } 
    }
    

    and apply new class:

    webWiev.setWebViewClient(ignoreXHeaderWebClient);