How do you get javascript code in java app?



  • Hello. Please indicate how the violin, located on the remote site in java, can be implemented? Trying to use ScriptEngineManager, but he doesn't want to work at all. It's a challenge to write your own browser. But I can't deal with the violets. First, the website's violators are downloading into the folder. But when they start to do a lot of mistakes. For example, there's no variable window, but the window isn't a variable? Is there something I don't understand?

        public void scriptExecutor(){
        try{
            File file = new File("/Users/user/desktop/js");
            File[] f = file.listFiles();
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("nashorn");
            for(int i = 0; i<f.length; i++){
                System.out.println(f[i]);
                engine.eval(Files.newBufferedReader(Paths.get(f[i].getAbsolutePath())));
    
        }
    
        }catch(Exception e){e.printStackTrace();}
    }
    



  • You have a very ambitious task. I'd say it's a top pilot. Google and he built a chrome using webkit. If you have the task of making a full browser, I'd advise you not to invent the bike and use that webkit as a move.
    Now on the substance of the question. Your problem is nashorn is just a move that js can do. Unfortunately, he doesn't know anything about the context of the browser, the object of the window, he doesn't even know anything about alert. To get him to execute the code from the page, you need to create a context according to the specifications for the browser context. I mean, you need to create an objective window, document and a bunch of other objects and methods and add them to the context of nashorn.

    For starters, look at the site specification. https://www.w3.org/TR/html51/browsers.html#apis-for-creating-and-navigating-browsing-contexts-by-name ♪

    You can also look at some existing projects like what you do, for example, https://sourceforge.net/projects/jxwb/




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2