简体   繁体   English

渲染完成后如何获取WebView HTML页面内容?

[英]How to get WebView HTML page content after complete rendering?

在此处输入图像描述

This topic was raised and discussed hundreds of times.这个话题被提出和讨论了数百次。 None of them correpond to the case I am facing.Please read below before answering.它们都不符合我面临的情况。请在回答前阅读以下内容。

I want to use WebView do get some info received from a server response and displayed in WebView but after embedded javascript execution.我想使用 WebView 从服务器响应中获取一些信息并显示在 WebView 中,但在嵌入 javascript 执行之后。 The wanted info can change in server at any time, purpose is to store this info locally on client side post requests to update it on on-demand basis.想要的信息可以随时在服务器中更改,目的是将此信息本地存储在客户端发布请求以按需更新。

I use WebChromeClient to determine when page is loaded (onProgressChanged = 100%) and JavaScriptInterface to get the HTML content, but I get the source code (with javascripts not executed yet).我使用 WebChromeClient 来确定何时加载页面 (onProgressChanged = 100%),并使用 JavaScriptInterface 来获取 HTML 内容,但我获得了源代码(尚未执行 javascript)。

The info I want to catch is available only after javascripts execution.我想要捕获的信息只有在 javascripts 执行后才可用。 Please see attached diagram, it may help to clarify my concern.请参阅附图,这可能有助于澄清我的担忧。

If someone was faced to similar issue I would appreciate to know how it was resolved.如果有人遇到类似的问题,我很想知道它是如何解决的。 Thanks谢谢

If you know when that JSON arrives on the page, then you can use WebView.evaluateJavascript and run javascript to extract the HTML.如果您知道 JSON 何时到达页面,则可以使用WebView.evaluateJavascript并运行 javascript 来提取 HTML。

According to https://stackoverflow.com/a/35917295/2921519 HTML extraction can be done by根据https://stackoverflow.com/a/35917295/2921519 HTML 提取可以通过

new XMLSerializer().serializeToString(document)

If you need the javascript to notify the Android side proactively when the page content changes, then you will need to add a javascript interface according to https://developer.android.com/guide/webapps/webview#BindingJavaScript如果需要javascript在页面内容变化时主动通知Android这边,那么就需要按照https://developer.android.com/guide/webapps/webview#BindingJavaScript添加一个javascript接口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM