Search code examples
javanashorn

Nashorn parse web page


I'm trying to use Nashorn to parse the JS content of a webpage. I use the following code to init Nashorn and load the page:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine eng = manager.getEngineByName("nashorn");
//...
        URL url = new URL("http://192.168.1.1/userRpm/StatusRpm.htm");
        URLConnection connection = url.openConnection();
        try {
            eng.eval(new InputStreamReader(connection.getInputStream()));
            value = eng.get("mobileParam[0]");
            System.out.println(value);
        } catch (ScriptException ex) {
            Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
        }

the URLConnection is able to reach the page, I tried to print the output and it worked, but for strange reason, the instruction: eng.eval(new InputStreamReader(connection.getInputStream())); give me the error:

Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:1:0 Expected an operand but found <

Maybe I misunderstand how it works. Anyone can explain me better?

the output of URL is:

< SCRIPT type="....


Solution

  • Nashorn can evaluate Javascript, but you are feeding a HTML page into it (which contains just a script element and a script) and therefore it fails.

    You could load the URL, extract the script from the script element (e.g. using JSoup) and then use Nashorn to run the extracted script.