webview / webview_java

A Java wrapper of https://github.com/webview/webview
Other
68 stars 11 forks source link

eval("console.log('hello world!')") is not work #8

Closed fangyuchuang closed 10 months ago

fangyuchuang commented 1 year ago

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.

    // 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);

    String url = "file:\\\\C:\\mansiExe\\client\\device\\customer\\nwcn-touyinji\\html\\index.html";
    wv.loadURL(url);

    // Run the webview event loop, the webview is fully disposed when this returns.
    new Thread(new Runnable() {
        @Override
        public void run() {
            Scanner sc=new Scanner(System.in);
            String word;
            //定义while控制循环输入
            System.out.println("请输入任意内容(输入exit就退出,否则继续输入):");
            while (true) {
                //键盘接收数据
                word=sc.nextLine();
                //判断接收的数据是否为"exit"
                wv.eval("console.log(11)} ");

                if (word.equals("exit")) {
                    //如果是,提示退出输入
                    System.out.println("退出输入!");
                    //跳出循环
                    break;
                }else {//如果不是,提示继续输入
                    System.out.println("请继续输入:");
                }
            }
        }
    }).start();

    wv.run();

}

when i press anykey, the java log can printf ,but the javascript is not ecxe.

e3ndr commented 1 year 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.

fangyuchuang commented 1 year ago

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();
}

}

fangyuchuang commented 1 year ago

is the webview.dll will call the edge api, if so, can i reinstall some difference edge version to try?

e3ndr commented 1 year ago

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.

Osiris-Team commented 1 year ago

@fangyuchuang After loadURL() add Thread.sleep(10)

newk5 commented 10 months ago

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

newk5 commented 10 months ago

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);
    }