Page 1 of 1

cef can't load url after CefRegisterExtension

PostPosted: Mon Aug 08, 2022 10:35 pm
by jiayun
Dear experts,

I work on a project, use CEF, load whiteboard url, when whiteboard url loaded, page will call window.whiteboard.postMessage, it need get handled from C++ code side, so I need inject js before page load.
So I do CefRegisterExtension in OnWebKitInitialized, code is:
const auto optimizationScript = R"(
window.whiteboard = window.whiteboard || {};
window.whiteboard.optimization = {
mode: "UCF",
};
)";
CefRegisterExtension("v8/optimizationScript", optimizationScript, new ClientV8ExtensionHandler(this));

const auto setupChannelScript = R"(
window.whiteboard = window.whiteboard || {};
(function() {
window.whiteboard.postMessage = function(jsonMsg) {
try {
window.postMessage(jsonMsg);
} catch (error) {
console.error('WinodwsBridge postMessage:', error);
}
};
})();
)";
CefRegisterExtension("v8/setupChannelScript", setupChannelScript, new ClientV8ExtensionHandler(this));
After I add above code, url can't be loaded, OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) can't be triggered.
If I remove above code, OnContextCreated will be triggered and url can load.
Please help advise, thanks.

Re: cef can't load url after CefRegisterExtension

PostPosted: Tue Aug 09, 2022 8:39 am
by magreenblatt
You can't access the "window" object when loading an extension.