Search in sources :

Example 1 with JsObject

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

the class ContentListening method main.

public static void main(String[] args) {
    // #docfragment "engine-creation"
    Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).build());
    Browser browser = engine.newBrowser();
    // #enddocfragment "engine-creation"
    // #docfragment "embed-browser-view"
    SwingUtilities.invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Content Listening");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    // #enddocfragment "embed-browser-view"
    // #docfragment "inject-js"
    browser.set(InjectJsCallback.class, params -> {
        Frame frame = params.frame();
        String window = "window";
        JsObject jsObject = frame.executeJavaScript(window);
        if (jsObject == null) {
            throw new IllegalStateException(format("'%s' JS object not found", window));
        }
        jsObject.putProperty("java", new JavaObject());
        return Response.proceed();
    });
    // #enddocfragment "inject-js"
    // #docfragment "frame-load-finished"
    browser.navigation().on(FrameLoadFinished.class, event -> {
        String javaScript = load("observer.js");
        event.frame().executeJavaScript(javaScript);
    });
    // #enddocfragment "frame-load-finished"
    // #docfragment "load-page"
    String html = load("index.html");
    String base64Html = Base64.getEncoder().encodeToString(html.getBytes(UTF_8));
    String dataUrl = "data:text/html;base64," + base64Html;
    browser.navigation().loadUrl(dataUrl);
// #enddocfragment "load-page"
}
Also used : JsObject(com.teamdev.jxbrowser.js.JsObject) Frame(com.teamdev.jxbrowser.frame.Frame) JFrame(javax.swing.JFrame) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 2 with JsObject

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

the class JsAccessibleClass 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 Java object into JavaScript and associate it
                // with the "window.java" JavaScript property.
                jsObject.putProperty("java", new JavaObject());
            }
            // Call the public method of the injected Java object from JS.
            frame.executeJavaScript("window.java.sayHelloTo('John')");
        });
    }
}
Also used : JsObject(com.teamdev.jxbrowser.js.JsObject) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 3 with JsObject

use of com.teamdev.jxbrowser.js.JsObject 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 4 with JsObject

use of com.teamdev.jxbrowser.js.JsObject in project convertigo by convertigo.

the class ApplicationComponentEditor method createBrowser.

private void createBrowser(Composite parent) {
    browserScroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
    browserScroll.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    browserScroll.setExpandHorizontal(true);
    browserScroll.setExpandVertical(true);
    Composite canvas = new Composite(browserScroll, SWT.NONE);
    browserScroll.setContent(canvas);
    GridLayout gl = new GridLayout(1, false);
    gl.marginBottom = gl.marginTop = gl.marginLeft = gl.marginRight = gl.marginHeight = gl.marginWidth = gl.horizontalSpacing = gl.verticalSpacing = 0;
    canvas.setLayout(gl);
    c8oBrowser = new C8oBrowser(canvas, SWT.NONE, applicationEditorInput.application.getProject());
    browserGD = new GridData(SWT.CENTER, SWT.CENTER, true, true);
    c8oBrowser.setLayoutData(browserGD);
    browser = c8oBrowser.getBrowser();
    debugUrl = c8oBrowser.getDebugUrl();
    browserInterface = new ApplicationComponentBrowserImpl();
    browser.set(InjectJsCallback.class, params -> {
        String url = params.frame().browser().url();
        if (baseUrl != null && url.startsWith(baseUrl)) {
            try {
                Frame frame = params.frame();
                JsObject sessionStorage = frame.executeJavaScript("sessionStorage");
                frame.executeJavaScript("sessionStorage.setItem('_c8ocafsession_storage_mode', 'session');\n" + "navigator.__defineGetter__('userAgent', function(){ return '" + deviceOS.agent() + "'});\n" + IOUtils.toString(getClass().getResourceAsStream("inject.js"), "UTF-8"));
                sessionStorage.call("setItem", "_c8ocafsession_storage_mode", "session");
                if (!dataset.equals("none")) {
                    String json = FileUtils.readFileToString(new File(datasetDir, dataset + ".json"), "UTF-8");
                    sessionStorage.call("setItem", "_c8ocafsession_storage_data", json);
                } else {
                    sessionStorage.call("setItem", "_c8ocafsession_storage_data", null);
                }
                JsObject window = frame.executeJavaScript("window");
                window.putProperty("java", browserInterface);
            } catch (Exception e) {
                Engine.logStudio.info("onScriptContextCreate failed for '" + url + "' with baseUrl '" + baseUrl + "': " + e.getMessage());
            }
        }
        // browser.setZoomLevel(zoomFactor.zoomLevel());
        return Response.proceed();
    });
    browser.set(ShowContextMenuCallback.class, (params, tell) -> {
        com.teamdev.jxbrowser.ui.Point location = params.location();
        highlightPoint(location.x(), location.y());
        tell.close();
    });
    browser.engine().permissions().set(RequestPermissionCallback.class, (params, tell) -> {
        tell.grant();
    });
}
Also used : JsObject(com.teamdev.jxbrowser.js.JsObject) Frame(com.teamdev.jxbrowser.frame.Frame) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) C8oBrowser(com.twinsoft.convertigo.eclipse.swt.C8oBrowser) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) DatabaseObjectFoundException(com.twinsoft.convertigo.engine.DatabaseObjectFoundException) JSONException(org.codehaus.jettison.json.JSONException) GridLayout(org.eclipse.swt.layout.GridLayout) GridData(org.eclipse.swt.layout.GridData) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) File(java.io.File)

Example 5 with JsObject

use of com.teamdev.jxbrowser.js.JsObject in project convertigo by convertigo.

the class ApplicationComponentEditor method createBrowser.

private void createBrowser(Composite parent) {
    browserScroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
    browserScroll.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    browserScroll.setExpandHorizontal(true);
    browserScroll.setExpandVertical(true);
    Composite canvas = new Composite(browserScroll, SWT.NONE);
    browserScroll.setContent(canvas);
    GridLayout gl = new GridLayout(1, false);
    gl.marginBottom = gl.marginTop = gl.marginLeft = gl.marginRight = gl.marginHeight = gl.marginWidth = gl.horizontalSpacing = gl.verticalSpacing = 0;
    canvas.setLayout(gl);
    c8oBrowser = new C8oBrowser(canvas, SWT.NONE, applicationEditorInput.application.getProject());
    browserGD = new GridData(SWT.CENTER, SWT.CENTER, true, true);
    c8oBrowser.setLayoutData(browserGD);
    browser = c8oBrowser.getBrowser();
    debugUrl = c8oBrowser.getDebugUrl();
    browserInterface = new ApplicationComponentBrowserImpl();
    browser.set(InjectJsCallback.class, params -> {
        String url = params.frame().browser().url();
        if (baseUrl != null && url.startsWith(baseUrl)) {
            try {
                Frame frame = params.frame();
                JsObject sessionStorage = frame.executeJavaScript("sessionStorage");
                frame.executeJavaScript("sessionStorage.setItem('_c8ocafsession_storage_mode', 'session');\n" + "navigator.__defineGetter__('userAgent', function(){ return '" + deviceOS.agent() + "'});\n" + IOUtils.toString(getClass().getResourceAsStream("inject.js"), "UTF-8"));
                sessionStorage.call("setItem", "_c8ocafsession_storage_mode", "session");
                if (!dataset.equals("none")) {
                    String json = FileUtils.readFileToString(new File(datasetDir, dataset + ".json"), "UTF-8");
                    sessionStorage.call("setItem", "_c8ocafsession_storage_data", json);
                } else {
                    sessionStorage.call("setItem", "_c8ocafsession_storage_data", null);
                }
                JsObject window = frame.executeJavaScript("window");
                window.putProperty("java", browserInterface);
            } catch (Exception e) {
                Engine.logStudio.info("onScriptContextCreate failed for '" + url + "' with baseUrl '" + baseUrl + "': " + e.getMessage());
            }
        }
        // browser.setZoomLevel(zoomFactor.zoomLevel());
        return Response.proceed();
    });
    browser.set(ShowContextMenuCallback.class, (params, tell) -> {
        com.teamdev.jxbrowser.ui.Point location = params.location();
        highlightPoint(location.x(), location.y());
        tell.close();
    });
    browser.engine().permissions().set(RequestPermissionCallback.class, (params, tell) -> {
        tell.grant();
    });
}
Also used : JsObject(com.teamdev.jxbrowser.js.JsObject) Frame(com.teamdev.jxbrowser.frame.Frame) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) C8oBrowser(com.twinsoft.convertigo.eclipse.swt.C8oBrowser) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) DatabaseObjectFoundException(com.twinsoft.convertigo.engine.DatabaseObjectFoundException) JSONException(org.codehaus.jettison.json.JSONException) GridLayout(org.eclipse.swt.layout.GridLayout) GridData(org.eclipse.swt.layout.GridData) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) File(java.io.File)

Aggregations

JsObject (com.teamdev.jxbrowser.js.JsObject)6 Browser (com.teamdev.jxbrowser.browser.Browser)4 Engine (com.teamdev.jxbrowser.engine.Engine)4 Frame (com.teamdev.jxbrowser.frame.Frame)3 C8oBrowser (com.twinsoft.convertigo.eclipse.swt.C8oBrowser)2 DatabaseObjectFoundException (com.twinsoft.convertigo.engine.DatabaseObjectFoundException)2 File (java.io.File)2 JSONException (org.codehaus.jettison.json.JSONException)2 CoreException (org.eclipse.core.runtime.CoreException)2 ScrolledComposite (org.eclipse.swt.custom.ScrolledComposite)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 Composite (org.eclipse.swt.widgets.Composite)2 PartInitException (org.eclipse.ui.PartInitException)2 OFF_SCREEN (com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN)1 BrowserView (com.teamdev.jxbrowser.view.swing.BrowserView)1 JFrame (javax.swing.JFrame)1