javascript 如何确定Android WebView何时完全加载完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5587888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:41:42  来源:igfitidea点击:

How to determine when Android WebView is completely done loading?

javascriptandroidwebview

提问by Carlos Rendon

I want to know how to determine when a WebView is completelydone loading.

我想知道如何确定 WebView 何时完全加载完成

Let me define completely.

让我完全定义。

  • All redirects have happened
  • The page is visibly displayed, nothing has yet to load
  • 所有重定向都发生了
  • 页面明显显示,尚未加载任何内容

Bad answers to this question:

这个问题的答案不好:

WebViewClient.onPageFinished()- this fires multiple times if there are redirects loading a page. It also fires before the page is displayed.

WebViewClient.onPageFinished()- 如果有重定向加载页面,这会触发多次。它也会在页面显示之前触发。

PictureListener.onNewPicture()- this fires every time the screen changes and AFAICT each redirect. How would I know which firing is the final one?

PictureListener.onNewPicture()- 每次屏幕更改和 AFAICT 每次重定向时都会触发。我怎么知道哪一次是最后一次开火?

WebChromeClient.onProgressChanged()- same problem as onPageFinished. (Added after David suggested this solution below)

WebChromeClient.onProgressChanged()- 与 onPageFinished 相同的问题。(在 David 在下面建议此解决方案之后添加)

Thread.sleep(5000)- should be obvious, why this isn't acceptable.

Thread.sleep(5000)- 应该很明显,为什么这是不可接受的。

Of course, a clever combination of the above that works would be perfect.

当然,上述有效的巧妙组合将是完美的。

Why do I want to know this you ask? Because I want to inject some javascript one timeonce the webview is completelydone loading.

为什么我想知道你问这个?因为我想注入一些JavaScript一次一次的WebView是完全完成加载。

采纳答案by Carlos Rendon

Thanks to David Caunt for his suggestion to look at the response headers.

感谢 David Caunt 建议查看响应标头。

I've found the following to work pretty well:

我发现以下工作非常好:

Instead of having webview perform the HTTP requests, do them yourself so you can check the HTTP status code for redirects. Follow the redirection chain. When you get to a request that is not a redirect set a flag and send the data to the webview via webview.loadDataWithBaseURL. The flag is checked in WebViewClient.onPageFinished()so it knows that all redirects are done. The javascript can be injected at this point. Of course the page itself might be doing some javascript manipulation at this point, it is difficult to know, so adding a delay (say 500ms) to the javascript allows for the DOM to settle. setTimeout(function(){/* your code */}, 500).

与其让 webview 执行 HTTP 请求,不如自己执行,这样您就可以检查 HTTP 状态代码以进行重定向。遵循重定向链。当您收到一个不是重定向的请求时,设置一个标志并通过webview.loadDataWithBaseURL. 该标志已签入, WebViewClient.onPageFinished()因此它知道所有重定向都已完成。此时可以注入 javascript。当然,此时页面本身可能正在执行一些 javascript 操作,这很难知道,因此向 javascript 添加延迟(比如 500 毫秒)可以让 DOM 稳定下来。setTimeout(function(){/* your code */}, 500).

回答by David Snabel-Caunt

Have you looked at setting your own WebChromeClient?

您是否考虑过设置自己的 WebChromeClient?

It looks like onProgressUpdatedmight inform you when a page has fully loaded.

看起来onProgressUpdated可能会在页面完全加载时通知您。

This appears to be the method used by Android's built-in browser.

这似乎是Android内置浏览器使用的方法。