How to create JBCefBrowser with CefRequestContextHandler?

Answered

Hello!
Faced the following problem:
I need to put some javascript in the page every time the content is loaded in the JBCefBrowser and org.cef.handler.CefRequestHandler#getResourceRequestHandler - helped me a lot with this.

Unfortunately, org.cef.handler.CefRequestHandler is useless in situations where Service Workers are used and we have to use org.cef.handler.CefRequestContextHandler as said in https://magpcss.org/ceforum/viewtopic.php?f=6&t=18520&p=49496&hilit=service+worker#p49496 .

So, the problem is - I am unable to find a way to create JBCefBrowser with custom CefRequestContextHandler...

As following from your API the most convenient way to create a JBCefBrowser is something like that:

com.intellij.ui.jcef.JBCefBrowser#create

But there are no parameters/methods to set/add custom CefRequestContextHandler.

Could you please advise how to proceed in this case?

Best regards, Andrew.

0
3 comments

Forwarded, please allow some time for answer. Thanks.

1

Hello! Unfortunately current api doesn't allow to create JBCefBrowser with custom CefRequestContextHandler. This possibility will be implemented in 23.1 (https://youtrack.jetbrains.com/issue/IDEA-305820).

The workaround is to create browser like this:

CefRequestContext context = ...;
JBCefClient client = JBCefApp.getInstance().createClient();
JComponent comp = JBCefOSRHandlerFactory.DEFAULT.createComponent();
CefRenderHandler handler = JBCefOSRHandlerFactory.DEFAULT.createCefRenderHandler(comp);
CefBrowserOsrWithHandler browser =
new CefBrowserOsrWithHandler(client.getCefClient(), "about:blank", context, handler, comp, null, null);
// Call via reflection (because JBCefOsrComponent isn't public):
((JBCefOsrComponent)comp).setBrowser(browser);
JBCefBrowserBuilder
builder = JBCefBrowser.createBuilder().setOffScreenRendering(true).setClient(client).setCefBrowser(browser);
JBCefBrowser.create(builder);
0

Thank you very much I will be waiting for 23.1 )

0

Please sign in to leave a comment.