Handle cases where platform info is not available (#597)
* Handle cases where platform info is not available * Safely return the correct os property
This commit is contained in:
parent
839e306cac
commit
a84f188b73
@ -32,17 +32,40 @@ class Environment {
|
||||
|
||||
async _loadEnvironmentInfo() {
|
||||
const browser = await this._getBrowser();
|
||||
const platform = await new Promise((resolve) => chrome.runtime.getPlatformInfo(resolve));
|
||||
const modifierInfo = this._getModifierInfo(browser, platform.os);
|
||||
const os = await this._getOperatingSystem();
|
||||
const modifierInfo = this._getModifierInfo(browser, os);
|
||||
return {
|
||||
browser,
|
||||
platform: {
|
||||
os: platform.os
|
||||
},
|
||||
platform: {os},
|
||||
modifiers: modifierInfo
|
||||
};
|
||||
}
|
||||
|
||||
async _getOperatingSystem() {
|
||||
try {
|
||||
const {os} = await this._getPlatformInfo();
|
||||
if (typeof os === 'string') {
|
||||
return os;
|
||||
}
|
||||
} catch (e) {
|
||||
// NOP
|
||||
}
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
_getPlatformInfo() {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.runtime.getPlatformInfo((result) => {
|
||||
const error = chrome.runtime.lastError;
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async _getBrowser() {
|
||||
if (EXTENSION_IS_BROWSER_EDGE) {
|
||||
return 'edge';
|
||||
@ -96,8 +119,15 @@ class Environment {
|
||||
['meta', 'Super']
|
||||
];
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Invalid OS: ${os}`);
|
||||
default: // 'unknown', etc
|
||||
separator = ' + ';
|
||||
osKeys = [
|
||||
['alt', 'Alt'],
|
||||
['ctrl', 'Ctrl'],
|
||||
['shift', 'Shift'],
|
||||
['meta', 'Meta']
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
const isFirefox = (browser === 'firefox' || browser === 'firefox-mobile');
|
||||
|
Loading…
Reference in New Issue
Block a user