|
| 1 | +/** |
| 2 | +* Adjusts options page for browser or system (mobile/Android) compatibility. |
| 3 | + * |
| 4 | + * Notice: You can include this asyncronously even when the whole DOM is not parsed yet. |
| 5 | + * It only accesses the body tag and that is very likely available as it's likely one of |
| 6 | + * the first HTML tags that is written and this script is obviously included afterwards |
| 7 | + * in the head tag. |
| 8 | + * This prevents unnecessary flackering when the CSS is added and the browser needs to |
| 9 | + * re-parse/render the HTML. |
| 10 | + * |
| 11 | + * @public |
| 12 | + * @module EnvironmentalOptions |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * Return the browser's browser info or fallback to an empty object. |
| 17 | + * |
| 18 | + * This is a workaround for Chrome/ium browsers that do not support runtime.getBrowserInfo(). |
| 19 | + * See https://github.com/TinyWebEx/AutomaticSettings/issues/18 |
| 20 | + * |
| 21 | + * @private |
| 22 | + * @returns {Promise<bool>} |
| 23 | + */ |
| 24 | +async function getBrowserInfo() { |
| 25 | + if (!browser.runtime.getBrowserInfo) { |
| 26 | + return {}; |
| 27 | + } |
| 28 | + |
| 29 | + return await browser.runtime.getBrowserInfo(); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Returns whether the current runtime is a mobile one (true) or not (false). |
| 34 | + * |
| 35 | + * @public |
| 36 | + * @returns {Promise<bool>} |
| 37 | + */ |
| 38 | + export async function isMobile() { |
| 39 | + const platformInfo = await browser.runtime.getPlatformInfo(); |
| 40 | + |
| 41 | + return platformInfo.os === "android"; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | +* Returns whether the current browser is from Mozilla. |
| 46 | +* |
| 47 | +* This includes Firefox and Thunderbird e.g. |
| 48 | +* |
| 49 | +* @public |
| 50 | +* @returns {Promise<bool>} |
| 51 | +*/ |
| 52 | +export async function isMozilla() { |
| 53 | + const browserInfo = await getBrowserInfo(); |
| 54 | + |
| 55 | + // Thunderbird is explicitly checked as a workaround as Thunderbird does not return the vendor correctly |
| 56 | + // see https://bugzilla.mozilla.org/show_bug.cgi?id=1702722 |
| 57 | + // and https://github.com/TinyWebEx/AutomaticSettings/issues/11 |
| 58 | + return browserInfo.vendor === "Mozilla" || browserInfo.name === "Thunderbird"; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | +* Returns whether the current browser is Firefox. |
| 63 | +* |
| 64 | +* This does not include Thunderbird! |
| 65 | +* |
| 66 | +* @public |
| 67 | +* @returns {Promise<bool>} |
| 68 | +*/ |
| 69 | +export async function isFirefox() { |
| 70 | + const browserInfo = await getBrowserInfo(); |
| 71 | + |
| 72 | + return browserInfo.name === "Firefox"; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Initalize this module. |
| 77 | + * |
| 78 | + * Currently this just adds a CSS class. |
| 79 | + * You can e.g. use this to disable all incompatible options on mobile devices. |
| 80 | + * |
| 81 | + * @public |
| 82 | + * @returns {Promise} |
| 83 | + */ |
| 84 | +export function init() { |
| 85 | + isMobile().then((isCurrentlyMobile) => { |
| 86 | + if (!isCurrentlyMobile) { |
| 87 | + return; |
| 88 | + } |
| 89 | + document.querySelector("body").classList.add("mobile"); |
| 90 | + }); |
| 91 | + isFirefox().then((isCurrentlyFirefox) => { |
| 92 | + if (!isCurrentlyFirefox) { |
| 93 | + return; |
| 94 | + } |
| 95 | + document.querySelector("body").classList.add("firefox"); |
| 96 | + }); |
| 97 | + isMozilla().then((isCurrentlyMozilla) => { |
| 98 | + if (!isCurrentlyMozilla) { |
| 99 | + return; |
| 100 | + } |
| 101 | + document.querySelector("body").classList.add("mozilla"); |
| 102 | + }); |
| 103 | + |
| 104 | + return Promise.all([isMobile, isFirefox]); |
| 105 | +} |
0 commit comments