Referencing DOM from Java

17 views
Skip to first unread message

mauro...@gmail.com

unread,
Aug 19, 2019, 7:00:20 AM8/19/19
to bck2brwsr
Hi,

Is it possible to perform operations on the DOM from Java code?
I'm aware that communicating whit JavaScript can be done using the @JavaScriptBody annotation, but I don't see how to return a HTML element. For primitive types this is working, obviously.

Are there any classes that represent elements from the DOM? 

Thanks

mauro...@gmail.com

unread,
Aug 20, 2019, 1:37:43 AM8/20/19
to bck2brwsr
Ok, answering part of my own question here. The Dukescript libraries provided exactly this:

These can be obtained by including this dependency:
<dependency>
            <groupId>com.dukescript.libraries</groupId>
            <artifactId>net.java.html.lib.dom</artifactId>
            <version>0.5</version>
        </dependency>

But now I'm facing the next hurdle: how to get access to the document. This doesn't work for me:

    public static void main(String... args) {
       
Document document = getDocument();
       
System.out.println(document.URL());
   
}
   
   
@JavaScriptBody(args = {}, body = "return window.document;")
   
private static native Document getDocument();

While the getDocument() method actually returned an object, I'm getting errors like these:

TypeError: target.URL__Ljava_lang_String_2 is not a function

Any suggestions?


Op maandag 19 augustus 2019 13:00:20 UTC+2 schreef mauro...@gmail.com:

mauro...@gmail.com

unread,
Aug 20, 2019, 1:44:56 AM8/20/19
to bck2brwsr
Answering my own question again :)

This does the job:

Document document = Document.$as(getDocument());

Op dinsdag 20 augustus 2019 07:37:43 UTC+2 schreef mauro...@gmail.com:

Jaroslav Tulach

unread,
Aug 20, 2019, 7:05:12 AM8/20/19
to mauro...@gmail.com, bck2brwsr
Amazing solution! Thanks for giving bck2brwsr a try!
-jt


út 20. 8. 2019 v 7:44 odesílatel <mauro...@gmail.com> napsal:
--
You received this message because you are subscribed to the Google Groups "bck2brwsr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bck2brwsr+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/bck2brwsr/2c4bb440-bb9f-45e5-98b3-f8a3be686fff%40googlegroups.com.

mauro...@gmail.com

unread,
Aug 21, 2019, 11:34:23 AM8/21/19
to bck2brwsr
Hi, good to see this group is alive :)

Can I ask another 2 questions?

1. How would I bind an event? In my case I want to handle a button click but the API is unclear to me. (this bytecode to JS thing is still a mystery to me ;))

I have the following code, but it gives me an error when clicked:
        HTMLButtonElement button = HTMLButtonElement.$as(document.createElement("BUTTON"));
        button
.textContent.set("click me");
        button
.onclick.set((event) -> {
           
System.out.println("clicked " + event.toString());
           
return null;
       
});

My guess is I have to a $as for the lambda as well. But how?

2. I know that only a subset of the Java API is available, which is understandable from a technical and practical viewpoint. But is this easily extendible? For instance java.util.function.Consumer is not available to the runtime. What would it take to implement this on bck2brwsr?

Hope that you can shine a light on my questions.

Thanks
Mauro



Op dinsdag 20 augustus 2019 13:05:12 UTC+2 schreef Jaroslav Tulach:
To unsubscribe from this group and stop receiving emails from it, send an email to bck2...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages