0

I'm using Firebug lite to debug the HTML elements in JavaFX webview. I'm using the below code.

engine.documentProperty().addListener(new ChangeListener<Document>() {
            @Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
                enableFirebug(engine);
            }
        }); 
private static void enableFirebug(final WebEngine engine) {
        engine.executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");

    }

I have a javascript which will print out the id's and name's of the elements onto firebug console, on every click on different HTML elements. I need to get this console output to my JavaFX application. Please help.

engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
            @Override
            public void changed(ObservableValue<? extends State> observable,
                    State oldValue, State newValue) { 
                JSListener listener = new JSListener();

                JSObject jsobj = (JSObject) engine.executeScript("console.log = function(){"
                        + "var lastElement = null; "
                        + "document.addEventListener('click', function(e) {"
                        + "if (e.target != lastElement) {"
                        + "lastElement = e.target;"
                        + "java.log(lastElement.name);"
                        + "}}"
                        + ");"
                        +"}");                      
                jsobj.setMember("java", listener);  
            }
        });

Java Code :

public class JSListener { 

    public void log(String text) {
        System.out.println(text);
    } 
}

I'm using the below code to get the element ID. Please let me know where I'm going wrong!

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
NaveenBharadwaj
  • 1,212
  • 5
  • 19
  • 42
  • Have you looked at [How to get console.log() from javascript to System.out in java?](http://stackoverflow.com/questions/28687640/javafx-8-webengine-how-to-get-console-log-from-javascript-to-system-out-in-ja) – ItachiUchiha May 19 '15 at 06:38
  • @ItachiUchiha, thanks for the link. I'm trying to execute similarly. However I'm not getting any output in java console. It would be great if I can catch it into a variable rather than printing on console. I have edited the question and added the code. Please point out my mistake. – NaveenBharadwaj May 19 '15 at 06:53

0 Answers0