javascriptgoogle-chrome-extensionmozillafirefox-addon-webextensions

How to check if a Firefox WebExtension is installed or not with page JavaScript?


I have developed a WebExtension for Firefox and my website works with the extension as a prerequisite. I need to check programmatically whether the extension is installed or not and if not ask the user to install it.

I am not able to find a way how to check this operation of whether my extension is already installed in the user's browser.

Editor note: Methods available in Firefox differ from those available in Chrome, so this question is not a duplicate.


Solution

  • Important note to begin with: A page can't query if an extension is installed without explicit help from the extension. This is done to prevent browser fingerprinting and/or preventing sites from denying content if certain extensions are installed.

    WebExtensions are largely built upon the same principles as Chrome extensions. As such, this question is relevant: Check whether user has a Chrome extension installed.

    However, some of the best methods available in Chrome are currently unavailable in Firefox:

    The files will then be available using a URL like:

    moz-extension://<random-UUID>/<path/to/resource>
    

    This UUID is randomly generated for every browser instance and is not your extension's ID. This prevents websites from fingerprinting the extensions a user has installed.

    As such, what are your options? The page can't talk directly to the extension context (background), and the background can't directly affect the page; you need a Content script to interact with the page content.

    How can page code and a content script communicate? They are isolated from each other unless content script does something about it.

    First off, generic tricks that work in both FF and Chrome:

    There are interesting Firefox-specific approaches as well: