简体   繁体   English

如何在 Eclipse 控制台中显示 javascript console.log?

[英]How to display javascript console.log in eclipse console?

Title says it all.标题说明了一切。 I am wondering if i can display javascript console.log in eclipse console rather than web browser's dev console?我想知道我是否可以在 Eclipse 控制台而不是 Web 浏览器的开发控制台中显示 javascript console.log

Just found an article regarding this.刚刚找到一篇关于这个的文章。

This is How it works(For Window 7).这就是它的工作原理(对于 Window 7)。

  1. Install Node.js javascript engine at Node.jsNode.js上安装 Node.js javascript 引擎
  2. Open your Eclipse, in the menu在菜单中打开 Eclipse

    Run->External Tools->External Tools Configuration运行->外部工具->外部工具配置

  3. Create new launch configuration under program category.在程序类别下创建新的启动配置。

  4. Set

    Location : C:\WINDOWS\system32\cmd.exe位置:C:\WINDOWS\system32\cmd.exe

    Working Directory : C:\WINDOWS\system32工作目录:C:\WINDOWS\system32

    Argument : /c "node ${resource_loc}"参数:/c "node ${resource_loc}"

  5. Now create new environment variable 'node' refers to node.exe file(wherever you installed)现在创建新的环境变量“节点”指的是 node.exe 文件(无论你安装在哪里)

All done.全部完成。

Redirect javascript console.logs, in Java console在 Java 控制台中重定向 javascript console.logs

Here is my solution to get javascript console messages in Java (with SWT browser)这是我在 Java 中获取 javascript 控制台消息的解决方案(使用 SWT 浏览器)

  1. create shell SWT and SWT browser see: Shell + Browser创建 shell SWT 和 SWT 浏览器参见:Shell + Browser
  2. create custom function SWT see: call Java from JavaScript创建自定义函数 SWT 请参阅:从 JavaScript 调用 Java
  3. Add listener on error events in javascript see: mdn event error在 javascript 中添加错误事件的侦听器, 请参阅:mdn 事件错误
  4. Override console object in javascript and call custom java function (2.)在 javascript 中覆盖控制台对象并调用自定义 java 函数 (2.)

Here is my example snippet:这是我的示例片段:

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.BrowserFunction;
import org.eclipse.swt.browser.LocationAdapter;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Snippet307d3 {

    public static final Shell createShell() {
        final var display = new Display();
        final var shell = new Shell(display);
        shell.setText("Snippet DEBUG");
        shell.setLayout(new FillLayout());
        shell.setBounds(10, 10, 300, 200);
        return shell;
    }

    public static final Browser createBrowser(Shell shell) {
        try {
            return new Browser(shell, SWT.NONE);
        } catch (final SWTError e) {
            System.out.println("Could not instantiate Browser: " + e.getMessage());
            shell.getDisplay().dispose();
            System.exit(-1);
            return null;
        }

    }

    public static final void runShell(Shell shell) {
        shell.open();
        final var display = shell.getDisplay();

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

        display.dispose();
    }

    public static void main(String[] args) {

        // -> Create shell
        final var shell = createShell();

        // -> Create browser
        final var browser = createBrowser(shell);
        browser.setJavascriptEnabled(true);

        // -> set HTML or use setUrl
        browser.setText(createHTML());
        // browser.setUrl(URL_DOCUMENT_HTML_TEST);

        // -> Create custom function
        final BrowserFunction function = new CustomFunction(browser, "theJavaFunctionDebugInEclipse");

        // -> Register function for cleanup
        browser.addProgressListener(ProgressListener.completedAdapter(event -> {
            browser.addLocationListener(new LocationAdapter() {
                @Override
                public void changed(LocationEvent event) {
                    browser.removeLocationListener(this);
                    System.out.println("left java function-aware page, so disposed CustomFunction");
                    function.dispose();
                }
            });
        }));

        // -> 6) Start shell
        runShell(shell);
    }

    private static class CustomFunction extends BrowserFunction {
        public CustomFunction(Browser browser, String name) {
            super(browser, name);
        }

        @Override
        public Object function(Object[] arguments) {
            for (final Object v : arguments)
                if (v != null)
                    System.out.println(v.toString());

            return new Object();
        }
    }

    private static String createHTML() {
        return """
                <!DOCTYPE>
                <html lang='en'>
                <head>
                <title>DEBUG SWT</title>
                <script>
                    const console = {
                        log : function(args) {
                            try {
                                theJavaFunctionDebugInEclipse('redirect > ' + args);
                            } catch (_e) {
                                return;
                            }
                        },

                        error : function(args) {
                            this.log(args);
                        },

                        exception : function(args) {
                            this.log(args);
                        },

                        debug : function(args) {
                            this.log(args);
                        },

                        trace : function(args) {
                            this.log(args);
                        },

                        info : function(args) {
                            this.log(args);
                        }
                    };

                    window.addEventListener('error', function(e) {
                        console.log(e.type + ' : ' + e.message);
                        console.log(e);
                    });
                </script>
                </head>
                <body>

                    <input id=button type='button' value='Push to Invoke Java'
                        onclick='function1();'>

                    <p>
                        <a href='http://www.eclipse.org'>go to eclipse.org</a>
                    </p>

                    <script>

                        // bad char sequence .. send error
                        eeeee

                        function function1() {
                            let result;

                            try {
                                // Call bad function java .. send log
                                result = badFunctionJava(12, false, null, [ 3.6,
                                        [ 'swt', true ] ], 'eclipse');
                            } catch (e) {
                                console.log('a error occurred: ' + e.message);
                                return;
                            }
                        }
                    </script>
                </body>
                </html>
                """;
    }

}

Further to @ringord's answer here , these would be the commands for your External Tools Configuration on Linux:除了@ringord's answer here ,这些将是您在 Linux 上的外部工具配置的命令:

  • Location : /home/<user>/.nvm/versions/node/<version>/bin/node (or wherever you installed node)位置:/home/<user>/. /home/<user>/.nvm/versions/node/<version>/bin/node /versions/node/<version>/bin/node(或安装节点的任何位置)
  • Working Directory : /home/<user>工作目录: /home/<user>
  • Arguments : ${container_loc}/${resource_name}参数: ${container_loc}/${resource_name}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM