gre*_*egm 81 android android-webview
在Android上,我有一个WebView显示页面.
如何在不请求页面的情况下获取页面源?
似乎WebView应该有某种getPageSource()方法返回一个字符串,但唉它没有.
如果我启用JavaScript,那么在此调用中放入适当的JavaScript以获取内容是什么?
webview.loadUrl("javascript:(function() { " +
"document.getElementsByTagName('body')[0].style.color = 'red'; " +
"})()");
Run Code Online (Sandbox Code Playgroud)
jlu*_*yiv 151
我知道这是一个迟到的答案,但我发现了这个问题,因为我遇到了同样的问题.我想我在lexandera.com上的这篇文章中找到了答案.下面的代码基本上是来自网站的剪切和粘贴.它似乎做了伎俩.
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");
Run Code Online (Sandbox Code Playgroud)
dur*_*a42 32
在第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");
Run Code Online (Sandbox Code Playgroud)
nag*_*ya0 17
这是基于jluckyiv的答案,但我认为更改Javascript更好更简单如下.
browser.loadUrl("javascript:HTMLOUT.processHTML(document.documentElement.outerHTML);");
Run Code Online (Sandbox Code Playgroud)
您是否考虑过单独获取HTML,然后将其加载到webview中?
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
103369 次 |
| 最近记录: |