1
1
import { v4 as uuidv4 } from 'uuid' ;
2
2
3
+ import { isStorageArea } from 'storage/storage' ;
4
+ import storage from 'storage/storage' ;
3
5
import { targetEnv } from 'utils/config' ;
4
6
5
7
function getText ( messageName , substitutions ) {
@@ -224,41 +226,71 @@ async function getActiveTab() {
224
226
return tab ;
225
227
}
226
228
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' } ) ;
229
236
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' } ) ) ;
234
239
} else {
235
240
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 {
236
258
( { 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 ) { }
243
272
}
244
273
}
245
274
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
+
246
285
if ( os === 'win' ) {
247
286
os = 'windows' ;
248
287
} else if ( os === 'mac' ) {
249
288
os = 'macos' ;
250
289
}
251
290
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 ) ) {
260
292
arch = '386' ;
261
- } else if ( arch === 'x86-64' ) {
293
+ } else if ( [ 'x86-64' , 'x86_64' ] . includes ( arch ) ) {
262
294
arch = 'amd64' ;
263
295
} else if ( arch . startsWith ( 'arm' ) ) {
264
296
arch = 'arm' ;
@@ -274,11 +306,13 @@ async function getPlatform({fallback = true} = {}) {
274
306
const isMobile = [ 'android' , 'ios' , 'ipados' ] . includes ( os ) ;
275
307
276
308
const isChrome = targetEnv === 'chrome' ;
277
- const isEdge = targetEnv === 'edge' ;
309
+ const isEdge =
310
+ [ 'chrome' , 'edge' ] . includes ( targetEnv ) &&
311
+ / \s e d g (?: e | a | i o s ) ? \/ / i. test ( navigator . userAgent ) ;
278
312
const isFirefox = targetEnv === 'firefox' ;
279
313
const isOpera =
280
314
[ 'chrome' , 'opera' ] . includes ( targetEnv ) &&
281
- / o p r \/ / i. test ( navigator . userAgent ) ;
315
+ / \s o p r \/ / i. test ( navigator . userAgent ) ;
282
316
const isSafari = targetEnv === 'safari' ;
283
317
const isSamsung = targetEnv === 'samsung' ;
284
318
@@ -303,13 +337,11 @@ async function getPlatform({fallback = true} = {}) {
303
337
}
304
338
305
339
async function isAndroid ( ) {
306
- const { os} = await getPlatform ( ) ;
307
- return os === 'android' ;
340
+ return ( await getPlatform ( ) ) . isAndroid ;
308
341
}
309
342
310
343
async function isMobile ( ) {
311
- const { os} = await getPlatform ( ) ;
312
- return [ 'android' , 'ios' , 'ipados' ] . includes ( os ) ;
344
+ return ( await getPlatform ( ) ) . isMobile ;
313
345
}
314
346
315
347
function getDarkColorSchemeQuery ( ) {
@@ -441,6 +473,13 @@ async function isValidTab({tab, tabId = null} = {}) {
441
473
}
442
474
}
443
475
476
+ function isBackgroundPageContext ( ) {
477
+ return (
478
+ window . location . href ===
479
+ browser . runtime . getURL ( '/src/background/index.html' )
480
+ ) ;
481
+ }
482
+
444
483
export {
445
484
onComplete ,
446
485
getText ,
@@ -461,5 +500,6 @@ export {
461
500
sleep ,
462
501
waitForDocumentLoad ,
463
502
makeDocumentVisible ,
464
- isValidTab
503
+ isValidTab ,
504
+ isBackgroundPageContext
465
505
} ;
0 commit comments