简体   繁体   中英

How to wait till SWT browser finishes loading?

I am using a SWT browser in my application. I need to run a script on HTML page in the browser. But the script is being run before the page is completely loaded. So how do i make the app wait till the browser finishes loading. I have tried something like this.

completed = true;
browser.addProgressListener(new ProgressListener() {
    @Override
    public void completed(ProgressEvent event) {
        completed = true; //say this is a global variable 
    }

    @Override
    public void changed(ProgressEvent event) {
        completed = false;
        System.out.println("Page changing");

    }
});

//some other method
void m1() 
{
    browser.setText("blah blah blah");
    while (completed == false) 
    {}
    // EXECUTE THE SCRIPT NOW
}

But this does not work!

This is similar to Java SWT browser: Waiting till dynamic page is fully loaded but there is no solution.

You can define a BrowserFunction and call it from the JavaScript code:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Browser browser = new Browser(shell, SWT.NONE);

    new CustomFunction(browser, "theJavaFunction");

    browser.setText("<style>#map { width: 100%; height: 300px; }</style><script src='http://maps.google.com/maps/api/js?sensor=false'></script><div id='map'></div><script>var map;function initialize() {  var mapOptions = { zoom: 8, center: new google.maps.LatLng(-34.397, 150.644) }; map = new google.maps.Map(document.getElementById('map'), mapOptions);} google.maps.event.addDomListener(window, 'load', initialize);window.onload = function () { theJavaFunction(); };</script>");
    shell.pack();
    shell.setSize(600, 400);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

private static class CustomFunction extends BrowserFunction
{
    CustomFunction(Browser browser, String name)
    {
        super(browser, name);
    }

    @Override
    public Object function(Object[] arguments)
    {
        System.out.println("theJavaFunction() called from javascript");
        return null;
    }
}

Baz gives the direction to the correct answer. I will attempt to put his answer in your context:

private boolean loading;    // Instance variable

createBrowserControl() {    // Browser widget definition method
    Browser b = ...;    // Create the browser widget
    browser.addProgressListener(new ProgressListener() {

        @Override
        public void completed(ProgressEvent event) {
            loading = false;
        }

        @Override
        public void changed(ProgressEvent event) {
            loading = true;
        }
    });
}

public boolean loadPage(String url) {
    Display display = ...   // Get the display from the UI or the widget
    boolean set = browser.setUrl(url);    // URL content loading is asynchronous
    loading = true;
    while(loading) {    // Add synchronous behavior: wait till it finishes loading
        if(!display.readAndDispatch()) {
            display.sleep();
        }
    }
    return set;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM