How to capture JS varible value sin a different java program - java

I have a JS program handling the communication from iframe to its parent. I am logging the different states on browser window (Inspect element's) console. How could i fetch these state values into my java program for validating/testing. The values are passed from JS in html file. Or if anyone can suggest any other way to validate the communication between parent and iframe.
Any suggestion or guidance will be helpful to move forward, I am stuck.
Thanks alot!!

Browser make http requests - so try implementing a basic http server in your java program and have the browser make ajax calls to the program.

Related

Calling a JavasScript function on a remote server from inside my JavaCode

There's a butt on a website that uses the onClick javascript function to run a JS function called HiddenUsers.show() or HiddenUsers.hide()
In my Java code, I've established an connection to this site using the HttpURLConnection class. I'm able to post login credentials with a BufferedOutputStream and I can get responses with an InputStream. But is it possible to send the command for that makes that button respond as clicked in turn triggering the JS function on the server side, or even better call the JS function itself from within my Java code?
is it possible to send the command for that makes that button respond as clicked in turn triggering the JS function on the server side
Perhaps I'm not fully following, but Javascript runs on the client (eg in the Browser), not on the server. So it doesn't make much sense to send javascript code to a webpage through HttpURLConnection. You can however, look through the webpage source and scripts and figure out what happens when the button is pressed and respond. For instance, if the button is a submit button you can use a HttpURLConnection to send get/post data from the form data associated with that button.
or even better call the JS function itself from within my Java code
You can call javascript code through a javascript ScriptEngine contained with java, but javascript calls that change the DOM of a webpage would be out of context.
There is a junvenile joke in your first sentence.
Outside of that, no, this isn't really possible. I assume you're trying to automate gets and posts to another server from java. It will receive the javascript, HTML etc... but it will not execute the javascript unless you go out of your way to get a javascript engine to execute it, and then what good will it do you to "show" something?
I'm assuming your angle is to acquire the users that are "hidden". The users may already be in the HTML markup somewhere, in which case you'll just want to parse the HTML you get from your response and get them. It is also possible that the ".show()" function gets the user list via an ajax call. If that is the case your best bet will be to figure out what that ajax call is, and process it directly with another HttpURLConnection.

scrape website multiple pages using Web Client java

I am trying to scrape a website, using Web Client, i am able to get the data on the first page and parse it, but I do not know how to read the data on the second page, the website is calling a java script to navigate to the second page. Can anyone suggest me how do I get the data from the next pages?
Thanks in advance
The problem you're going to have is while you (a person) can read the JavaScript in the first page and see it is navigating to another page, having the computer do this is going to be hard.
If you could identify the block of code performing the navigation, you would then need to execute it in such a way that allowed your program to extract the URL. This again is going to be very specific to the structure of the JavaScript and would require a person to identify this.
In short, I think you're dead in the water with this one, though it serves as a good example of why the Unobtrusive JavaScript concept is so important.
This framework integrates HtmlUnit with its headless javascript enabled browser to fully support scriping multiple pages in the same WebClient session: https://github.com/subes/invesdwin-webproxy

Back button HTML-page access with JAVA

I want to activate the back button of the HTML-browser. with a JAVA command not Javascript.
does anyone know how?
Assuming you are talking about an applet: I assume you would have to use the netscape.javascript packages to hit the JS API for it.
If you are talking about server side Java, then you have no way to trigger the back functionality. The closest you could come would be to read the referer (warning: Optional! Sometimes forged! Make sure it is a URI on your domain!) and issue a Location header to redirect forwards to it.
You can't. Java runs on the server. JavaScript runs on the client. You could have a JSP output JavaScript which is then run at client time in the browser.

Calling JavaScript methods from Java

I have a webpage that has JavaScript in it. The script contains a method that updates the webpage. I also have a java UDP server. When I get some parameters from a client, I want to call the method in the javascript to update the page.
Is it possible to call methods in Javascript from Java source code? Any pointers?
Thanks!
EDIT: For Ajax, the "request" initiates from the webpage. I want something that can change the webpage by itself - without this request.
A more succinct question would be: Can I dynamically update a webpage from java source file?
In order to reading javascript result you need a browser runtime AFAIK (You cannot get javascript result through a raw socket). You have to include a browser (JTextPane should be able to do it) into your udp server.
DWR is the answer, but it seems dead with no progress for some months. I don't think so you can directly call JavaScript methods from Java without passing an Asynchronous call using Ajax.
I have no idea on how you would dynamically change your content of webpage without an request being passed.
This is what we wanted to do:
-Send co-ordinates from an android phone to a server
-Plot these on a map in a browser window
The complexity was - One 'box' was server for android, and client for google maps. And we needed some glue between these two functionalities.
We initially tried ActiveMQ but could not get it to work.
Due to time constraints, we were forced to explore other approaches... our end result isn't elegant, but it works.
We have a FIFO on the server to which the co-ordinates are written. On the same server, our map page is also hosted. On a button press, XMLHttpRequest is sent to the server. In response, a co-ordinate is dequeued and sent back, which is plotted on the map using google maps api.
I will be happy to share more details/answer questions...

How can I open a webpage within a Java app and run my own javascript code

I would like to open a webpage and run a javascript code from within a java app.
For example I would like to open the page www.mytestpage.com and run the following javascript code:
document.getElementById("txtEmail").value="test#hotmail.com";
submit();
void(0);
This works in a browser...how can I do it programatically within a java app?
Thanks!
You can use Rhino to execute JavaScript but you won't have a DOM available - i.e. document.getElementById() would work.
You can use HTML Unit (headless) or WebDriver/Selenium (Driving a browser) to execute JavaScript in an environment that has a DOM available.
I'm not sure what you are looking for but I assume that you want to write automated POST request. This can be done in with Http Client library. Only you have to set appropriate request (POST or GET) parameters.
Look at examples - with this library you can do basic authentication or post files too.
Your question is a bit ambiguous, as we don't know the position of the Java program.
If that's a Java applet inside your page, you should look at Java<->JavaScript interaction, it works well.
If you need a separate Java program to control a browser, like sending a bookmarklet in the address bar (as one of your tags suggests), it is a bit harder (depends on target browser), perhaps look at the Robot class.
There's Rhino JS engine written in Java that you can run on app server such as Tomcat and feed JS to, however - it's not clear what are you trying to do with this?
There's also Envjs simulated browser environment which is based on Rhino but complete enough to run jQuery and/or Prototype
DWR (and other frameworks) now support "reverse ajax." The general idea is that you use one of three methods to communicate back to the client:
Comet (long-lived https session)
Polling
opportunistic / piggy-back (i.e. next time a request comes from the client, append your js call)
Regardless of method (which is typically a configuration-time decision and not a coding issue), you will have full access to any/all js calls you want to make.
Check out the reference page from DWR to get a pretty good explanation.

Categories

Resources