Search in sources :

Example 1 with Navigation

use of com.teamdev.jxbrowser.navigation.Navigation in project JxBrowser-Examples by TeamDev-IP.

the class XPath method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(OFF_SCREEN);
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Evaluate XPath");
        frame.getContentPane().add(view, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    Navigation navigation = browser.navigation();
    navigation.on(FrameLoadFinished.class, event -> event.frame().document().flatMap(Document::documentElement).ifPresent(element -> {
        try {
            XPathResult result = element.evaluate("count(//div)");
            if (result.isNumber()) {
                System.out.println("Result: " + result.asNumber());
            }
        } catch (XPathException e) {
            System.out.println(e.getMessage());
        }
    }));
    navigation.loadUrl("https://www.teamdev.com/jxbrowser");
}
Also used : XPathResult(com.teamdev.jxbrowser.dom.XPathResult) Navigation(com.teamdev.jxbrowser.navigation.Navigation) SwingUtilities(javax.swing.SwingUtilities) FrameLoadFinished(com.teamdev.jxbrowser.navigation.event.FrameLoadFinished) Document(com.teamdev.jxbrowser.dom.Document) OFF_SCREEN(com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN) Browser(com.teamdev.jxbrowser.browser.Browser) XPathException(com.teamdev.jxbrowser.dom.XPathException) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Navigation(com.teamdev.jxbrowser.navigation.Navigation) JFrame(javax.swing.JFrame) XPathException(com.teamdev.jxbrowser.dom.XPathException) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) XPathResult(com.teamdev.jxbrowser.dom.XPathResult) Document(com.teamdev.jxbrowser.dom.Document) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 2 with Navigation

use of com.teamdev.jxbrowser.navigation.Navigation in project JxBrowser-Examples by TeamDev-IP.

the class ZoomLevel 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("Change Zoom Level");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    // Listen to the zoom changed events.
    ZoomLevels levels = engine.zoomLevels();
    levels.on(ZoomLevelChanged.class, event -> System.out.println("Url: " + event.host() + "\n" + "Zoom level: " + event.level()));
    Navigation navigation = browser.navigation();
    navigation.on(FrameLoadFinished.class, event -> {
        if (event.frame().isMain()) {
            browser.zoom().level(com.teamdev.jxbrowser.zoom.ZoomLevel.P_200);
        }
    });
    navigation.loadUrl("https://www.google.com");
}
Also used : Navigation(com.teamdev.jxbrowser.navigation.Navigation) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) ZoomLevels(com.teamdev.jxbrowser.zoom.ZoomLevels) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 3 with Navigation

use of com.teamdev.jxbrowser.navigation.Navigation in project JxBrowser-Examples by TeamDev-IP.

the class GetHtml 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("Get HTML");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    });
    Navigation navigation = browser.navigation();
    // Add the callback for waiting till the page is loaded.
    navigation.on(FrameLoadFinished.class, event -> {
        if (event.frame().isMain()) {
            browser.mainFrame().ifPresent(frame -> System.out.println("HTML = " + frame.html()));
        }
    });
    navigation.loadUrl("https://www.teamdev.com/jxbrowser#features");
}
Also used : Navigation(com.teamdev.jxbrowser.navigation.Navigation) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Aggregations

Browser (com.teamdev.jxbrowser.browser.Browser)3 Engine (com.teamdev.jxbrowser.engine.Engine)3 Navigation (com.teamdev.jxbrowser.navigation.Navigation)3 BrowserView (com.teamdev.jxbrowser.view.swing.BrowserView)3 JFrame (javax.swing.JFrame)3 Document (com.teamdev.jxbrowser.dom.Document)1 XPathException (com.teamdev.jxbrowser.dom.XPathException)1 XPathResult (com.teamdev.jxbrowser.dom.XPathResult)1 OFF_SCREEN (com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN)1 FrameLoadFinished (com.teamdev.jxbrowser.navigation.event.FrameLoadFinished)1 ZoomLevels (com.teamdev.jxbrowser.zoom.ZoomLevels)1 BorderLayout (java.awt.BorderLayout)1 SwingUtilities (javax.swing.SwingUtilities)1