Closed fangyuchuang closed 10 months ago
You have a syntax error here:
wv.eval("console.log(11)} ");
^
It should be:
wv.eval("console.log(11);");
See if that fixes your issue.
thank for your reply, i am sorry for making the mistake code, but i try again by the correct code , it can not work correct. you can try the example below.
import dev.webview.Webview; import java.util.Scanner;
public class Example { public static void main(String[] args) { Webview wv = new Webview(true); // Can optionally be created with an AWT component to be painted on.
// Calling `await echo(1,2,3)` will return `[1,2,3]`
wv.bind("echo", (arguments) -> {
return arguments;
});
wv.setTitle("My Webview App");
wv.setSize(800, 600);
wv.loadURL("http://www.google.com");
new Thread(new Runnable() {
@Override
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.print("input a num: ");
while (true) {
int num1 = scanner.nextInt();
switch (num1) {
case 1:
wv.eval("console.log('hello world!')");
wv.eval("window.alert('hello world!')");
System.out.println("1 is press");
break;
default:
System.out.println("default");
}
}
}
}).start();
// Run the webview event loop, the webview is fully disposed when this returns.
wv.run();
}
}
is the webview.dll will call the edge api, if so, can i reinstall some difference edge version to try?
is the webview.dll will call the edge api, if so, can i reinstall some difference edge version to try?
You will need the latest Edge and WebView2 Runtime installed.
@fangyuchuang After loadURL() add Thread.sleep(10)
Same issue here. I even tried to delay calling .eval until the webview fully initialized. I also tried it inside a .bind() lambda but it doesn't seem to be working
Ok after more testing I noticed the following:
Calling webview.eval() after webview.run() (like in a different thread) will never work. webview.eval only works inside a webview.bind() lambda or if you call it after loadHTML but before .run(). Those cases will work if you load a URL from disk (file://) or from somewhere on the web (http://). However if loading a url made from data URL, webview.eval will never work in any ocasion.
This is the code I'm using to convert my html to a base64 data URL
public void loadHTML(String html) {
String c = "data:text/html;base64,%s".formatted(Base64.getEncoder().encodeToString(html.getBytes()));
webview.loadURL(c);
}
wv.evall("console.log('hello world!')") used by java.
public static void main(String[] args) { Webview wv = new Webview(true); // Can optionally be created with an AWT component to be painted on.
}
when i press anykey, the java log can printf ,but the javascript is not ecxe.