Android javascript injection, how would I call this function $(document).ready(function(){...}) - java

I am working on some javascript injection into m android webview. I am just not completely sure of the syntax it is looking for
I want to call this function in my html page:
$(document).ready(function({...})
I have a webview and javascript interface setup in my app. I also have javascript enabled
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/mydocument.html");
myWebView.addJavascriptInterface(new myJavaScriptInterface(), "jsintector");
myWebView.loadUrl("javascript:ready()");
the javascript:ready() is supposed to be the part in the android that calls the correct function in the html page. What is the proper syntax to access this function? $(document).ready(function({...})
(edit: the javascript is already in the html, I just want to force it to run from the android side)

Try to search before...
Anyway, the simplest way to do this is to do the following:
load the url into a string
inject the javascript into the string
use loaddata to load the html into the webview

Related

Call JSP function with JSoup

I'm trying to scrape a .jsp webpage with Jsoup. The page I'm trying to scrape is basically a 6 steps form, filling each form takes me to another one but to do so I must click a button that calls a jsp function. I don't know how to do so with jsoup.
So here is a piece of the jsp page I'm trying to scrape.
<img id="nextButtonId" onkeypress="disableButtons(this);onIntroductionFormSubmit()" height="50"
alt="suivant" title="suivant" src="/eAppointment54-etrangers/element/images/buttons/next.gif"
**onclick="disableButtons(this);onIntroductionFormSubmit()"** onmouseover="downNextSrcPicture(this);"
onmouseout="upNextSrcPicture(this);" style="display: block;">
I want to call the onclick methods onclick="disableButtons(this);onIntroductionFormSubmit(). Maybe there is a way I can do this without having to simulate the actual clicking.
Thanks in advance !!!
Jsoup is not a browser engine, it is just an HTML parser/writer. To do such a thing you should do one of these, I recommend the first one:
Implement the method yourself (it's probably just an HTTP call)
Use an automation library like Selenium (no experience)
(Not recommended) Run a full browser environment such as JavaFX WebView and inject code to do the action.

Call HTML page with javascript content

I am stuck at an issue, I don't know if its possible or not.
I need to call a HTML page, but the page has some content being loaded via Javascript too and I need to get that also.
Is this some how possible ?
Currently I have used plain: new URL(url).openConnection() and it's not returning that.
Any Help ?
Thanks
To do that you'll need to host a WebView in your application. It can be hidden if you want. Call the webview loadUrl to load the content you want. Handle the WebViewClient onPageFinished so you know all of the content has loaded, and then use the javascript bridge interface (addJavascriptInterface) to pass the HTML back up to the application.

javascript onclick in html confusion

I was going through some java code(which uses playframework) that provides some shopping cart functionality.The html page contains some javascript (it uses jquery) which is something I have very little knowledge of.Looking through the html,I found a table like this
<table>
...
<td>
buy
</td>
...
</table>
(The template uses ${} to get dynamic values thru playframework template language)
In the entire code ,the only addItem(itemId) method I could find was in the ShoppingCart class which has this signature
public void addItem(Item item) {
...
}
When I searched for onclick ,I found this page
onclick doc,which gives the syntax of onclick as
onclick="SomeJavaScriptCode"
Going thru all the javascript code(it uses jquery and ejohn-templating)I couldn't find any method like 'addItem'
So,how does the onclick work?How would it know which method to invoke?Will it look in the entire code (javascript and java)to find a matching method?
somewhere you have an addItem method in your javascript code. The code in the html is executing that method, not the addItem method you found in the java code.
First is it would only look for javascript methods and not java methods. Second is you can use debugging in firebug to reach to the method called.
It is relatively simple.
It looks like your Java code is executed on the server side, and the JavaScript (including jQuery) is executed on the client side. So the ${item.id} is executed on the server, and onClick executes whatever javascript code you provide for it on the client side.
I do not know playframework, but the addItem should be javascript, and public void addItem(Item item) is java code. Both are not related
This addItem javascript function is somewhere. If you are unable to find it in your code base, you can use the console of your browser (or firebug on Firefox) to see this javascript function. Go to the Script tab and search for addItem.
Javascript can't access server side functions unless is via AJAX or something like it. Even then, it doesn't have direct access to the funcions, but to a page that access that.
Look more carefully for that function in your Javascript using firebug, or the google chrome console. It should be there.

Access Javascript from Java

I m trying to access JavaScript function from Servlet code. But I'm getting the error shown below.
Here is the code:
out.println("<FRAME src=\"javascript:parent.newWindow('" + URL+ "') \" scrolling=No noresize />");
And this is the error that occurs in JavaScript:
Object does not support this property or method;
You can't access a Javascript function from your servlet code. Javascript executes on the client (= your user's browser) and the servlet code executes on your server (for example Tomcat, JBoss, whatever you're using).
What are you trying to accomplish with your code? I'm sure there's a simpler way to do it than what you just described.
[edited]
I see you just updated your description, so here's my view:
I'm guessing that you want to display a page to the user and when the page is displayed, you want to open a new window which will display another page using the URL parameter to point its address. If this is the case, you should probably just do this in the first page's onLoad() Javascript event using window.open().
There is no newWindow property on a window object (which is what parent references), so this is not unexpected.
Maybe you are looking for the open method instead?
If so, then:
Putting it as the src of an iframe is a very strange thing to do
It will probably be zapped by pop-up blockers
Ok. You try to generate javascript code inside Servlet code. When you do, your code goes to Web browser and it's seen there as a html document with javascript inside. So, your error rather comes from web browser and links to javascript error. Probably it's newWindow method. To open new window you should call window.open() function, I guess.

How to call a javascript function and get the html code it returns ,Java,Android

There is a HTML page that has a javascript function in it.This function returns a frame with a random picture.
Is there a way to call this function and get the HTML code that is hidding? (So i get the image)
Exactly i want to get a html stream from GoogleServices.js
by calling:
GS_googleAddAdSenseService("ca-pub-YOU
RPUBIDHERE");
GS_googleEnableAllServices();
GA_googleAddSlot("ca-pub-YOURPUBIDHERE", "ADSLOT_NAME_HERE");
GA_googleFetchAds();
GA_googleFillSlot("ADSLOT_NAME_HERE");
An example of how Java and JavaScript can call each other using a WebView in Android can be found here:
http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/src/com/google/android/webviewdemo/WebViewDemo.java

Categories