Get data from text box of a website using WebView - android

In my application, I am showing our own e-commerce website in WebView. In that, I have username and password textboxes. I want to get username of text data while he is entering and want to store it in my file. I do not have any control of website(Login page). It is completely build by third party How can I achieve this ? please help.
Thank you in advance.

You can use JavaScript for load data from the webView.
I use following JS code for load html from the webView
JS_PARSER = "(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();"
webview.getSettings().setJavaScriptEnabled(true);
webView.evaluateJavascript(JS_PARSER, <callback for handle response with html>);
evaluateJavascript docs
And try to load data from the html.
As second way you can make JS function for load text from the textBoxs. And execute the function for each textBox.
JS function for load data from textBox:
function myFunction(){
return document.getElementById("f6").value;
}
Android code for second way:
String LOAD_FUN = function getValue(){ return document.getElementById('%s').value; }();
webView.evaluateJavascript(String.format(LOAD_FUN, "<String with textBoxId>"), <callback for handle textBox value>)

Related

is it possible to save/retrieve specific data from webview cache?

i load a web page in a webview that contains a form, i want to know if there is a way to save the data that user put in the form in the webview cache and auto fill it in his next visit?
this is all i have done for now.
webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
not sure if it's the right way but, what about running js script on webview to get form input values, navigating DOM tree of html or using selector with id ?
once you got them you can store them in your app and for repopulate, before loading the page in webview, you manipulate html (if you could) or you use another js script to set the values
i'm not sure but i thing cache is only for cachable values (images, css...) not for input

Android Webview goback() issue with loadDataWithBaseURL method

Issue is goback() not showing my html data back. Steps to produce this issue is like
Loaded html data using method loadDataWithBaseURL. It renders html data fine.
Then click on one link inside html data then webview moves to next page showing that link which is also fine.
When I call method goback() from this page it should show my html data but it is showing me blank screen. Inside onPageFinished() I am getting url as about:blank.
Thanks in advance!
If you use loadDataWithBaseURL you will need to send in the url parameter something different from null, if you send null the url will always be "about:blank"
Example:
var page = new RazorView().GenerateString();
webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", "");
var url = webView.CopyBackForwardList().GetItemAtIndex(1).Url;
//url will get the Html From Previous Page
Notice im using C# but it should be the same with java, except for the CapitalizedWords

How to clear html page before showing into a webview in Android?

I have the URL of a webpage to be displayed into a webview in my Android app. Before showing this page i want to clear the html code of this page from some tag (such as the header, footer, ecc..) in order to show only few information. How can i do it? I tried to solve the issue working with JSoup but i can't understand how to create and pass the "new page" to the webview. Anybody can help me?
EDIT
I cleaned the html code useless through jsoup libraries. Then, always by mean of these, i get head and body content and finally i showing the "cleared" web page through these lines:
headURL = doc.select("head").outerHtml();
bodyURL = doc.select("body").outerHtml();
webview.loadData( "<html>"+headURL+bodyURL+"</html>" , "text/html", "charset=UTF-8");
webview.setWebViewClient(new DisPlayWebPageActivityClient());
The view shows the new page but do not load css files specified in the head(that has not been touched). Who can say me why?
You can fetch the WebPage you want to display as a string, parse and remove whatever you don't want and then load this string as data in your webview.
Something like:
String webContent = fetchPage(url);
String cleanedWebContent = cleanUp(webContent);
webView.loadData(cleanedWebContent, "text/html", "UTF-8");
Of course, you will need to implement fetchPage and cleanUp as they are not Android methods

How to handle callbacks in webview

I am working on an android project right now and have a question about how to do callbacks in different webviews. I used JSInterface for my project too. Here I have 2 webviews. One has an index page, anther is a overlay(still a html page though.) What I want to do is if any user clicks on some links on the overlay, it should fire a callback function which is written in the java file where the index page was connected to through JSInterface. It might sound confusing, but I have draw something to help make it clear!
Thanks!
You can use a custom URL scheme like myurl://function for your functionality links. Then write an event handler for the WebView's shouldOverrideUrlLoading event in which you decide how to process the URL: either instruct the webview to load it, or do some custom action.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.startsWith("myurl://"))
{
// Parse further to extract function and do custom action
}
else
{
// Load the page via the webview
view.loadUrl(url);
}
return true;
}
I used startsWith to check the URL for this quick and dirty example, but you should consider using android.net.Uri.parse for parsing URLs.
This should allow you to call the Java function foo() without having to go through the first WebView.
If you want to go through the first webview, then you can call a function on the JSInterface like this (where webView1 is the first WebView retrieved through findViewById):
webView1.loadUrl("javascript:myjsinterface.myjsfunc();")

Launch a background web search

I am trying to launch a websearch using data input from a user. The data is input through TextEdit boxes. Upon submission of the data, i would like my program to: 1) search for a specific webpage based on the user input 2)Find specific elements at the webpage 3) Display the webpage.
Here is an example:
User Input (in a non browser/webview page)
1) Store Name: Macey's 2)Zip Code: 77471
In the background my program will:
1) Find the Macey's website
2) Find the store nearest zip code 77471
3) Load the Web page for the store nearest zip code 77471
Obviously there is a lot of error handeling, exceptions, ect that would go along with this. For the sake of making this example "easy" lets pretend that 1) A the Macey's main page exists 2)A sperate page for the 77471 store exists. 3)There is a link to the 77471 store on the Macey's main page.
I have the code for getting the user input variables and i know how to launch the webview. What i dont know how to do is to search for the Macy's home page, then find the link i am looking for on the homepage and navigate to it. Loading the webview is not the problem. Find the data is.
Below is my current code. Right now i am setup so that the user will navigate to the webpage they are looking for but i would rather handle the searching for them, if it is possible.
public void InitializeWebView(){
portal = (WebView)findViewById(R.id.web_Portal);
WebSettings Settings = portal.getSettings();
Settings.setSavePassword(false);
Settings.setSaveFormData(false);
Settings.setJavaScriptEnabled(true);
Settings.setSupportZoom(true);
Settings.supportZoom();
portal.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
public void searchAndShow(String Store, String zip){
portal.loadUrl("http://www.google.com");
}
You can get search result in JSON format from google using their API. Here is a nice example in JAVA. Just don't use key parameter until you do not have a vlid key.

Categories

Resources