Java Writing Browser kernel implementation JavaScript Document object and built-in method

Source: Internet
Author: User

Original article. Reprint please specify.

Before reading this article, you will need to know how the browser loads JavaScript.

Of course, there is a need to understand Java and JavaScript itself.

This article first describes how the browser loads and executes JavaScript.

Next, we'll show you how to call objects in Java and JavaScript.

Finally, how to implement JavaScript Document object and other built-in methods with Java is introduced.

After the browser obtains HTML text containing JavaScript code from the server, it runs the JavaScript code block sequentially.

Looking at the code is the simplest way:

Package Org.taoningyu.js;import Java.io.ioexception;import Java.net.malformedurlexception;import Java.net.URL; Import Javax.script.invocable;import Javax.script.scriptengine;import Javax.script.scriptenginemanager;import Javax.script.scriptexception;import Org.jsoup.jsoup;import Org.jsoup.nodes.document;import Org.jsoup.nodes.element;public class Taodocument {Document mydoc = null;public taodocument (String url) throws malformedu Rlexception, Ioexception{mydoc = jsoup.parse (new URL (URL), 30000),//use Jsoup to implement document tree} public Element getElementById (    String ID) {//Implement document's getElementById method System.out.println ("Java println:\t" +mydoc.getelementbyid (ID)); return Mydoc.getelementbyid (ID);//Returns the element object}public static void Main (string[] args) throws Malformedurlexception, I    oexception {Scriptenginemanager sem = new Scriptenginemanager (); ScriptEngine se = Sem.getenginebyname ("javascript"); Initialize Java built-in JavaScript engine try {se.eval ("function test () {" + "id= ' AreadeFault ';                  Print (' JS print: ' +document.getelementbyid (ID)); return document.getElementById (ID);} ");     Test JavaScript to define its own function, the function is output ID areadefault element, and return. Invocable invocableengine = (invocable) se;//conversion engine type is invocable se.put ("document", New Taodocument ("http://www.ifeng.co m/")); Association object, this step is very important, the document object of the associated JavaScript is taodocument. That is, my own implementation of the Document object Element callbackvalue= (Element) invocableengine.invokefunction ("test"); Executes the function directly. The return value is element System.out.println ("Callback return:" +callbackvalue); The printout returns the content Se.eval ("Test ()");//The second way of calling functions, I prefer this way} catch (Scriptexception e) {//TODO auto-generated catch Blocke.prin Tstacktrace ();} catch (Nosuchmethodexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

By implementing the Document object, the HTML page can be parsed dynamically.

In an additional article. I'll show you how to combine httpclient to make it a prototype for headless browsers.



Java Writing Browser kernel implementation JavaScript Document object and built-in method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.