Search in sources :

Example 1 with ConsoleMessage

use of com.teamdev.jxbrowser.js.ConsoleMessage in project JxBrowser-Examples by TeamDev-IP.

the class JsConsoleEvents method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("JS Console Listener");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.on(ConsoleMessageReceived.class, event -> {
        ConsoleMessage consoleMessage = event.consoleMessage();
        System.out.printf("Level: %s\nMessage: %s%n", consoleMessage.level().name(), consoleMessage.message());
    });
    browser.mainFrame().ifPresent(frame -> frame.executeJavaScript("console.error(\"Error message\");"));
}
Also used : JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) ConsoleMessage(com.teamdev.jxbrowser.js.ConsoleMessage) Browser(com.teamdev.jxbrowser.browser.Browser)

Aggregations

Browser (com.teamdev.jxbrowser.browser.Browser)1 Engine (com.teamdev.jxbrowser.engine.Engine)1 ConsoleMessage (com.teamdev.jxbrowser.js.ConsoleMessage)1 BrowserView (com.teamdev.jxbrowser.view.swing.BrowserView)1 JFrame (javax.swing.JFrame)1