google-chromegoogle-chrome-extensionchromiumbrowser-feature-detection

How to detect whether browser supports chrome extensions


I need to test whether a web page is loaded in a browser which supports Chrome extensions installed from the Chrome Web Store.
* Google Chrome and MS Edge should return a positive result.
* Chrome for Android should return a negative result.

Testing for the existence of window.chrome is not good enough. Chrome for Android, for example, will expose window.chrome but it does not support Chrome extensions.
I want to avoid user-agent sniffing and media queries if possible, since those methods are prone for errors.

Is there a javascript API I can use to test for extension support?
Is testing for window.chrome.runtime good enough?


Solution

  • Chrome 105 and older

    If your site is https you can test for the presence of window.chrome.runtime.sendMessage - it's exposed in Chromium on all https pages by the built-in browser extension for CryptoToken authorization support.

    P.S. there are chromium forks that can run extensions on Android e.g. Kiwi, Yandex.

    Chrome 106 and newer

    There's no reliable method.

    Until crbug/1370496 is fixed you'll have to infer it from navigator.userAgent. You can also check the web page for presence of artifacts from known extensions e.g. Stylus extension adds DOM elements to document.documentElement that match style.stylus[id^="stylus-"].