Example usage for org.eclipse.swt.browser Browser execute

List of usage examples for org.eclipse.swt.browser Browser execute

Introduction

In this page you can find the example usage for org.eclipse.swt.browser Browser execute.

Prototype

public boolean execute(String script) 

Source Link

Document

Execute the specified script.

Usage

From source file:Snippet160.java

public static void main(String[] args) {
    final String html = "<html><title>Snippet</title><body><p id='myid'>Best Friends</p><p id='myid2'>Cat and Dog</p></body></html>";
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Browser browser = new Browser(shell, SWT.NONE);
    browser.addStatusTextListener(new StatusTextListener() {
        public void changed(StatusTextEvent event) {
            browser.setData("query", event.text);
        }/* w  w  w.j  a v  a  2  s . c o  m*/
    });
    browser.addProgressListener(new ProgressListener() {
        public void changed(ProgressEvent event) {
        }

        public void completed(ProgressEvent event) {
            /*
             * Use JavaScript to query the desired node content through the
             * Document Object Model
             * 
             * Assign result to the window property status to pass the
             * result to the StatusTextListener This trick is required since
             * <code>execute</code> does not return the <code>String</code>
             * directly.
             */
            boolean result = browser
                    .execute("window.status=document.getElementById('myid').childNodes[0].nodeValue;");
            if (!result) {
                /*
                 * Script may fail or may not be supported on certain
                 * platforms.
                 */
                System.out.println("Script was not executed.");
                return;
            }
            String value = (String) browser.getData("query");
            System.out.println("Node value: " + value);
        }
    });
    /* Load an HTML document */
    browser.setText(html);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet161.java

public static void main(String[] args) {
    final String html = "<html><title>Snippet</title><body><p id='myid'>Best Friends</p><p id='myid2'>Cat and Dog</p></body></html>";
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 161");
    shell.setLayout(new FillLayout());
    final Browser browser;
    try {//from w w w  . ja  va 2s .c o m
        browser = new Browser(shell, SWT.BORDER);
    } catch (SWTError e) {
        System.out.println("Could not instantiate Browser: " + e.getMessage());
        display.dispose();
        return;
    }
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout(SWT.VERTICAL));
    final Text text = new Text(comp, SWT.MULTI);
    text.setText("var newNode = document.createElement('P'); \r\n"
            + "var text = document.createTextNode('At least when I am around');\r\n"
            + "newNode.appendChild(text);\r\n" + "document.getElementById('myid').appendChild(newNode);\r\n"
            + "\r\n" + "document.bgColor='yellow';");
    final Button button = new Button(comp, SWT.PUSH);
    button.setText("Execute Script");
    button.addListener(SWT.Selection, event -> {
        boolean result = browser.execute(text.getText());
        if (!result) {
            /* Script may fail or may not be supported on certain platforms. */
            System.out.println("Script was not executed.");
        }
    });
    browser.setText(html);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}