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.
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 page of the editor, which is loaded with the java-browser integrated into its own 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:
staticclassCustomFunctionextendsBrowserFunction{ //CustomFunctionData data = new CustomFunctionData(null); public CustomFunction(Browser browser, String name) { super(browser, name); } public Object function(Object[] arguments) { templateText = (String) arguments[0]; return null; } }
Creating a new customdata gives an echo, although this method was described on stackowerflow Create an object:
new CustomFunction(browser, "getTextAreaContent");
Now we can proudly call the function we need from the browser:
Method number 2 - creating a script and calling through the browser 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:
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.