5

Title says it all. I am wondering if i can display javascript console.log in eclipse console rather than web browser's dev console?

2

3 Answers 3

2

Redirect javascript console.logs, in Java console

Here is my solution to get javascript console messages in Java (with SWT browser)

  1. create shell SWT and SWT browser see: Shell + Browser
  2. create custom function SWT see: call Java from JavaScript
  3. Add listener on error events in javascript see: mdn event error
  4. Override console object in javascript and call custom java function (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>
                """;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

Just found an article regarding this.

This is How it works(For Window 7).

  1. Install Node.js javascript engine at Node.js
  2. Open your Eclipse, in the menu

    Run->External Tools->External Tools Configuration

  3. Create new launch configuration under program category.

  4. Set

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

    Working Directory : C:\WINDOWS\system32

    Argument : /c "node ${resource_loc}"

  5. Now create new environment variable 'node' refers to node.exe file(wherever you installed)

All done.

2 Comments

That's great, but link-only answers aren't useful on SO. If you could summarize the main points and link, that would be useful.
I see, i will fix it soon.
0

Further to @ringord's answer here, these would be the commands for your External Tools Configuration on Linux:

  • Location : /home/<user>/.nvm/versions/node/<version>/bin/node (or wherever you installed node)
  • Working Directory : /home/<user>
  • Arguments : ${container_loc}/${resource_name}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.