Re: Referencing DOM from Java

13 views
Skip to first unread message

Jaroslav Tulach

unread,
Nov 7, 2019, 10:29:37 PM11/7/19
to bck2...@apidesign.org


22. 8. 2019 v 20:10, Mauro de Wit <mauro...@gmail.com>:


Hi Jaroslav,

I'll give your suggestion a try and, once successful, will definitely create a pull request. It sure sounds easy :)
Does it have to be JDK 8? Since version 0.30 JDK 11 is supported and wouldn't it be more logical to use those classes?

By the way, the error is quite cryptic. But here it is:
TypeError: arr is null
    callJavaFunction__Ljava_lang_Object_2Lnet_java_html_lib_Function$A5_2_3Ljava_lang_Object_2Lnet_java_html_lib_Function$JavaFn_2 http://localhost:10506/lib/net.java.html.lib-0.5.js:724
    access$100__Ljava_lang_Object_2Lnet_java_html_lib_Function$A5_2_3Ljava_lang_Object_2Lnet_java_html_lib_Function$JavaFn_2 http://localhost:10506/lib/net.java.html.lib-0.5.js:748
    apply__Ljava_lang_Object_2Ljava_lang_Object_2_3Ljava_lang_Object_2 http://localhost:10506/lib/net.java.html.lib-0.5.js:2042
    apply__Ljava_lang_Object_2Ljava_lang_Object_2_3Ljava_lang_Object_2 http://localhost:10506/lib/net.java.html.lib-0.5.js:31835
    net_1java_1html_1lib_1Function$apply$Ljava_1lang_1Object_12_13Ljava_1lang_1Object_12__Ljava_lang_Object_2Lnet_java_html_lib_Function_2Ljava_lang_Object_2Ljava_lang_Object_2 http://localhost:10506/lib/net.java.html.lib-0.5.js:80

I hope you can point me in the right direction.


Regards,

Mauro

On Thu, 22 Aug 2019 at 17:15, Jaroslav Tulach <jarosla...@gmail.com> wrote:
Hello Mauro,
hard to say what is wrong in your code without seeing the error.

As far as your question about `java.util.function` interfaces go, it is just a matter of including them in the emulation library: https://github.com/jtulach/bck2brwsr/tree/master/rt/emul/compact/src/main/java/java/util - just copy the classes from JDK8 and that's it. They are likely to work without any problems as it is a pure bytecode - no native methods.

You can either create a pull request and I include your classes in next version of bck2brwsr or you can create your own JAR with the emulation classes and include it on classpath - they don't have to be in the emul-rt.jar/js.

I hope you choose to create the pull request - I haven't got a contribution to bck2brwsr for years: It would be a reason to celebrate.
-jt


st 21. 8. 2019 v 17:34 odesílatel <mauro...@gmail.com> napsal:
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:
Amazing solution! Thanks for giving bck2brwsr a try!
-jt


út 20. 8. 2019 v 7:44 odesílatel <mauro...@gmail.com> napsal:
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:
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:
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

--
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 bck2...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/bck2brwsr/2c4bb440-bb9f-45e5-98b3-f8a3be686fff%40googlegroups.com.

--
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/e3a5f950-3134-4373-acbc-88056c5bb81b%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages