1 #// Usage: jjs -fx browser_dom.js
   2 
   3 /*
   4  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   5  *
   6  * Redistribution and use in source and binary forms, with or without
   7  * modification, are permitted provided that the following conditions
   8  * are met:
   9  *
  10  *   - Redistributions of source code must retain the above copyright
  11  *     notice, this list of conditions and the following disclaimer.
  12  *
  13  *   - Redistributions in binary form must reproduce the above copyright
  14  *     notice, this list of conditions and the following disclaimer in the
  15  *     documentation and/or other materials provided with the distribution.
  16  *
  17  *   - Neither the name of Oracle nor the names of its
  18  *     contributors may be used to endorse or promote products derived
  19  *     from this software without specific prior written permission.
  20  *
  21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  22  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 
  34 if (!$OPTIONS._fx) {
  35     print("Usage: jjs -fx browser_dom.js");
  36     exit(1);
  37 }
  38 
  39 // JavaFX classes used
  40 var ChangeListener = Java.type("javafx.beans.value.ChangeListener");
  41 var Scene     = Java.type("javafx.scene.Scene");
  42 var WebView   = Java.type("javafx.scene.web.WebView");
  43 var EventListener = Java.type("org.w3c.dom.events.EventListener");
  44 
  45 // JavaFX start method
  46 function start(stage) {
  47     start.title = "Web View";
  48     var wv = new WebView();
  49     wv.engine.loadContent(<<EOF
  50 <html>
  51 <head>
  52 <title>
  53 This is the title
  54 </title>
  55 <script>
  56 // click count for OK button
  57 var okCount = 0;
  58 </script>
  59 </head>
  60 <body>
  61 Button from the input html<br>
  62 <button type="button" onclick="okCount++">OK</button><br>
  63 </body>
  64 </html>
  65 EOF, "text/html");
  66 
  67     // attach onload handler
  68     wv.engine.loadWorker.stateProperty().addListener(
  69         new ChangeListener() {
  70             changed: function() {
  71                // DOM document element
  72                var document = wv.engine.document;
  73                // DOM manipulation
  74                var btn = document.createElement("button");
  75                var n = 0;
  76                // attach a button handler - nashorn function!
  77                btn.onclick = function() {
  78                    n++; print("You clicked " + n + " time(s)");
  79                    print("you clicked OK " + wv.engine.executeScript("okCount"));
  80                };
  81                // attach text to button
  82                var t = document.createTextNode("Click Me!"); 
  83                btn.appendChild(t);
  84                // attach button to the document
  85                document.body.appendChild(btn); 
  86            }
  87         }
  88     );
  89     stage.scene = new Scene(wv, 750, 500);
  90     stage.show();
  91 }