Can CefBrowser.loadString execute Javascript?

Having problems with building or using the JCEF Java binding? Ask your questions here.

Can CefBrowser.loadString execute Javascript?

Postby expleeve » Wed Jun 17, 2015 5:47 am

Now, I use jcef in my application. My application generates a html string, I use CefBrowser.loadString to evaluate it.
The problem is :
When the html content is simple, jcef works fine;
But when html content contains some js or css in my local file system, my application doesn't work.
Only simple html elements can be shown...

My code as below:
StringBuilder sb = new StringBuilder();
sb.append("<html>\n");
sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"C:/Users/Administrator/Desktop/test/d3.parcoords.css\">\n");
sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"C:/Users/Administrator/Desktop/test/style.css\">\n");
sb.append("<script src=\"C:/Users/Administrator/Desktop/test/d3.min.js\"></script>\n");
sb.append("<script src=\"C:/Users/Administrator/Desktop/test/d3.svg.multibrush.js\"></script>\n");
sb.append("<script src=\"C:/Users/Administrator/Desktop/test/sylvester.js\"></script>\n");
sb.append("<script src=\"C:/Users/Administrator/Desktop/test/d3.parcoords.js\"></script>\n");
sb.append("<body style=\"background-color:#80ff00\">\n");
sb.append("<h3>test1</h3><div id=\"contentPlay\" class=\"parcoords\" style=\"width:400px;height:350px;\"></div>\n");
sb.append("\n");
sb.append("<script>\n");
sb.append("var parcoords = d3.parcoords()(\"#contentPlay\")\n");
sb.append(" .height(300)\n");
sb.append(" .width(400)\n");
sb.append(" .data([{\"A\":\"1\",\"B\":\"2\",\"C\":\"3\"},{\"A\":\"1\",\"B\":\"2\",\"C\":\"2\"},{\"A\":\"3\",\"B\":\"2\",\"C\":\"1\"}])\n");
sb.append(" .smoothness(0.25)\n");
sb.append(" .showControlPoints(false)\n");
sb.append(" .render()\n");
sb.append(" .brushMode(\"1D-axes\")\n");
sb.append(" .interactive(); \n");
sb.append("</script>\n");
sb.append("</body>\n");
sb.append("</html>\n");
browser_.loadString(sb.toString(), "file:///C:/");


I don't know whether I was using in wrong way. How can I deal with it?

Another question:
After create a CefBrowser, It doesn't work if CefBrowser.loadString as follow; Using in SwingUtilities.invokeLater and check isLoading,it also doesn't work.
I have to add loadString method in a CefLoadHandlerAdapter.onLoadingStateChange, replace the html content every time.
Is there any offical solutions?

Thanks very much.
expleeve
Newbie
 
Posts: 2
Joined: Wed Jun 17, 2015 5:17 am

Re: Can CefBrowser.loadString execute Javascript?

Postby expleeve » Wed Jun 24, 2015 2:28 am

no reply...
I have a solution as below. An example in tests.detailed.MainFrame.

1/ add a custom CefAppHandlerAdapter to CefApp by CefApp.addAppHandler(T extends CefAppHandlerAdapter)
2/ override CefAppHandlerAdapter.onRegisterCustomSchemes, add a scheme name by registrar.addCustomScheme
3/ override CefAppHandlerAdapter.onContextInitialized, using cefApp.registerSchemeHandlerFactory, map your sheme name and domain name to a CefSchemeHandlerFactory
4/ The most important step is when invoking CefSchemeHandlerFactory.create, return a custom CefResourceHandlerAdapter
5/ see ClientSchemeHandler as an example, replace its html in processRequest every time.
6/ When some js or css files in your html content, loadContent should be modified.
expleeve
Newbie
 
Posts: 2
Joined: Wed Jun 17, 2015 5:17 am


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 121 guests