Journey icon indicating copy to clipboard operation
Journey copied to clipboard

How to use CefBrowser

Open ara2am opened this issue 4 years ago • 2 comments

Version: 0.3.3-online JDK version: jdk1.8.0_231 Os and IDE of development: Win10 x64, Intelij IDEA

I want to load url, set user agent and set some simple listeners to cef. But the simple code like :

JourneyBrowserView browser = new JourneyBrowserView ();
browser.getCefBrowser ().loadURL ("https://www.google.com");

Does nothing.

SwingUtilities.invokeLater (() ->
{
    browser.getCefBrowser ().loadURL ("https://www.google.com");
});

Doesn't work too.

But this code works: JourneyBrowserView browser = new JourneyBrowserView ("https://www.google.com");

So, is it my blame or ...7

ara2am avatar Apr 10 '20 07:04 ara2am

I'd recommend just setting the initial URL for now. If you are going to set the URL after loading the browser it has to be done after the browser has been created (see here).

Unfortunately, on Windows, you will run into an issue adding the CefLifeSpanHandler which is #13. I'll work on getting a fix for this in 0.4.1 as well but in the meantime you can remove the handler and re-add it manually, like so:

JourneyBrowserView browser = new JourneyBrowserView();
browser.getCefClient().removeLifeSpanHandler();
browser.getCefClient().addLifeSpanHandler(CefLifeSpanHandlerProxy.createHandler(new CefLifeSpanHandlerProxy() {
    @Override
    public boolean onBeforePopup(CefBrowserProxy browser, CefFrameProxy frame, String targetUrl, String targetFrameName) {
        return false;
    }

    @Override
    public void onAfterCreated(CefBrowserProxy browser) {
        SwingUtilities.invokeLater(() -> browser.loadURL("http://google.com"));
    }

    @Override
    public void onAfterParentChanged(CefBrowserProxy browser) {
    }

    @Override
    public boolean doClose(CefBrowserProxy browser) {
        return false;
    }

    @Override
    public void onBeforeClose(CefBrowserProxy browser) {
    }
}));

If you run into the issue on #13 you'll need to manually set the zoom level.

BFergerson avatar Apr 10 '20 07:04 BFergerson

@BFergerson thanks, it works.

Sorry for another question, but is there any way to add listeners for events like onConsoleMessage, onPageStarted/onProgressChanged/onPageFinished in 0.3.3 ?

ara2am avatar Apr 10 '20 08:04 ara2am