IDK what i was going here
This commit is contained in:
@@ -88,7 +88,6 @@ public class HtmlApp {
|
|||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
""";
|
""";
|
||||||
|
|
||||||
// Erstelle das GUI und lade das Beispiel
|
// Erstelle das GUI und lade das Beispiel
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package org.openautonomousconnection.htmlparser.script;
|
||||||
|
|
||||||
|
import org.openautonomousconnection.htmlparser.dom.*;
|
||||||
|
import org.openautonomousconnection.htmlparser.events.Event;
|
||||||
|
|
||||||
|
public final class DomAdapter {
|
||||||
|
private final Document document;
|
||||||
|
|
||||||
|
public DomAdapter(Document document) {
|
||||||
|
this.document = document;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Document document() { return document; }
|
||||||
|
|
||||||
|
// expose: document.querySelector(...)
|
||||||
|
public Element querySelector(String sel) { return document.querySelector(sel); }
|
||||||
|
public Element getElementById(String id) { return document.getElementById(id); }
|
||||||
|
|
||||||
|
public Element createElement(String tag) { return document.createElement(tag); }
|
||||||
|
public TextNode createTextNode(String text) { return document.createTextNode(text); }
|
||||||
|
|
||||||
|
public void appendChild(Element parent, Object child) {
|
||||||
|
if (parent == null || child == null) return;
|
||||||
|
if (child instanceof Element e) parent.appendChild(e);
|
||||||
|
else if (child instanceof TextNode t) parent.appendChild(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setText(Element el, String text) { if (el != null) el.setText(text); }
|
||||||
|
public void setAttr(Element el, String k, String v) { if (el != null) el.setAttribute(k, v); }
|
||||||
|
public String getAttr(Element el, String k) { return el == null ? null : el.getAttribute(k); }
|
||||||
|
|
||||||
|
public void styleSet(Element el, String k, String v) { if (el != null) el.style().set(k, v); }
|
||||||
|
|
||||||
|
public void click(Element el) {
|
||||||
|
if (el == null) return;
|
||||||
|
el.dispatchEvent(new Event("click", true));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package org.openautonomousconnection.htmlparser.script;
|
||||||
|
|
||||||
|
import org.openautonomousconnection.htmlparser.dom.*;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public final class ScriptEngine implements AutoCloseable {
|
||||||
|
|
||||||
|
private final PythonInterpreter py;
|
||||||
|
private final PyDomAdapter dom;
|
||||||
|
|
||||||
|
public ScriptEngine(Document document) {
|
||||||
|
this.py = new PythonInterpreter();
|
||||||
|
this.dom = new PyDomAdapter(Objects.requireNonNull(document));
|
||||||
|
// expose "document" + "dom"
|
||||||
|
py.set("dom", dom);
|
||||||
|
py.set("document", document);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exec(String code) {
|
||||||
|
if (code == null) return;
|
||||||
|
py.exec(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
try { py.close(); } catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user