1 line
No EOL
12 KiB
JavaScript
1 line
No EOL
12 KiB
JavaScript
import browser from"./browser.js";import appSettings from"./common/appsettings.js";import events from"./emby-apiclient/events.js";let appMode=globalThis.appMode,isNativeTizen="tizen"===appMode,isNativeLG="webos"===appMode;function generateRandomUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(c){var r=16*Math.random()|0;return("x"===c?r:3&r|8).toString(16)})}function getDeviceId(){if("undefined"!=typeof Windows&&Windows.System){try{var sysIdInfo=Windows.System.Profile.SystemIdentification.getSystemIdForPublisher();if(0<sysIdInfo.id.length){let sysIdString=Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(sysIdInfo.id);return sysIdString=(sysIdString=(sysIdString=sysIdString.replaceAll("+","")).replaceAll("/","")).replaceAll("=",""),Promise.resolve(sysIdString)}}catch(ex){}sysIdInfo=new Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation;return Promise.resolve(sysIdInfo.id)}sysIdInfo="_deviceId2";let deviceId=appSettings.get(sysIdInfo);return deviceId||(deviceId=globalThis.crypto&&crypto.randomUUID?crypto.randomUUID():generateRandomUUID(),appSettings.set(sysIdInfo,deviceId)),Promise.resolve(deviceId)}function getWindowsDeviceName(){try{var currentDeviceInfo=new Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation;if(currentDeviceInfo.friendlyName&¤tDeviceInfo.friendlyName.length)return currentDeviceInfo.friendlyName}catch(err){}return"Windows.Xbox"!==Windows.System.Profile.AnalyticsInfo.versionInfo.deviceFamily?"Windows":"Xbox One"}function containsGreaseChar(value){return value.includes("\\")||value.includes("/")||value.includes(";")||value.includes('"')||value.toLowerCase().includes("brand")}function isGenericBrand(value){return!!(value=value.toLowerCase()).includes("chromium")}function compareBrands(a,b){a=a.brand||"",b=b.brand||"";return containsGreaseChar(a)&&!containsGreaseChar(b)?1:!containsGreaseChar(a)&&containsGreaseChar(b)?-1:isGenericBrand(a)&&!isGenericBrand(b)?1:!isGenericBrand(a)&&isGenericBrand(b)||a<b?-1:b<a?1:0}function map(i){return i}function getDeviceName(){if(browser.tizen){let deviceName="Samsung Smart TV";return deviceName+=globalThis.webapis&&globalThis.webapis.productinfo?" ("+globalThis.webapis.productinfo.getRealModel()+")":"",Promise.resolve(deviceName)}if(isNativeLG){let deviceName="LG Smart TV";var modelName=browser.modelName;return deviceName+=0<modelName.length?" ("+modelName+")":"",Promise.resolve(deviceName)}if("undefined"!=typeof Windows)return Promise.resolve(getWindowsDeviceName());if(browser.operaTv)return Promise.resolve("Opera TV");let deviceName;modelName=Array.prototype.map.call(navigator.userAgentData?.brands||[],map).sort(compareBrands);return deviceName=(deviceName=modelName.length?modelName[0].brand:deviceName)||"Web Browser",navigator.userAgentData.getHighEntropyValues(["platform"]).then(function(values){return values.platform&&(deviceName+=" "+values.platform),Promise.resolve(deviceName)})}function isXboxUWP(){return!("undefined"==typeof Windows||!Windows.System)&&"Windows.Xbox"===Windows.System.Profile.AnalyticsInfo.versionInfo.deviceFamily}function supportsFullscreen(){var element;return!browser.tv&&!("undefined"==typeof document||!((element=document.documentElement).requestFullscreen||element.mozRequestFullScreen||element.webkitRequestFullscreen||element.msRequestFullscreen)&&!document.createElement("video").webkitEnterFullscreen)}function supportsSoundEffects(){return!(browser.tizen||browser.edge||browser.operaTv||isNativeLG||browser.netcast||(navigator.hardwareConcurrency||4)<4||(navigator.deviceMemory||2)<2||!globalThis.AudioContext)&&document.createElement("audio").canPlayType("audio/mp3").replace(/no/,"")}function supportInAppConnectSignup(){return"winjs"===appMode||"embyclient"===appMode||!!(browser.operaTv||isNativeTizen||isNativeLG)||globalThis.location.href.toLowerCase().startsWith("https")}function getSyncProfile(){return Emby.importModule("./modules/browserdeviceprofile.js").then(function(profileBuilder){return profileBuilder({maxStaticMusicBitrate:appSettings.maxStaticMusicBitrate()})})}function supportsColorScheme(){if(!globalThis.matchMedia)return!1;try{var isDarkMode=window.matchMedia("(prefers-color-scheme: dark)").matches,isLightMode=window.matchMedia("(prefers-color-scheme: light)").matches,isNotSpecified=window.matchMedia("(prefers-color-scheme: no-preference)").matches;return!(!isDarkMode&&!isLightMode&&!isNotSpecified)}catch(err){return!1}}function getPreferredTheme(){try{if(window.matchMedia("(prefers-color-scheme: dark)").matches)return"dark";if(window.matchMedia("(prefers-color-scheme: light)").matches)return"light"}catch(err){}return null}function hasPhysicalBackButton(){return!!browser.tv}let supportedFeatures=(()=>{var features={},isLocalServer=(navigator.share&&(features.sharing=!0),"winjs"===appMode||browser.tv||(features.filedownload=!0),(browser.operaTv||isNativeTizen||isNativeLG||"winjs"===appMode)&&(features.exit=!0),isNativeLG||(features.exitmenu=!0),isNativeLG||isNativeTizen||(features.plugins=!0,features.screensaver=!0),browser.operaTv||isNativeTizen||isNativeLG||browser.playstation||(features.externallinks=!0,features.externalpremium=!0),browser.operaTv||(features.externallinkdisplay=!0,features.externalappinfo=!0),browser.tv||(features.displaymode=!0,"winjs"!==appMode&&(features.fullscreenmediaqueries=!0)),"winjs"!==appMode||browser.xboxOne||(features.sync=!0),supportsFullscreen()&&(features.fullscreenchange=!0),supportsSoundEffects()&&(features.soundeffects=!0),supportInAppConnectSignup()&&(features.connectsignup=!0),features.themesongvolume=!0,!appMode);return isLocalServer||(features.multiserver=!0,browser.electron)||"https:"===(globalThis.location||{}).protocol&&(features.rejectinsecureaddresses=!0),browser.tv&&(features.physicalvolumecontrol=!0,features.nativevolumeosd=!0),features.otherapppromotions=!0,features.targetblank=!0,features.subtitleappearancesettings=!0,browser.tv||(features.fileinput=!0,features.keyboardsettings=!0,features.remotecontrol=!0),isXboxUWP()||(features.youtube=!0),browser.chrome&&(features.chromecast=!0),supportsColorScheme()&&(features.preferredtheme=!0),isNativeTizen||isNativeLG||"winjs"===appMode||(features.premiereinheader=!0),hasPhysicalBackButton()&&(features.physicalbackbutton=!0),isNativeTizen||isNativeLG||(features.serversetup=!0),features.subtitlepositionbottom=!0,features.subtitlepositiontop=!0,globalThis.SpeechRecognition&&globalThis.isSecureContext&&(isNativeTizen||isNativeLG||(features.speechrecognition=!0)),features})();function getDefaultLayout(){if("undefined"!=typeof Windows&&Windows.System)switch(Windows.System.Profile.AnalyticsInfo.versionInfo.deviceFamily){case"Windows.Desktop":return Windows.UI.ViewManagement.UIViewSettings.getForCurrentView().userInteractionMode===Windows.UI.ViewManagement.UserInteractionMode.touch?"mobile":"desktop";case"Windows.Mobile":return"mobile";case"Windows.Xbox":return"tv";default:return"mobile"}return null}function getAppVersion(){var version;return"undefined"!=typeof Windows&&Windows.ApplicationModel?(version=Windows.ApplicationModel.Package.current.id.version,Promise.resolve(version.major+"."+version.minor+"."+version.build+"."+version.revision)):isNativeTizen&&tizen.application&&tizen.application.getAppInfo?Promise.resolve(tizen.application.getAppInfo().version):globalThis.curWidget&&globalThis.curWidget.version?Promise.resolve(globalThis.curWidget.version):globalThis.webOS&&globalThis.webOS.fetchAppInfo?new Promise(function(resolve){globalThis.webOS.fetchAppInfo(function(info){resolve(info.version)})}):Promise.resolve(globalThis.dashboardVersion||"3.0.0")}function getAppName(){return browser.electron?"Emby Theater":isNativeLG?"Emby for LG":isNativeTizen?"Emby for Samsung":"winjs"===appMode?"Emby for Windows":"Emby Web"}function brandsContain(brands,txt){for(let i=0,length=brands.length;i<length;i++)if((brands[i].brand||"").toLowerCase().includes(txt))return!0;return!1}let deviceId,deviceName,appName=getAppName(),appVersion,appHost={getWindowState:function(){return document.windowState||"Normal"},setWindowState:function(state){throw new Error("setWindowState is not supported and should not be called")},exit:function(){if(events.trigger(this,"beforeexit"),window.NetCastExit)try{window.NetCastExit()}catch(err){console.log("error closing application: "+err)}else if(window.webOS)try{webOS.platformBack()}catch(err){console.log("error closing application: "+err)}else if(window.tizen)try{tizen.application.getCurrentApplication().exit()}catch(err){console.log("error closing application: "+err)}else window.close()},supports:function(command){return supportedFeatures[command]},moreIcon:"dots-horiz",getSyncProfile:getSyncProfile,init:function(){return getDeviceId().then(function(generatedDeviceId){return deviceId=generatedDeviceId,getDeviceName().then(function(generatedDeviceName){return deviceName=generatedDeviceName,getAppVersion().then(function(generatedAppVersion){appVersion=generatedAppVersion})})})},deviceName:function(){return deviceName},deviceId:function(){return deviceId},appName:function(){return appName},appVersion:function(){return appVersion},getPushTokenInfo:function(){return{}},setTheme:function(themeSettings){var metaThemeColor=document.querySelector("meta[name=theme-color]");metaThemeColor&&metaThemeColor.setAttribute("content",themeSettings.themeColor)},setUserScalable:function(scalable){browser.tv||(scalable=scalable?"viewport-fit=cover, width=device-width, initial-scale=1, minimum-scale=1, user-scalable=yes":"viewport-fit=cover, width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no",document.querySelector("meta[name=viewport]").setAttribute("content",scalable))},deviceIconUrl:function(){var brands=navigator.userAgentData?.brands||[];return browser.xboxOne?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/xboxone.png":"winjs"===appMode?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/windowsrt.png":browser.opera||browser.operaTv?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/opera.png":isNativeTizen?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/samsungtv.png":isNativeLG||browser.netcast?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/lgtv.png":browser.playstation?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/playstation.png":browser.chromecast?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/chromecast.png":brandsContain(brands,"chrome")?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/chrome.png":brandsContain(brands,"safari")?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/safari.png":brandsContain(brands,"firefox")?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/firefox.png":brandsContain(brands,"edge")?"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/edge_chromium.png":"https://github.com/MediaBrowser/Emby.Resources/raw/master/images/devices/html5.png"},getPreferredTheme:getPreferredTheme,getDefaultLayout:getDefaultLayout,requestSpeechRecognitionPermission:function(){return Promise.resolve()}},doc=globalThis.document,_isHidden=!1;function onAppVisible(){_isHidden&&(_isHidden=!1,events.trigger(appHost,"resume"))}function onAppHidden(){_isHidden=_isHidden||!0}doc&&doc.addEventListener("visibilitychange",function(){(document.hidden?onAppHidden:onAppVisible)()}),globalThis.addEventListener&&doc&&(globalThis.addEventListener("focus",onAppVisible),globalThis.addEventListener("blur",onAppHidden));export default appHost; |