I'm building a web app with Diagram in it.
For the diagram I used jsPlumb library Link: https://jsplumbtoolkit.com/
One of my requirement is to make Blocks inside the Diagram like flowchart. And one type of the blocks is a able to be inserted by customizable script. So, when user double-clicked that block, they can input Javascript code in the textarea and will be executed later.
Here lies my problem : I able to run the script code very well, when it is still on the front-end side (jsp) using browser using "new Function" from Javascript. But after the block is saved, the script saved to DB. And if I need to run it again, then it will be executed from back-end (Java).
Therefore, I used ScriptEngine to run the Javascript. The problem is ajax, $-sign, console, etc are not recognized from Java. And I found out later, that ScriptEngine did not support for those kind of things.
So, I wonder is there is any possible way to make these possible ? I'm open to other alternative idea.
Thank you
-
According documentation ajax is supported by this library.Reporter– Reporter2019年08月27日 10:03:09 +00:00Commented Aug 27, 2019 at 10:03
-
@reporter I see, but the $-sign just before ajax ($.ajax) is not recognizedLyn– Lyn2019年08月28日 01:18:52 +00:00Commented Aug 28, 2019 at 1:18
-
This lib uses a different syntax.Reporter– Reporter2019年08月28日 08:14:27 +00:00Commented Aug 28, 2019 at 8:14
1 Answer 1
Sample code
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
final HtmlPage page = webClient.getPage("http://127.0.0.1:9090/mysite");
page.executeJavaScript("$");
webClient.close();
This http://127.0.0.1:9090/mysite can be your local website which already has jquery
You can also inject a script tag on the fly in a blank html page
If your are behind a proxy then
final WebClient webClient = new WebClient(BrowserVersion.CHROME, "proxyhost", 8080);
Alternate Idea
Use Jaunt - Java Web Scraping & JSON Querying Java Library
Sample code and full documentation available in the site