Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if browser or CEF/Electron

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?

like image 536
alexandernst Avatar asked Nov 04 '25 05:11

alexandernst


1 Answers

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:

  • https://ourcodeworld.com/articles/read/173/how-to-use-cefsharp-chromium-embedded-framework-csharp-in-a-winforms-application
  • https://github.com/cefsharp/CefSharp/issues/2246
like image 54
Austin Salonen Avatar answered Nov 05 '25 19:11

Austin Salonen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!