1

I'm working with Android 5.0 and the last update of the WebView component. I try to embed a remote website (I can't modify this website source code) into a WebView.

My code below

webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains(URL_SERVLET)) {
            //Do nothing 
            Log.d("DEBUG", "load IMAGE");
            //view.stopLoading();
        } else {
            Log.d("DEBUG", "load URL into WebView");
            view.loadUrl(url);
        }
        return true;
    }
    // Called when all page resources loaded
    public void onPageFinished(WebView view, String url) {
        webView.setVisibility(View.VISIBLE);
        splashscreenLayout.setVisibility(View.GONE);
    }
});

On the website side, I get a button which open another url (using javascript) inside a popup. When I click on it via the Android Webview, this new URL replace the current one in the webview. I just want that nothing happened when I click on this button.

It works with this code: this url is not loaded. But the webview become white, like a blank screen.

I tried to call stopLoading(), doesn't do the trick.

I can't call webview.reload() because the remote website is written in jsp with one url which provide (generate on the fly I guess) html page. If I call reload(), the WebView is reinitialize to the first page (login page).

I also tried to save webview state and restore it, but it is not working.

Is there a way to "block" a changing of url and keep the webview pointing on the first url without reload?

Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50
cedric
  • 11
  • 2

1 Answers1

0

There's a somewhat hackish way to do this, which would be to write some javascript that prevents page navigation and inject it into your WebView.

The javascript would look like the answer posted here

var location = window.document.location;

var preventNavigation = function () {
    var originalHashValue = location.hash;

    window.setTimeout(function () {
        location.hash = 'preventNavigation' + ~~ (9999 * Math.random());
        location.hash = originalHashValue;
    }, 0);
};

window.addEventListener('beforeunload', preventNavigation, false);
window.addEventListener('unload', preventNavigation, false);

Note: An alternative and less intrusive way of doing this might be to have your javascript simply disable the specific button in question.

Then, to inject this into your WebView, you can follow the example posted here

WebView myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();

webSettings.setJavaScriptEnabled(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
myWebView.setWebViewClient(new WebViewClient() {
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
      return false;
   }

   @Override
   public void onPageFinished(WebView view, String url) {
      super.onPageFinished(view, url);

      injectScriptFile(view, "js/script.js"); // see below ...

      // test if the script was loaded
      view.loadUrl("javascript:setTimeout(test(), 500)");
   }

   private void injectScriptFile(WevView view, String scriptFile) {
      InputStream input;
      try {
         input = getAssets().open(scriptFile);
         byte[] buffer = new byte[input.available()];
         input.read(buffer);
         input.close();

         // String-ify the script byte-array using BASE64 encoding !!!
         String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
         view.loadUrl("javascript:(function() {" +
                      "var parent = document.getElementsByTagName('head').item(0);" +
                      "var script = document.createElement('script');" +
                      "script.type = 'text/javascript';" +
         // Tell the browser to BASE64-decode the string into your script !!!
                      "script.innerHTML = window.atob('" + encoded + "');" +
                      "parent.appendChild(script)" +
                      "})()");
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
});

myWebView.loadUrl("http://www.example.com");
Community
  • 1
  • 1
Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50