106 lines
3.4 KiB
HTML
106 lines
3.4 KiB
HTML
<script>
|
|
let onboardingClientName = null;
|
|
let onboardingClientNameSyncPromise = null;
|
|
const onboardingClientNameStorageKey = 'pulse-signage-player-client-name';
|
|
const onboardingDeviceIdStorageKey = 'pulse-signage-player-device-id';
|
|
|
|
function getSessionStorageItem(key) {
|
|
try {
|
|
return window.sessionStorage.getItem(key) || '';
|
|
} catch (_error) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
function setSessionStorageItem(key, value) {
|
|
try {
|
|
window.sessionStorage.setItem(key, value);
|
|
} catch (_error) {
|
|
// ignore storage errors
|
|
}
|
|
}
|
|
|
|
function getOnboardingDeviceId() {
|
|
try {
|
|
var storedDeviceId = getSessionStorageItem(onboardingDeviceIdStorageKey);
|
|
if (storedDeviceId) {
|
|
return String(storedDeviceId || '').trim();
|
|
}
|
|
var nextDeviceId = window.crypto && window.crypto.randomUUID ? window.crypto.randomUUID() : 'device-' + Date.now() + '-' + Math.random().toString(16).slice(2);
|
|
setSessionStorageItem(onboardingDeviceIdStorageKey, nextDeviceId);
|
|
return String(nextDeviceId || '').trim();
|
|
} catch (_error) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
// Return the onboarding client name when one was assigned, otherwise a stable client id.
|
|
function getOnboardingClientName() {
|
|
if (onboardingClientName) {
|
|
return onboardingClientName;
|
|
}
|
|
try {
|
|
var storedClientName = getSessionStorageItem(onboardingClientNameStorageKey);
|
|
if (storedClientName) {
|
|
onboardingClientName = storedClientName;
|
|
return onboardingClientName;
|
|
}
|
|
var genericClientName = getSessionStorageItem('pulse-signage-player-client-name');
|
|
if (genericClientName) {
|
|
onboardingClientName = genericClientName;
|
|
return onboardingClientName;
|
|
}
|
|
} catch (_error) {
|
|
// fall through to client id generation
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function applyOnboardingClientName(renamedClientName, socket) {
|
|
var normalizedName = String(renamedClientName || '').trim();
|
|
if (!normalizedName) {
|
|
return;
|
|
}
|
|
onboardingClientName = normalizedName;
|
|
setSessionStorageItem('pulse-signage-player-client-name', normalizedName);
|
|
setSessionStorageItem(onboardingClientNameStorageKey, normalizedName);
|
|
if (socket && socket.readyState === WebSocket.OPEN) {
|
|
sendCommandState(socket);
|
|
}
|
|
}
|
|
|
|
function syncOnboardingClientNameFromServer(socket) {
|
|
var deviceId = getOnboardingDeviceId();
|
|
if (!deviceId) {
|
|
return Promise.resolve(getOnboardingClientName());
|
|
}
|
|
|
|
if (onboardingClientNameSyncPromise) {
|
|
return onboardingClientNameSyncPromise;
|
|
}
|
|
|
|
onboardingClientNameSyncPromise = fetch('/api/onboarding/status?deviceId=' + encodeURIComponent(deviceId), {
|
|
cache: 'no-store'
|
|
}).then(function (response) {
|
|
if (!response.ok) {
|
|
return null;
|
|
}
|
|
return response.json().catch(function () {
|
|
return null;
|
|
});
|
|
}).then(function (payload) {
|
|
var serverName = payload && payload.clientName ? String(payload.clientName).trim() : '';
|
|
if (serverName && !getOnboardingClientName()) {
|
|
applyOnboardingClientName(serverName, null);
|
|
}
|
|
return onboardingClientName || getOnboardingClientName();
|
|
}).catch(function () {
|
|
return onboardingClientName || getOnboardingClientName();
|
|
}).finally(function () {
|
|
onboardingClientNameSyncPromise = null;
|
|
});
|
|
|
|
return onboardingClientNameSyncPromise;
|
|
}
|
|
</script>
|