firefox-addonfirefox-addon-webextensionsfirefox-android

Detect firefox android in firefox addon?


Before Firefox Quantum, I can detect firefox android in firefox addon with this snippet below.

const { Cc, Ci } = require('chrome');
/**
  * Is firefox android?
  *
  * @returns {boolean} true if there is firefox android in firefox addon
  * @see https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/Code_snippets#Supporting_both_desktop_and_mobile
  */
module.exports = () =>
Cc['@mozilla.org/xre/app-info;1']
  .getService(Ci.nsIXULRuntime)
  .widgetToolkit.toLowerCase() === 'android';

But now, this snippet is archived. How to detect this?


Solution

  • Cc, Ci and require-syntax is XUL/SDK technology which has been deprecated in favour of WebExtensions.

    In a WebExtension, you can retrieve platform info using browser.runtime.getPlatformInfo:

    browser.runtime.getPlatformInfo().then((info) => {
        console.log(info.os); // will return android
    });
    

    This resolves into a PlatformInfo object, which contains the "os" property where "android" will be set. https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/PlatformOs

    For more details, see https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/getPlatformInfo