Since the same code will be running on Desktop AND Android version, sometimes I need to run platform specific code.
Is there some “synchronous” easy way to tell I’m running on Android?
Currently I’m using this ugly monstrosity which I would like to replace with something nicer:
// NOTE: platform info is not available from content scripts!
export let isAndroid = false;
// currently only Firefox can be Android
export const isAndroidPromise = $IS_FIREFOX && !$IS_THUNDERBIRD && browser.runtime.getPlatformInfo ?
browser.runtime.getPlatformInfo().then(({os}) => isAndroid = os === 'android') :
Promise.resolve(false);
This is some serious out of box thinking , I was not expecting CSS to come to save the day!
But I’m slightly worried though. Isn’t there some Windows touch tablet that would match that query?
Also, from the “long term” point of view, this won’t work in service workers.
It’s great though that it works in content scripts!
EDIT:
Maybe we could ask for a new API, something like browser.extension.inAndroidContext, similar to:
Hm… I’m a bit hesitant about a inAndroidContext property since it bakes the concept of the host OS into the variable name. Something like inMobileContext has more promise, but it’s hard to determine what the line between true and false would mean here. Is an Android tablet a mobile device? Is an Android laptop? What about Android apps running on Windows machines or Chromebooks?
So, what I’m actually looking for is not a inMobileContext, since screen size can be solved with CSS.
What I’m looking for is a “browser detection”. Since Firefox for Android is a different browser with limited API and restricted behavior that sometimes needs special handling.
For other browsers I’m using special “global variables” that are replaced during the build with my Webpack (since I’m submitting a different build for each store).
But for Android I don’t have that and using Promise is not always possible (like those “user-actions” handlers).
This really feels like a trivial thing that should be there somewhere. I mean, the source code is submitted to the single store and the store will push it to two browsers, but the source code can’t tell which one without resolving a promise first?
The feature detection can be OK for some (simple) cases, but not all.
// First detect OS with top level await
const { os } = await browser.runtime.getPlatformInfo();
const isAndroid = os === 'android';
// Do other stuff afterwards
(async () => {
console.log(isAndroid);
})();
However, this looks like it will break the “top level event loop, event handlers registration requirement” for the non-persistent background scripts and service workers.
But I don’t have answers to all of your questions. It would be a good idea to test your extension with IS_MOBILE inverted, to make sure nothing is catastrophically broken. I’m only using the value for minor UI tweaks.