Hi, I'm trying to use web-views and wondering how can I send an event from javascript to cljfx. any idea ?
what I ended up doing was using WebSockets via Sente (https://github.com/ptaoussanis/sente) to create an event bus, this is a little bit involved but ultimately has a nice property: you can transparently dispatch events between the webview and javafx bi-directionally using the same re-frame esque conventions (if you also plan on using ClojureScript in the Webview)
I would suggest having a look at this: https://openjfx.io/javadoc/14/javafx.web/javafx/scene/web/WebEngine.html
this doc describes how one can setup communication with the web page
you can access the web engine by creating custom props, for example
here are some examples of built-in custom props that access WebEngine: https://github.com/cljfx/cljfx/blob/master/src/cljfx/ext/web_view.clj
thank you a lot, I was hoping for something builtin to cljfx, but it seems doable. So if I understand well, I will have to create my own lifecycle, which will setup the communication in its create method (by using WebEngine)?
you will create extension lifecycle that wraps web-view lifecycle:
(def ext-with-my-stuff
(fx/make-ext-with-props
{:my-custom-prop-that-establishes-communication ...}))
{:fx/type ext-with-my-stuff
:props {:my-custom-prop-that-establishes-communication ...}
:desc {:fx/type :web-view
...}}
nice 🙂 I will start like this. thank you a lot for the help
I've got a java question (I'm not so much familiar with clj/java interop). I'm trying to make this snippet work in the context of my cljfx app: (its from the https://openjfx.io/javadoc/14/javafx.web/javafx/scene/web/WebEngine.html you've linked)
EventListener listener = new EventListener() {
public void handleEvent(Event ev) {
Platform.exit();
}
};
Document doc = webEngine.getDocument();
Element el = doc.getElementById("exit-app");
((EventTarget) el).addEventListener("click", listener, false);
To be honest I don't even understand the first statement... And I don't know how can I translate it to clojure... sorry for the noobyness :)first statement — you mean this?
EventListener listener = new EventListener() {
public void handleEvent(Event ev) {
Platform.exit();
}
}
yes
it’s like that in clojure:
(reify EventListener
(handleEvent [this ev]
(Platform/exit)))
so it creates an instance of EventListener interface
@joelkuiper seems appealing to be able to use clojurescript in a webview, i've tryed without success. do you have some code to show ?
@vlaaad, ho! I'm surprised that it is so simple! thank you (looking at the javadoc I taught that I would need a proxy)
@vlaaad, when I try to evaluate your expression I get this error
Syntax error (IllegalArgumentException) compiling reify* at (codemirror.clj:75:1).
Can't define method not in interfaces: handleEvent
Maybe EventListener is not what I taught it was. I'm using java.util.EventListener
Am I missing something ?yep, it’s org.w3c.dom.events.EventListener
ok 🙂 thank you a lot
I found it like that: WebEngine’s document property is defined here: https://openjfx.io/javadoc/14/javafx.web/javafx/scene/web/WebEngine.html#documentProperty()
it returns a value of type “Document”, and it’s has a link leading here: https://docs.oracle.com/en/java/javase/12/docs/api/java.xml/org/w3c/dom/Document.html?is-external=true
So this is something from the JDK, and at that site I searched for addEventListener
and found this: https://docs.oracle.com/en/java/javase/12/docs/api/java.xml/org/w3c/dom/events/EventTarget.html#addEventListener(java.lang.String,org.w3c.dom.events.EventListener,boolean)