Skip to content

Commit d65ec42

Browse files
committed
fix: improve platform detection
1 parent 76f60b6 commit d65ec42

File tree

4 files changed

+71
-31
lines changed

4 files changed

+71
-31
lines changed

src/assets/locales/en/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
"description": "Title of the option."
272272
},
273273

274-
"optionTitle_WebCite": {
274+
"optionTitle_webcite": {
275275
"message": "WebCite",
276276
"description": "Title of the option."
277277
},

src/background/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ async function processMessage(request, sender) {
797797
type: request.type
798798
});
799799
} else if (request.id === 'getPlatform') {
800-
return getPlatform({fallback: false});
800+
return getPlatform();
801801
} else if (request.id === 'storageRequest') {
802802
const data = await registry.getStorageItem({
803803
storageId: request.storageId,

src/storage/storage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async function isStorageArea({area = 'local'} = {}) {
99
}
1010
}
1111

12-
const storageReady = {};
12+
const storageReady = {session: true};
1313
async function isStorageReady({area = 'local'} = {}) {
1414
if (storageReady[area]) {
1515
return true;

src/utils/common.js

+68-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {v4 as uuidv4} from 'uuid';
22

3+
import {isStorageArea} from 'storage/storage';
4+
import storage from 'storage/storage';
35
import {targetEnv} from 'utils/config';
46

57
function getText(messageName, substitutions) {
@@ -224,41 +226,71 @@ async function getActiveTab() {
224226
return tab;
225227
}
226228

227-
async function getPlatform({fallback = true} = {}) {
228-
let os, arch;
229+
let platformInfo;
230+
async function getPlatformInfo() {
231+
if (platformInfo) {
232+
return platformInfo;
233+
}
234+
235+
const isSessionStorage = await isStorageArea({area: 'session'});
229236

230-
if (targetEnv === 'samsung') {
231-
// Samsung Internet 13: runtime.getPlatformInfo fails.
232-
os = 'android';
233-
arch = '';
237+
if (isSessionStorage) {
238+
({platformInfo} = await storage.get('platformInfo', {area: 'session'}));
234239
} else {
235240
try {
241+
platformInfo = JSON.parse(window.sessionStorage.getItem('platformInfo'));
242+
} catch (err) {}
243+
}
244+
245+
if (!platformInfo) {
246+
let os, arch;
247+
248+
if (targetEnv === 'samsung') {
249+
// Samsung Internet 13: runtime.getPlatformInfo fails.
250+
os = 'android';
251+
arch = '';
252+
} else if (targetEnv === 'safari') {
253+
// Safari: runtime.getPlatformInfo returns 'ios' on iPadOS.
254+
({os, arch} = await browser.runtime.sendNativeMessage('application.id', {
255+
id: 'getPlatformInfo'
256+
}));
257+
} else {
236258
({os, arch} = await browser.runtime.getPlatformInfo());
237-
} catch (err) {
238-
if (fallback) {
239-
({os, arch} = await browser.runtime.sendMessage({id: 'getPlatform'}));
240-
} else {
241-
throw err;
242-
}
259+
}
260+
261+
platformInfo = {os, arch};
262+
263+
if (isSessionStorage) {
264+
await storage.set({platformInfo}, {area: 'session'});
265+
} else {
266+
try {
267+
window.sessionStorage.setItem(
268+
'platformInfo',
269+
JSON.stringify(platformInfo)
270+
);
271+
} catch (err) {}
243272
}
244273
}
245274

275+
return platformInfo;
276+
}
277+
278+
async function getPlatform() {
279+
if (!isBackgroundPageContext()) {
280+
return browser.runtime.sendMessage({id: 'getPlatform'});
281+
}
282+
283+
let {os, arch} = await getPlatformInfo();
284+
246285
if (os === 'win') {
247286
os = 'windows';
248287
} else if (os === 'mac') {
249288
os = 'macos';
250289
}
251290

252-
if (
253-
navigator.platform === 'MacIntel' &&
254-
(os === 'ios' || typeof navigator.standalone !== 'undefined')
255-
) {
256-
os = 'ipados';
257-
}
258-
259-
if (arch === 'x86-32') {
291+
if (['x86-32', 'i386'].includes(arch)) {
260292
arch = '386';
261-
} else if (arch === 'x86-64') {
293+
} else if (['x86-64', 'x86_64'].includes(arch)) {
262294
arch = 'amd64';
263295
} else if (arch.startsWith('arm')) {
264296
arch = 'arm';
@@ -274,11 +306,13 @@ async function getPlatform({fallback = true} = {}) {
274306
const isMobile = ['android', 'ios', 'ipados'].includes(os);
275307

276308
const isChrome = targetEnv === 'chrome';
277-
const isEdge = targetEnv === 'edge';
309+
const isEdge =
310+
['chrome', 'edge'].includes(targetEnv) &&
311+
/\sedg(?:e|a|ios)?\//i.test(navigator.userAgent);
278312
const isFirefox = targetEnv === 'firefox';
279313
const isOpera =
280314
['chrome', 'opera'].includes(targetEnv) &&
281-
/ opr\//i.test(navigator.userAgent);
315+
/\sopr\//i.test(navigator.userAgent);
282316
const isSafari = targetEnv === 'safari';
283317
const isSamsung = targetEnv === 'samsung';
284318

@@ -303,13 +337,11 @@ async function getPlatform({fallback = true} = {}) {
303337
}
304338

305339
async function isAndroid() {
306-
const {os} = await getPlatform();
307-
return os === 'android';
340+
return (await getPlatform()).isAndroid;
308341
}
309342

310343
async function isMobile() {
311-
const {os} = await getPlatform();
312-
return ['android', 'ios', 'ipados'].includes(os);
344+
return (await getPlatform()).isMobile;
313345
}
314346

315347
function getDarkColorSchemeQuery() {
@@ -441,6 +473,13 @@ async function isValidTab({tab, tabId = null} = {}) {
441473
}
442474
}
443475

476+
function isBackgroundPageContext() {
477+
return (
478+
window.location.href ===
479+
browser.runtime.getURL('/src/background/index.html')
480+
);
481+
}
482+
444483
export {
445484
onComplete,
446485
getText,
@@ -461,5 +500,6 @@ export {
461500
sleep,
462501
waitForDocumentLoad,
463502
makeDocumentVisible,
464-
isValidTab
503+
isValidTab,
504+
isBackgroundPageContext
465505
};

0 commit comments

Comments
 (0)