The hobbit or back and forth
Some time ago, I urgently needed to find possible ways to get variables from js and the browser in order to transfer their value in my application to java. Unfortunately, this question was not solved on Habré, or perhaps the power left me and the search is no longer so easy.
Anyway, today I will try to shed some light on this issue to some extent. So let's get started!
Task
In short, I will describe the situation - there is a certain application with the editor page, as editor we have a free
NiceEdit . Why exactly the external editor - I needed to quickly build in the editor + the user data is presented as html and writing a bike would be quite silly. There were attempts to build a ready-made widget on swt, but either the project is outdated or the author has turned his head - the widget refused to work.
The main problem is that the user enters / edits the data in the editor, clicks the miracle button, and our application should receive what has just been written.
UPD
I will describe in more detail what the application is:
This is a standalone Eclipse RCP application in which user data is presented in html \ xml format due to circumstances. To create such data, view-ha was made with a browser into which nicedit was loaded and the user wrote there what he wanted. The problem was to get this data back to the application.
Decision
As it turned out, there is more than one solution to this problem:
- Method number 1 - JNI
The simplest and most useful thing that came to mind is to do it in the HTML editor page, which is loaded by the swt browser built into the application, its text field and set the editor on it:
<script type="text/javascript"> var textArea; bkLib.onDomLoaded(function() { new nicEditor({fullPanel : true}).panelInstance('area1'); var el = document.getElementById('EDITOR').getElementsByTagName('div'); textArea = el[el.length-1]; }); </script>
Further, in the application itself, we need to write a native function, which we will call:
static class CustomFunction extends BrowserFunction {
Creating a new customdata gives an echo, although this method was described on stackowerflow
Create an object of our function:
new CustomFunction(browser, "getTextAreaContent");
Now we can proudly call the function we need from the browser:
browser.execute("var cont = textArea.innerHTML; getTextAreaContent(cont);");
- Method number 2 - the creation of a script and a call through the browser (this code is an example, which is all shown on stackflowflow - it catches the selection of text on the page and sends it to the application).
Nobody prevents us from creating a variable, writing a necessary piece of script there and executing it through a browser. And the script will create a user event, changing the browser status:
final String SELECTIONSCRIPT = "function handleSelection() { " + " var selTxt = ''; " + " if (window.getSelection) { " + " selTxt = window.getSelection(); " + " } else if (document.getSelection) { " + " selTxt = document.getSelection(); " + " } else if (document.selection) { " + " selTxt = document.selection.createRange().text; " + " } " + " window.status = '::SELECTION::' + selTxt; " + "} " + "document.attachEvent('onmouseup', handleSelection); "; final Browser browser = new Browser(shell, SWT.NONE); browser.addStatusTextListener(new StatusTextListener() { public void changed(StatusTextEvent arg0) { if (arg0.text.startsWith("::SELECTION::")) { String selection = arg0.text.substring("::SELECTION::".length()); System.out.println(selection); } }}); browser.addProgressListener(new ProgressAdapter() { @Override public void completed(ProgressEvent arg0) { browser.execute(SELECTIONSCRIPT); } });
True, there are some pitfalls waiting for us, which no one mentioned in various forums - not all browsers allow applications to change the window status without the user's permission. That is, for example, in Mozilla, you need to set the dom.disable_window_status_change flag to false in about: config.
Here are both ways that I know and used ... I will be glad to read new ideas and comments on the article. Thanks for attention.