在Android上,我有一个显示页面的WebView
。
如何在不再请求页面的情况下获取页面源?
似乎WebView
应该有某种返回字符串的getPageSource()
方法,但是它没有。
如果我启用了JavaScript,那么在此调用中输入内容的适当JavaScript是什么?
webview.loadUrl("javascript:(function() { " +
"document.getElementsByTagName('body')[0].style.color = 'red'; " +
"})()");
答案 0 :(得分:146)
我知道这是一个迟到的答案,但我发现了这个问题,因为我遇到了同样的问题。我想我在lexandera.com上的this post找到了答案。下面的代码基本上是来自网站的剪切和粘贴。它似乎可以解决问题。
final Context myApp = this;
/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(String html)
{
// process the html as needed by the app
}
}
final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);
/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
browser.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
/* load a web page */
browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html");
答案 1 :(得分:32)
Per issue 12987,Blundell的回答崩溃了(至少在我的2.3 VM上)。相反,我使用特殊前缀拦截对console.log的调用:
// intercept calls to console.log
web.setWebChromeClient(new WebChromeClient() {
public boolean onConsoleMessage(ConsoleMessage cmsg)
{
// check secret prefix
if (cmsg.message().startsWith("MAGIC"))
{
String msg = cmsg.message().substring(5); // strip off prefix
/* process HTML */
return true;
}
return false;
}
});
// inject the JavaScript on page load
web.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String address)
{
// have the page spill its guts, with a secret prefix
view.loadUrl("javascript:console.log('MAGIC'+document.getElementsByTagName('html')[0].innerHTML);");
}
});
web.loadUrl("http://www.google.com");
答案 2 :(得分:17)
这是基于jluckyiv's的答案, 但我认为更改Javascript更好,更简单如下。
browser.loadUrl("javascript:HTMLOUT.processHTML(document.documentElement.outerHTML);");
答案 3 :(得分:6)
您是否考虑过单独提取HTML,然后将其加载到网页视图中?
String fetchContent(WebView view, String url) throws IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = httpClient.execute(get);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity); // assume html for simplicity
view.loadDataWithBaseURL(url, html, "text/html", "utf-8", url); // todo: get mime, charset from entity
if (statusCode != 200) {
// handle fail
}
return html;
}
答案 4 :(得分:4)
我设法使用来自@ jluckyiv的答案来解决这个问题,但是我必须在MyJavaScriptInterface中的processHTML方法中添加@JavascriptInterface注释。
class MyJavaScriptInterface
{
@SuppressWarnings("unused")
@JavascriptInterface
public void processHTML(String html)
{
// process the html as needed by the app
}
}
答案 5 :(得分:1)
如果您的targetSdkVersion为&gt; = 17,您还需要使用@JavascriptInterface注释该方法 - 因为SDK 17中存在新的安全要求,即所有javascript方法都必须使用@JavascriptInterface注释。否则你会看到如下错误:Uncaught TypeError:Object [object Object]在null时没有方法'processHTML':1
答案 6 :(得分:0)
如果您正在使用kitkat及以上版本,则可以使用chrome远程调试工具查找进出Webview的所有请求和响应,以及查看页面的html源代码。