机器人的WebView" location.replace"不工作机器人、工作、WebView、QUOT

2023-09-08 00:17:52 作者:不需要脆弱的伪装

我有一个Android的WebView与页面重定向到另一个网页,使用 location.replace(URL)。 比方说,我有A页重定向到页面B(使用location.replace)。当pressing回从网页B按钮,页面返回页面A,会被重定向到页面B了。 当我调试的历史API(history.length),我可以清楚地看到,页面的B的长度为1(仅在Android 4.X的WebView已经增加。在iOS /网页浏览器/安卓2.X它保持不变),这是一个错误! (location.replace不应该改变history.lenght!)

I have an Android webview with a page that redirects to another page, using location.replace(url). Lets say that I have page "A" that redirects to page "B" (using location.replace). When pressing "back" button from page "B", the page returns to page "A", which redirects it to page "B" again. When I debug the history api (history.length), I can clearly see that on page "B" the length has incremented in "1" (only on Android 4.X webview. On iOS / web browser / Android 2.X it remains the same), which is a bug! (location.replace shouldn't change history.lenght!)

推荐答案

我用的Yaniv工作在这个项目上,我们找到了问题的原因,据介绍,当我们试图添加的mailto:链接处理,根据这个答案。

I work with Yaniv on this project and we found the cause of the problem, it was introduced when we tried to add mailto: links handling according to this answer.

使用下面的扩展类WebViewClient的回答提示:

The answer suggested using the following extending class of WebViewClient:

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {     
        if(MailTo.isMailTo(url)){
            MailTo mt = MailTo.parse(url);
            // build intent and start new activity
            return true;
        }
        else {
            view.loadUrl(url);
            return true;
        }
    }
}

问题是,明确地告诉 WebViewClient 要加载的URL,并返回true(意思是我们处理这件),增加了页面的历史记录。 WebViews完全有能力处理普通的URL本身,所以返回虚假和不接触视图实例将让的WebView加载网页和处理它,因为它应该。

The problem was that explicitly telling the WebViewClient to load the URL and returning true (meaning "we handled this") added the page to the history. WebViews are quite capable of handling regular URLs by themselves, so returning false and not touching the view instance will let the WebView load the page and handle it as it should.

所以:

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {     
        if(MailTo.isMailTo(url)){
            MailTo mt = MailTo.parse(url);
            // build intent and start new activity
            return true;
        }
        else {
            return false;
        }
    }
}