Tag Archives: window.history.go()

Android WebView monitors and rewrites window.history.go (- 1) method call


import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by  on 2019/2/22.
 * desc:Make the webview support window.history.go()
 * Replace the history.go() method in js with webview.goBack
 */
public class WebViewGoBackSupport {

    /**
     * Custom, links loaded when history.go is called by js to listen for history.go method calls
     */
    private final static String CONSTANTS_GO_BACK = "/CONSTANTS_GO_BACK#";
    /**
     * Methods to call back when the webview fails to return
     */
    @Nullable
    private Runnable whenCannotGoBackRunnable;
    @NonNull
    private WebView webview;

    /**
     * @param whenCannotGoBackRunnable Methods to call back when there is no previous page and history.go is called by js
     */
    @SuppressLint("SetJavaScriptEnabled")
    public WebViewGoBackSupport(@NonNull WebView webview, @Nullable Runnable whenCannotGoBackRunnable) {
        this.webview = webview;
        this.whenCannotGoBackRunnable = whenCannotGoBackRunnable;
        this.webview.getSettings().setJavaScriptEnabled(true);
    }

    /**
     * @param whenCannotGoBackRunnable Method to call back when there is no previous page and js calls history.go
     * @param defaultClient when true, no need to call it manually onPageFinished、shouldOverrideUrlLoading
     */
    public WebViewGoBackSupport(@NonNull WebView webview, @Nullable Runnable whenCannotGoBackRunnable, boolean defaultClient) {
        this(webview, whenCannotGoBackRunnable);
        if (defaultClient) {
            webview.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    WebViewGoBackSupport.this.onPageFinished(url);
                    super.onPageFinished(view, url);
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    WebViewGoBackSupport.this.shouldOverrideUrlLoading(url);
                    return super.shouldOverrideUrlLoading(view, url);
                }
            });
        }
    }

    /**
     * Called in the corresponding method of WebViewClient
     * Note: This method needs to be called manually, or use defaultClient == true
     */
    public void onPageFinished(String url) {
        //Rewrite js page window.history.go method
        String script = "javascript:(function(){window.history.go = function(index){" +
                "window.location.href='" + CONSTANTS_GO_BACK + "'+index;" +
                "}})()";
        webview.loadUrl(script);
    }

    /**
     * Called in the corresponding method of WebViewClient
     * Note: This method needs to be called manually, or use defaultClient == true
     */
    public void shouldOverrideUrlLoading(String url) {
        if (!TextUtils.isEmpty(url) && url.contains(CONSTANTS_GO_BACK)) {
            int step = -1;
            try {
                //Parsing the parameters passed in when js calls history.go
                step = Integer.parseInt(url.split("#")[1]);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (webview.canGoBackOrForward(step)) {
                webview.goBackOrForward(step);
            } else if (whenCannotGoBackRunnable != null) {
                //Callback method when there is no previous page and history.go is called by js
                whenCannotGoBackRunnable.run();
            }
        }
    }
}