2

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

3
  • According documentation ajax is supported by this library. Commented Aug 27, 2019 at 10:03
  • @reporter I see, but the $-sign just before ajax ($.ajax) is not recognized Commented Aug 28, 2019 at 1:18
  • This lib uses a different syntax. Commented Aug 28, 2019 at 8:14

1 Answer 1

2

Use HtmlUnit Java Library

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

Sign up to request clarification or add additional context in comments.

1 Comment

final WebClient webClient = new WebClient(BrowserVersion.CHROME); The first line already indicating that I might need to anticipated all kind of browsers to use this. Regardless of that, Thank you for your idea. I will try it and see if it will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.