11import { v4 as uuidv4 } from 'uuid' ;
22
3+ import { isStorageArea } from 'storage/storage' ;
4+ import storage from 'storage/storage' ;
35import { targetEnv } from 'utils/config' ;
46
57function 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+ / \s e d g (?: e | a | i o s ) ? \/ / i. test ( navigator . userAgent ) ;
278312 const isFirefox = targetEnv === 'firefox' ;
279313 const isOpera =
280314 [ 'chrome' , 'opera' ] . includes ( targetEnv ) &&
281- / o p r \/ / i. test ( navigator . userAgent ) ;
315+ / \s o p r \/ / 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
305339async function isAndroid ( ) {
306- const { os} = await getPlatform ( ) ;
307- return os === 'android' ;
340+ return ( await getPlatform ( ) ) . isAndroid ;
308341}
309342
310343async function isMobile ( ) {
311- const { os} = await getPlatform ( ) ;
312- return [ 'android' , 'ios' , 'ipados' ] . includes ( os ) ;
344+ return ( await getPlatform ( ) ) . isMobile ;
313345}
314346
315347function 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+
444483export {
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