Java: Control browser through Process - java

I'm remote controlling a Java application on a PC through an Android phone, and I needed my application to open a browser at the phones command, chrome in this case. I created a "Process" for chrome, opening a certain address. However, I need to be able to give tools on the Android phone for controlling the web page, such as scrolling. Can I programmatically send a command for chrome to scroll from my PC application containing the Process?
Sorry, it may have been unclear, but the only connection the android phone has to the program is through a socket. It is only used as a remote control for another Java application on a PC, which has its own screen.

I do not think that clean solution exists.
But I can suggest the following directions:
(1) try to investigate the native chrome API. If it has such ability call it with JNI.
(2) Try to use class java.awt.Robot. It allows to simulate user's activity, e.g. mouse clicks. Unfortunately it does not allow you to find any window outside your application, so it is a problem to decide where to perform the click.
(3) You can create proxy server and make browser you open to go to the target URL through the proxy. The proxy server will insert into the page your javascript that will communicate with server. The application that opens browser will send commands to server. The javascript that you inserted will receive these commands using AJAX and perform them. JavaScript can scroll browser window, so theoretically you can implement this.

If you can target the tab you want to control and edit the address bar you could send the command 'javascript:scrollTo(x, y)'. I just tested it on this page and it seems to work fine, replacing what I typed with the original address of the page.

Can I programmatically send a command for chrome to scroll from my PC
application containing the Process?
Not directly. What you could do is make some sort of web service that sits between the Android client and page that the Android client can send commands to and the page can periodically poll via AJAX calls to see what the client wants. That would be a clean DIY way that would work on other browsers besides Chrome.

You can use vnc viewer applications for that.
http://code.google.com/p/android-vnc-viewer/

Related

Javafx webengine protocols

I've created a browser using Javafx, which works fine to go to websites but I'd like to be able to do external protocols (ie: chrome:// or, if you are familiar with nexus mod manager, it launches it via the web using nxm://). I am essentially doing the same thing as nxm on my website, but I want to be able to use the client I have created. Simply typing something like nxm:// yields an about:blank page. Is this possible with jfx?
Edit: I was thinking I could check for a prefix at navigation other than http and https and launch it completely outside the browser?

Can you communicate with a browser window, after opening it with a JavaFX app?

My question is just as the title indicates.
I have a JavaFX app, and need to open a browser window. After opening it, I want to be able to communicate from the browser back to the app that opened it.
For example, if I opened the default browser window like this:
URI u = new URI(url);
java.awt.Desktop.getDesktop().browse(u);
Three options:
Through Javascript
Using javascript, and the window name you could access any window.
E.g. See: https://stackoverflow.com/a/16525481/1688441
Through Ajax Calls and a Server/Database
As another user answered, communication could be done with an intermediate server.
Third party library
The only other thing I can think of is using a 3rd party library to get the window within Operating System, though not much more I can suggest.
You can communicate back and forth with a WebView component. See http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm
If you are launching a page in the hosts browser, you'll need to develop a server based method to communicate.

call gui from JSP?

I am currently learning JSP and Java Servlets. I was wondering if there is any way to call a java GUI from a JSP, not necessarily to load into the page, but to load onto the screen for database input. I am struggling trying to find some reference on how to do this on the net, but no luck so far.
You could use Java Webstart to start up the application on the client side. Or, ask the user to install a local client differently (like a daemon service).
The "GUI application" must have some kind of RPC (=remote procedure call) service running in background.
Once the client has your gui application installed, the JSP can invoke urls on http://localhost:port (ajax would help sending messages to the GUI application). The GUI application must have some rpc system (rest, webservice) that receives the requests from your web application (your jsp app) and start the GUI requested by the URL.
The problem here is to make sure the client has the port of choice available.
Another possibility is to register some URL to your application in the client side, so that when the user clicks a url like myapp://form/123 the OS automatically invokes your application to handle that URL. This kind of thing must be done differently per operating system. It's the way it works when you click on a magnet link or a skype://link. The procedure to register urls in the operating system is different per OS, you need to do some research (I never did that, but I am sure it is possible). You can also register a file extension to your app, and make sure that the specified extension is registered to your app. When the browser downloads the file and opens it, your app is invoked with the file downloaded as parameter (which will contain the instruction for your GUI). But most browser will not start automatically the app associated to the file after the download: again, you need to do some tricks on the client's operating system to make the "download and open" the default behavior for the browser.

Open html in browser from Java. Bug if browser is not started?

I am using
java.awt.Desktop.getDesktop().browse(uri);
to show my users a generated html file. All is fine if the browser (firefox 3.5.7; linux) is started before I make the call. But if the browser start is triggered from the getDesktop().browse call then the java application will not exit until the browser closes.
How can I avoid this behaviour? Is this known under windows/macOsx too?
(If I then force the exit of the java application the browser will close too and sometime even crash!?)
The reason the browser exists is because the browser is launched as a dependent process...so when the first process shuts down, all its dependent processes are shut down with it. But in the case when you start the browser first, it already has a different process ID that isn't affected by your application. I think the only way to avoid this behavior is to use a different technique (perhaps Runtime.exec()?) to launch the browser in a way that registers it as a non-dependent process.
The project Browser Launcher (http://browserlaunch2.sourceforge.net/) can be the solution for your problem. You can launch a browser from your code like this:
String url = "http://....";
BrowserLauncher() launcher = new edu.stanford.ejalbert.BrowserLauncher();
launcher.openURLinBrowser(url);

Use java.awt.Desktop on client/server application for open browser on client

I'm trying to use the desktop class in client/server application.
I want the default browser to open on the client side when the client clicks on a button. What happens is that the browser opens on the server. How can I fix it?
Thanks
If you call any Desktop method on the server, then of course the reaction will occur on the server.
If you want a window to pop up on the client, then call the method on the client.
What kind of client are you using?
If you're using a rich Java client, then that change should be relatively easy.
Its browser specific,
Fiddle with the following setting, in IE:
Change the settings in IE to open a new window. As you pointed out the settings is available in
Tools->Internet Options->Advanced->Reuse Windows for shortcuts
Its browser specific,
Fiddle with the following setting, in IE:
Change the settings in IE to open a new window. As you pointed out the settings is available in
Tools->Internet Options->Advanced->Reuse Windows for shortcuts
Check this answer

Categories