Determine element of url from webview shouldOverRideUrlLoading - android

I am displaying a html in a custom activity with a webview. I am overriding
public boolean shouldOverrideUrlLoading(WebView view, String url) {
to intercept certain urls. Now I would like to be able to figure out some more details about the html around that url.
In my case I would like to get the html element that contains the url. E.g. if the url comes from
Manfred Moser
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.
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?

There is a dirty hack:
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");");
Described here: http://lexandera.com/2009/01/extracting-html-from-a-webview/
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.

In addition to #Peter Knego's approach, there is the inverse:
Use addJavascriptInterface(), per his step #1
For your link, use an onClick attribute on your <a> tag to call out some method on the Java object from step #1, where the Java object turns around and loads the URL into the WebView, and supplies whatever sort of identifying information you want.
Suffice it to say, there's no way to get the information you want from shouldOverrideUrlLoading().

Related

How to scroll Android WebView programmatically to a specific element

I load an HTML string directly into a WebView with loadDataWithBaseURL(null, html, null, null, null) and later I'd like to be able to scroll programmatically to an internal element, e.g. <p id="pos1"> or <a name="pos1"></a>
I found a similar question, where the answer was to use javascript:
https://stackoverflow.com/a/2239301/2534762
But is that really the only way? Isn't there another simple way to say directly to a WebView: "go to #pos1" without having to embed a script in every HTML page that you want to scroll? It seems to me a common task begging for a simple solution, but apparently I'm wrong... or is there a way to do that, maybe calling loadUrl or loadDataWithBaseURL in some particular way?
I found that I don't need to embed JavaScript in the HTML page, as suggested in that other answer, I can instead write one line of JavaScript directly in the URL:
webView.loadUrl("javascript:document.getElementById('"+aID+"').scrollIntoView()");

How to Modify content of resource response android webview

What I'm trying to do is to intercept the response data and modify the response from a particular URL.
For example, when loadUrl("http://foo.bar") is called to a WebView, let's say that it will load "page.html", "data.json", "someimage.png", and so on. In this case, how can I modify the contents of data.json when the resource is fetched, and pass the modified file to the WebView?
I think shouldInterceptRequest might be the answer, but I can't figure out how to get a WebResourceResponse from a WebResourceRequest.
I'm sorry if my English is awkward ;(

Android, Webview, get submitted form data

I want my Android application to use Webview to display an html form. When the user presses the Submit button, it want the name/value pairs sent back to my program. How do I do this?
I have looked at other similar questions but not of the responses tell how, specifically, to do this.
Look into the use of Javascript interfaces within WebView. Please see this area of the Android Developers Guide. Basically, you should be able to communicate from the page to the device with the Javascript interface and let your WebView Activity know when the submit button is pressed and send its values over.
I have found what I needed. If I set action='FORM" in the form html, then I can intercept the action with:
public void onLoadResource (WebView view, String url){
int index = url.indexOf("FORM?");
if (index != -1){
String d = URLDecoder.decode(url.substring(index+5));
}
}
The name/value pairs are in String d.
If you want to save Name/Value pairs and are willing to use jQuery then it's quite simple.
Assuming you put jQuery in your assets folder you can read it into a file from:
getResources().getAssets()
In the WebViewClient that you set (assuming you overrode the client) you can do:
view.loadUrl("javascript:" + escapedJqueryStringHere);
Then, make sure you have a JavascriptInterface configured for your webview with a method, for example, called 'ReturnJSONFromForm(String json)' configured for "MyJInterface" then load the form into the page and then try this:
webview.loadUrl("javascript:MyJInterface.ReturnJSONFromForm($(\"form\").serialize());");
Now in your interface, just create a JSONObject from the resultant string and there you go! Name/Value pairs from your form.
Serialize won't capture disabled fields or submit buttons.. if you need that, then a more complex javascript solution exists but it's far more difficult.

Read values from a webview in android

I have a webview in a alertbox, in this webview I have a <textarea>
Anyone have an idea how to get the texts typed in this field and be used in the android app. as normal Edittext fields
Happy Coding..!
Its not possible as you are saying. An alternative can be helpful. You should add an interface to your WebView which contains a method which can get value from web page. You have create and to call a javascript function which can pass the value of your textarea to your interface function by using that Interface. I hope you understand.
For example: Add an interface to your Webview like:
mainView.addJavascriptInterface(MyOwnCreatedClassName, "AndroidInterface");
Here MyOwnCreatedClassName is the name of the class which will contain that method and AndroidInterface is the name that the Javascript will use to call that method.
for complete example follow:
Using webView.AddJavascriptInterface with MonoDroid

Call a webview inside another webview in android?

I have a custom html(suppose a.html) file which i am using from the resources and thus building a string with the entire document which is then passed to the WebView.
There's some text in a.html on click of which i want load another file b.html in the webview itself.
Please let me know what is the apporach to be followed here.
Step #1: Implement a WebViewClient, overriding shouldOverrideURLLoading().
Step #2: In shouldOverrideURLLoading(), load what you want -- this will be called on link clicks and redirects, and you are supplied the URL that ordinarily would be loaded.
I think if you set a WebViewClient and override shouldOverrideUrlLoading() then you can have the link loaded into the current WebView by returning false.

Categories

Resources