Switched to JavaFX and added builtin Support
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package org.openautonomousconnection.luascript.fx;
|
||||
|
||||
import javafx.scene.web.WebEngine;
|
||||
import org.openautonomousconnection.luascript.fx.FxThreadBridge;
|
||||
import org.openautonomousconnection.luascript.hosts.ResourceHost;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* ResourceHost that loads script src via URLConnection.
|
||||
*
|
||||
* <p>Relative URLs are resolved against the current WebEngine location.</p>
|
||||
*/
|
||||
public final class FxWebViewResourceHost implements ResourceHost {
|
||||
|
||||
private final WebEngine engine;
|
||||
|
||||
/**
|
||||
* Creates a new resource host.
|
||||
*
|
||||
* @param engine web engine
|
||||
*/
|
||||
public FxWebViewResourceHost(WebEngine engine) {
|
||||
this.engine = Objects.requireNonNull(engine, "engine");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readText(String src) throws Exception {
|
||||
Objects.requireNonNull(src, "src");
|
||||
String trimmed = src.trim();
|
||||
if (trimmed.isEmpty()) throw new IllegalArgumentException("src is empty");
|
||||
|
||||
String base = FxThreadBridge.callAndWait(engine::getLocation);
|
||||
URL url = (base == null || base.isBlank())
|
||||
? new URL(trimmed)
|
||||
: new URL(new URL(base), trimmed);
|
||||
|
||||
URLConnection con = url.openConnection();
|
||||
con.setUseCaches(false);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
StringBuilder sb = new StringBuilder(4096);
|
||||
char[] buf = new char[8192];
|
||||
int r;
|
||||
while ((r = br.read(buf)) >= 0) {
|
||||
sb.append(buf, 0, r);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user