Search in sources :

Example 1 with OFF_SCREEN

use of com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN in project JxBrowser-Examples by TeamDev-IP.

the class DomGetAttributes method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(OFF_SCREEN);
    Browser browser = engine.newBrowser();
    browser.mainFrame().ifPresent(mainFrame -> {
        mainFrame.loadHtml("<html><body><a href='#' id='link' title='link title'>Link</a></body></html>");
    });
    browser.mainFrame().flatMap(Frame::document).flatMap(Document::documentElement).flatMap(element -> element.findElementById("link")).ifPresent(linkElement -> linkElement.attributes().forEach(DomGetAttributes::print));
}
Also used : Document(com.teamdev.jxbrowser.dom.Document) OFF_SCREEN(com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN) Browser(com.teamdev.jxbrowser.browser.Browser) Engine(com.teamdev.jxbrowser.engine.Engine) Frame(com.teamdev.jxbrowser.frame.Frame) Document(com.teamdev.jxbrowser.dom.Document) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 2 with OFF_SCREEN

use of com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN in project JxBrowser-Examples by TeamDev-IP.

the class JsFunctionCallback method main.

public static void main(String[] args) {
    try (Engine engine = Engine.newInstance(OFF_SCREEN)) {
        Browser browser = engine.newBrowser();
        browser.mainFrame().ifPresent(frame -> {
            JsObject jsObject = frame.executeJavaScript("window");
            if (jsObject != null) {
                // Inject JsFunctionCallback into JavaScript and associate it
                // with the "window.sayHelloTo" JavaScript property.
                jsObject.putProperty("sayHelloTo", (com.teamdev.jxbrowser.js.JsFunctionCallback) arguments -> "Hello, " + arguments[0]);
            }
            String greetings = frame.executeJavaScript("window.sayHelloTo('John')");
            System.out.println(greetings);
        });
    }
}
Also used : OFF_SCREEN(com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN) Browser(com.teamdev.jxbrowser.browser.Browser) JsObject(com.teamdev.jxbrowser.js.JsObject) Engine(com.teamdev.jxbrowser.engine.Engine) JsObject(com.teamdev.jxbrowser.js.JsObject) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 3 with OFF_SCREEN

use of com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN 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)

Aggregations

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