从web视图确定网址的元素shouldOverRideUrlLoading视图、元素、网址、web

2023-09-03 21:02:21 作者:血液不再循环

我在显示用的WebView自定义活动一个HTML。我重写

I am displaying a html in a custom activity with a webview. I am overriding

public boolean shouldOverrideUrlLoading(WebView view, String url) {

拦截某些URL。现在,我希望能够找出有关围绕该URL的HTML一些更多的细节。

to intercept certain urls. Now I would like to be able to figure out some more details about the html around that url.

在我来说,我想获得一个包含URL的HTML元素。例如。如果URL来自

In my case I would like to get the html element that contains the url. E.g. if the url comes from

<a href="http://stackoverflow.com/users/136445/manfred-moser">Manfred Moser</a>

我希望能够以某种方式从锚标记中检索值曼弗雷德·莫泽。有没有办法做到这一点。我发现,你没有访问来自Java中的DOM。

I would like to be able to somehow retrieve the value "Manfred Moser" from within the anchor tag. Is there a way to do that. I found that you do not have access to the DOM from Java.

我能想到的一个办法是单独下载页面,甚至在装入的WebView之前解析这一切。但是这是一个丑陋的黑客攻击的最好的。有没有更好的办法?

One way I can think of would be to download the page separately and parse it all before even loading it in webview. However that is an ugly hack at best. Is there a better way?

推荐答案

有一个肮脏的黑客:

绑定一些Java对象,以便它可以被称为从Javascript使用的WebView:

Bind some Java object so that it can be called from Javascript with WebView:

addJavascriptInterface(javaObjectExposed, "JSname")

强制通过

Force execute javascript within an existing page by

WebView.loadUrl("javascript:window.JSname.passData("some data");"); 

描述如下: http://lexandera.com/2009/ 01 /解压-HTML-从-A-的WebView /

注:这是一个安全风险 - 在本网页的任何JS code可以访问/致电您绑定的Java对象。最好通过一些一次性cookie来使用loadURL(),并将它们传回Java对象,以检查它是否是你的code进行调用。

Note: this is a security risk - any JS code in this web page could access/call your binded Java object. Best to pass some one-time cookies to loadUrl() and pass them back your Java object to check that it's your code making the call.

 
精彩推荐
图片推荐