如何从 SWT 与 Javascript 函数对话

发布于 2024-12-10 00:08:39 字数 2433 浏览 0 评论 0原文

我的 HTML 文件有一个 javascript 函数 xxx_return(),它将返回一个字符串值。有什么办法可以从Java层获取这个值吗?

我正在使用 SWT shell 来显示此 html。 SWT 是否具有获取脚本函数返回值的功能?

编辑:

我的代码如下所示: 包test.html.simulation;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class BrowserExample{
public static void main(String[] args) 
{
  Display display = new Display();
  final Shell shell = new Shell(display);
  String html="";
  Object ob=null;
    shell.setText("Browser Example");
    shell.setSize(500, 350);

        final Browser browser = new Browser(shell, SWT.NONE);
        browser.setBounds(5, 75, 600, 400);

        browser.setUrl("http://localhost/test/tryxml.html");

        shell.open();
        //System.out.println(browser.getUrl());
        //try
        {
        html=(String)browser.evaluate("returnHTML();");
        }/*catch(SWTException e)
        {
            System.out.println(e.getMessage());

        }*/
        System.out.println(html);

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
            }

        display.dispose();


}

这段代码给了我一个像预期的对象一样的SWT异常:

Exception in thread "main" org.eclipse.swt.SWTException: Object expected
at org.eclipse.swt.browser.WebBrowser$EvaluateFunction.function(Unknown Source)
at org.eclipse.swt.browser.WebSite.Invoke(Unknown Source)
at org.eclipse.swt.browser.WebSite$7.method6(Unknown Source)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(Unknown Source)
at org.eclipse.swt.internal.ole.win32.COM.VtblCall(Native Method)
at org.eclipse.swt.internal.ole.win32.IDispatch.Invoke(Unknown Source)
at org.eclipse.swt.ole.win32.OleAutomation.invoke(Unknown Source)
at org.eclipse.swt.ole.win32.OleAutomation.invoke(Unknown Source)
at org.eclipse.swt.browser.IE.execute(Unknown Source)
at org.eclipse.swt.browser.WebBrowser.evaluate(Unknown Source)
at org.eclipse.swt.browser.Browser.evaluate(Unknown Source)
at test.html.simulation.BrowserExample.main(BrowserExample.java:29)

在java脚本中,我在脚本标签中编写了一个函数,例如:

<script>
function returnHTML()
  {
   var str=document.body.innerHTML;
   //alert(str);
   return str;
  }
</script>

任何人都可以找到其中的错误吗?我不明白它在哪里遇到错误。

谢谢。

My HTML file has a javascript function xxx_return(), which will return a string value. Is there any way i can take this value from Java layer?.

I am using SWT shell to display this html. Does SWT carry any feature to get the return values of a script function?

edit:

My code is something like below:
package test.html.simulation;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class BrowserExample{
public static void main(String[] args) 
{
  Display display = new Display();
  final Shell shell = new Shell(display);
  String html="";
  Object ob=null;
    shell.setText("Browser Example");
    shell.setSize(500, 350);

        final Browser browser = new Browser(shell, SWT.NONE);
        browser.setBounds(5, 75, 600, 400);

        browser.setUrl("http://localhost/test/tryxml.html");

        shell.open();
        //System.out.println(browser.getUrl());
        //try
        {
        html=(String)browser.evaluate("returnHTML();");
        }/*catch(SWTException e)
        {
            System.out.println(e.getMessage());

        }*/
        System.out.println(html);

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
            }

        display.dispose();


}

This code gives me an SWT Exception like Object expected:

Exception in thread "main" org.eclipse.swt.SWTException: Object expected
at org.eclipse.swt.browser.WebBrowser$EvaluateFunction.function(Unknown Source)
at org.eclipse.swt.browser.WebSite.Invoke(Unknown Source)
at org.eclipse.swt.browser.WebSite$7.method6(Unknown Source)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(Unknown Source)
at org.eclipse.swt.internal.ole.win32.COM.VtblCall(Native Method)
at org.eclipse.swt.internal.ole.win32.IDispatch.Invoke(Unknown Source)
at org.eclipse.swt.ole.win32.OleAutomation.invoke(Unknown Source)
at org.eclipse.swt.ole.win32.OleAutomation.invoke(Unknown Source)
at org.eclipse.swt.browser.IE.execute(Unknown Source)
at org.eclipse.swt.browser.WebBrowser.evaluate(Unknown Source)
at org.eclipse.swt.browser.Browser.evaluate(Unknown Source)
at test.html.simulation.BrowserExample.main(BrowserExample.java:29)

In the java script i have written a function in the script tag like:

<script>
function returnHTML()
  {
   var str=document.body.innerHTML;
   //alert(str);
   return str;
  }
</script>

Can anyone find the error in this?. I don't understand where it hits the error.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情 2024-12-17 00:08:39

使用 SWT Browser 对象。然后您可以简单地使用String result = (String)Browser.evaluate("xxx_return();")

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

寂寞清仓 2024-12-17 00:08:39

我发现,由于在页面加载到 shell 之前调用了 Browser.evaluate() ,所以发生了异常。我添加了一个 ProgressListener 来了解完成情况,并尝试调用它工作。

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

谢谢大家

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

ぺ禁宫浮华殁 2024-12-17 00:08:39

除了上述解决方案外,在表达式前面添加“return”。另外,根据您正在评估的内容,可以忽略已完成的事件。下面的表达式就可以了。

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

当然,如果您要从浏览器中加载的页面评估 JavaScript,则必须在完成事件后调用评估,否则 JavaScript 可能尚未加载。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文