How to talk to a Javascript function from SWT

北慕城南 提交于 2019-12-03 06:51:24

Use an SWT Browser object. Then you can simply use String result = (String)Browser.evaluate("xxx_return();").

I found it, the exception occurred since the Browser.evaluate() was getting called before the page was loaded in the shell. I added a ProgressListener to know the completion, and tried calling it worked.

browser.addProgressListener(new ProgressListener() {
              public void changed(ProgressEvent event)
              {

              }
              public void completed(ProgressEvent event)
              {String htm;
                htm=(String)browser.evaluate("return returnHTML()"); 
                System.out.println(htm);
              }
            });

Thanks All

In addition the above solutions, add "return" in front of the expression. Also depending on what you are evaluating, completed event can be ignored. Following expression just works.

browser.evaluate("return 4 + 5;")

Of course if you're evaluating javascript from the page loaded in the browser, evaluate must be called after completed event, otherwise the javascript may not have been loaded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!