Is there a way I can detect if my JS code is running inside a real browser or inside a toolkit like CEF or Electron?
If you have control over the app hosting the CEF control you can register an object to be bound, like below:
chromeBrowser.JavascriptObjectRepository.Register("boundAsync", new CefCustomObject(chromeBrowser), true);
CefCustomObject class:
class CefCustomObject
{
// Declare a local instance of chromium and the main form in order to execute things from here in the main thread
private static ChromiumWebBrowser _instanceBrowser = null;
public CefCustomObject(ChromiumWebBrowser originalBrowser)
{
_instanceBrowser = originalBrowser;
}
}
You can then can check for the existance of this object in the web app. javascript in web app:
(async function()
{
if ("CefSharp" in window) {
await CefSharp.BindObjectAsync("boundAsync");
if (boundAsync) {
alert('running inside CEF')
}
}
})();
Sources:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With