Save worktree changes

This commit is contained in:
2026-07-25 02:29:19 +01:00
parent 8d3b7d557b
commit db9d718cd8
170 changed files with 11719 additions and 3414 deletions
+17
View File
@@ -382,6 +382,23 @@ body.screen-blackout #app {
overflow: hidden;
}
.template-region.rtmp {
background: #000;
}
.template-region.rtmp video {
width: 100%;
height: 100%;
object-fit: contain;
display: block;
pointer-events: none;
}
.template-region-rtmp-placeholder {
position: absolute;
inset: 0;
}
.template-region-placeholder {
width: 100%;
height: 100%;
@@ -0,0 +1,354 @@
// Capture the current viewport dimensions.
function getCurrentViewport() {
return {
width: window.innerWidth,
height: window.innerHeight
};
}
// Command websocket and player-state helpers.
// Send the current playback state to the command websocket.
function sendCommandState(currentSlide) {
if (!commandSocket || commandSocket.readyState !== WebSocket.OPEN) {
return;
}
commandSocket.send(JSON.stringify({
type: 'state',
clientId: getCommandClientId(),
clientName: getOnboardingClientName() || null,
deviceId: getOnboardingDeviceId() || null,
userAgent: window.navigator.userAgent || '',
page: window.location.href,
viewport: getCurrentViewport(),
paused: isPaused,
blackout: isBlackout,
currentSlide: currentSlide ? {
id: currentSlide.id || null,
title: currentSlide.title || '',
kind: currentSlide.kind || '',
playlistSignature: currentPlaylistSignature || ''
} : null
}));
}
// Debounce command-state updates during rapid changes.
function scheduleCommandStateUpdate() {
if (commandStateTimer) {
window.clearTimeout(commandStateTimer);
}
commandStateTimer = window.setTimeout(function () {
commandStateTimer = null;
sendCommandState(lastRenderedSlide);
}, 300);
}
// Debounce rerenders after viewport changes.
function scheduleViewportRenderUpdate() {
if (viewportRenderTimer) {
window.clearTimeout(viewportRenderTimer);
}
viewportRenderTimer = window.setTimeout(function () {
viewportRenderTimer = null;
if (slides.length) {
var activeSlides = getCurrentActiveSlides();
if (getCurrentRenderKey(activeSlides) === lastRenderedViewKey) {
return;
}
showCurrent();
}
}, 150);
}
// Cancel the current slide-advance timer.
function clearSlideTimer() {
if (timer) {
window.clearTimeout(timer);
timer = null;
}
}
// Schedule the next slide transition.
function scheduleSlideAdvance(delayMs) {
clearSlideTimer();
var holdDelayMs = Math.max(1, Number(delayMs || 0));
slideExpiresAt = Date.now() + holdDelayMs;
timer = window.setTimeout(function () {
timer = null;
slideExpiresAt = null;
pausedRemainingMs = null;
const activeSlides = getCurrentActiveSlides();
if (activeSlides.length < 2) {
refresh();
return;
}
if (index >= activeSlides.length) {
index = 0;
}
index = (index + 1) % activeSlides.length;
showCurrent();
}, holdDelayMs);
}
// Cancel any pending fade-transition cleanup.
function clearSlideTransitionTimer() {
if (slideTransitionTimer) {
window.clearTimeout(slideTransitionTimer);
slideTransitionTimer = null;
}
}
// Swap slide markup with optional fade animation.
function renderSlideMarkup(markup, shouldFade) {
clearSlideTransitionTimer();
if (typeof destroyRtmpRegions === 'function') {
destroyRtmpRegions(app);
}
if (!shouldFade) {
app.innerHTML = markup;
if (typeof syncRtmpRegions === 'function') {
syncRtmpRegions(app);
}
return app.firstElementChild;
}
var topLevelChildren = Array.prototype.slice.call(app.children || []);
var existingShells = topLevelChildren.filter(function (child) {
return child && child.classList && child.classList.contains('slide-shell');
});
var previousShell = existingShells.length ? existingShells[existingShells.length - 1] : app.firstElementChild;
if (existingShells.length > 1) {
existingShells.slice(0, -1).forEach(function (shell) {
if (shell && shell.parentNode) {
shell.parentNode.removeChild(shell);
}
});
}
var nextShell = document.createElement('div');
nextShell.className = 'slide-shell';
nextShell.style.opacity = '0';
nextShell.innerHTML = markup;
if (!previousShell || (previousShell.classList && previousShell.classList.contains('empty'))) {
app.innerHTML = '';
nextShell.style.opacity = '1';
app.appendChild(nextShell);
if (typeof syncRtmpRegions === 'function') {
syncRtmpRegions(nextShell);
}
return nextShell;
}
if (!previousShell.classList.contains('slide-shell')) {
previousShell.classList.add('slide-shell');
}
previousShell.style.opacity = '1';
app.appendChild(nextShell);
void nextShell.offsetHeight;
window.requestAnimationFrame(function () {
nextShell.style.opacity = '1';
previousShell.style.opacity = '0';
});
if (typeof syncRtmpRegions === 'function') {
syncRtmpRegions(nextShell);
}
slideTransitionTimer = window.setTimeout(function () {
if (previousShell && previousShell.parentNode) {
previousShell.parentNode.removeChild(previousShell);
}
if (nextShell) {
nextShell.style.opacity = '1';
}
slideTransitionTimer = null;
}, slideFadeDurationMs);
return nextShell;
}
// Mirror blackout state onto the document body.
function syncBlackoutState() {
document.body.classList.toggle('screen-blackout', isBlackout);
}
// Apply pause state and preserve remaining slide time.
function setPaused(nextPaused) {
var normalized = Boolean(nextPaused);
if (isPaused === normalized) {
return;
}
if (normalized) {
pausedRemainingMs = slideExpiresAt ? Math.max(0, slideExpiresAt - Date.now()) : null;
isPaused = true;
clearSlideTimer();
sendCommandState(lastRenderedSlide);
return;
}
isPaused = false;
sendCommandState(lastRenderedSlide);
if (!slides.length || !lastRenderedSlide) {
return;
}
if (pausedRemainingMs !== null) {
scheduleSlideAdvance(pausedRemainingMs);
pausedRemainingMs = null;
}
}
// Apply blackout state and notify the server.
function setBlackout(nextBlackout) {
var normalized = Boolean(nextBlackout);
if (isBlackout === normalized) {
return;
}
isBlackout = normalized;
syncBlackoutState();
sendCommandState(lastRenderedSlide);
}
// Coerce command payload values into booleans or null.
function normalizeBoolean(value) {
if (value === true || value === false) {
return value;
}
if (value === null || value === undefined) {
return null;
}
var normalized = String(value).trim().toLowerCase();
if (['1', 'true', 'yes', 'on'].indexOf(normalized) !== -1) {
return true;
}
if (['0', 'false', 'no', 'off', ''].indexOf(normalized) !== -1) {
return false;
}
return null;
}
// Move to the previous or next active slide.
function navigateSlides(offset) {
const manualSlides = getCurrentActiveSlides();
if (!manualSlides.length) {
return;
}
let currentIndex = manualSlides.findIndex(function (slide) {
return slide && lastRenderedSlide && slide.id === lastRenderedSlide.id;
});
if (currentIndex < 0) {
currentIndex = Math.min(Math.max(index, 0), manualSlides.length - 1);
}
const nextIndex = (currentIndex + offset + manualSlides.length) % manualSlides.length;
clearSlideTimer();
applyPendingPlaylistUpdate();
renderSlideAtIndex(manualSlides, nextIndex);
}
// Route incoming websocket command messages.
function handleCommandMessage(rawMessage) {
var payload;
try {
payload = JSON.parse(String(rawMessage || ''));
} catch (_error) {
return;
}
if (!payload || payload.type !== 'command') {
return;
}
switch (payload.command) {
case 'refresh':
refresh();
return;
case 'setclientname':
if (payload.clientName) {
applyOnboardingClientName(payload.clientName, commandSocket);
}
return;
case 'redirect':
if (payload.url) {
window.location.replace(String(payload.url));
}
return;
case 'pause':
setPaused(!isPaused);
return;
case 'blackout':
var desiredBlackout = normalizeBoolean(payload.blackout);
if (desiredBlackout !== null) {
setBlackout(desiredBlackout);
} else {
setBlackout(!isBlackout);
}
return;
case 'previous':
case 'left':
navigateSlides(-1);
return;
case 'next':
case 'right':
navigateSlides(1);
return;
case 'reload':
window.location.reload();
return;
}
}
// Retry the command websocket after a disconnect.
function scheduleCommandReconnect() {
if (commandReconnectTimer) {
return;
}
commandReconnectTimer = window.setTimeout(function () {
commandReconnectTimer = null;
connectCommandSocket();
}, 5000);
}
// Open and wire the command websocket connection.
function connectCommandSocket() {
if (!window.WebSocket) {
return;
}
if (commandSocket && (commandSocket.readyState === WebSocket.OPEN || commandSocket.readyState === WebSocket.CONNECTING)) {
return;
}
var protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
var socketUrl = new URL(commandSocketPath, window.location.origin);
if (window.__pulsePageAuthToken) {
socketUrl.searchParams.set('auth', window.__pulsePageAuthToken);
}
var socket = new WebSocket(socketUrl.toString());
commandSocket = socket;
socket.onopen = function () {
if (typeof syncOnboardingClientNameFromServer === 'function') {
syncOnboardingClientNameFromServer(socket).then(function () {
sendCommandHello(socket);
});
return;
}
sendCommandHello(socket);
};
socket.onmessage = function (event) {
handleCommandMessage(event.data);
};
socket.onclose = function () {
commandSocket = null;
scheduleCommandReconnect();
};
socket.onerror = function () {
try {
socket.close();
} catch (_error) {
// ignore socket close errors
}
};
}
+160
View File
@@ -0,0 +1,160 @@
// Show or hide the offline status banner.
function setOfflineBannerVisible(visible, message) {
var normalizedVisible = Boolean(visible);
var bannerMessage = String(message || 'Offline mode: using cached playlist.').trim();
if (normalizedVisible) {
if (!offlineBanner) {
offlineBanner = document.createElement('div');
offlineBanner.className = 'player-offline-banner';
offlineBanner.style.position = 'fixed';
offlineBanner.style.right = '0';
offlineBanner.style.bottom = '0';
offlineBanner.style.left = 'auto';
offlineBanner.style.top = 'auto';
offlineBanner.style.width = '1.25rem';
offlineBanner.style.height = '1.25rem';
offlineBanner.style.zIndex = '9999';
offlineBanner.style.background = 'linear-gradient(135deg, #ff4d4f 0%, #b00020 100%)';
offlineBanner.style.clipPath = 'circle(100% at 100% 100%)';
offlineBanner.style.webkitClipPath = 'circle(100% at 100% 100%)';
offlineBanner.style.boxShadow = '0 0 0 1px rgba(0, 0, 0, 0.16), 0 4px 12px rgba(0, 0, 0, 0.18)';
offlineBanner.style.pointerEvents = 'none';
document.body.appendChild(offlineBanner);
}
offlineBanner.textContent = '';
offlineBanner.setAttribute('aria-label', bannerMessage);
offlineBanner.setAttribute('role', 'img');
offlineBanner.title = bannerMessage;
offlineBannerVisible = true;
return;
}
offlineBannerVisible = false;
if (offlineBanner && offlineBanner.parentNode) {
offlineBanner.parentNode.removeChild(offlineBanner);
}
offlineBanner = null;
}
// Update the offline banner based on connectivity or playlist availability.
function syncOfflineBanner() {
if (!window.navigator.onLine) {
setOfflineBannerVisible(true, 'Offline mode: using cached playlist.');
return;
}
if (offlineBannerVisible) {
setOfflineBannerVisible(false);
}
}
// Clear any pending playlist refresh retry.
function clearRefreshRetry() {
if (refreshRetryTimer) {
window.clearTimeout(refreshRetryTimer);
refreshRetryTimer = null;
}
}
// Retry playlist refresh with a short backoff while the player is offline.
function scheduleRefreshRetry() {
if (refreshRetryTimer) {
return;
}
if (window.navigator.onLine === false) {
refreshRetryDelayMs = refreshRetryDelayMs ? Math.min(refreshRetryDelayMs * 2, 15000) : 3000;
} else {
refreshRetryDelayMs = refreshRetryDelayMs ? Math.min(refreshRetryDelayMs * 2, 8000) : 3000;
}
refreshRetryTimer = window.setTimeout(function () {
refreshRetryTimer = null;
refresh();
}, refreshRetryDelayMs);
}
function clearScreenWakeLockRetry() {
if (screenWakeLockRetryTimer) {
window.clearTimeout(screenWakeLockRetryTimer);
screenWakeLockRetryTimer = null;
}
}
function supportsScreenWakeLock() {
return Boolean(window.navigator && window.navigator.wakeLock && typeof window.navigator.wakeLock.request === 'function');
}
function scheduleScreenWakeLockRetry() {
if (screenWakeLockRetryTimer) {
return;
}
if (!supportsScreenWakeLock() || document.visibilityState !== 'visible') {
return;
}
screenWakeLockRetryTimer = window.setTimeout(function () {
screenWakeLockRetryTimer = null;
acquireScreenWakeLock();
}, 2000);
}
function releaseScreenWakeLock() {
if (screenWakeLock && typeof screenWakeLock.release === 'function') {
try {
screenWakeLock.release();
} catch (_error) {
// ignore wake lock release errors
}
}
screenWakeLock = null;
screenWakeLockRequestPromise = null;
clearScreenWakeLockRetry();
}
function acquireScreenWakeLock() {
if (!supportsScreenWakeLock() || document.visibilityState !== 'visible') {
return Promise.resolve(null);
}
if (screenWakeLockRequestPromise) {
return screenWakeLockRequestPromise;
}
if (screenWakeLock && screenWakeLock.released === false) {
return Promise.resolve(screenWakeLock);
}
screenWakeLockRequestPromise = window.navigator.wakeLock.request('screen').then(function (sentinel) {
screenWakeLock = sentinel;
screenWakeLock.addEventListener('release', function () {
screenWakeLock = null;
if (document.visibilityState === 'visible') {
scheduleScreenWakeLockRetry();
}
});
clearScreenWakeLockRetry();
return screenWakeLock;
}).catch(function (error) {
screenWakeLock = null;
if (error && error.name !== 'NotAllowedError') {
scheduleScreenWakeLockRetry();
}
return null;
}).finally(function () {
screenWakeLockRequestPromise = null;
});
return screenWakeLockRequestPromise;
}
function syncScreenWakeLock() {
if (!supportsScreenWakeLock()) {
return;
}
if (document.visibilityState === 'visible') {
acquireScreenWakeLock();
return;
}
releaseScreenWakeLock();
}
// Clear the retry cadence after a successful refresh.
function markRefreshHealthy() {
refreshRetryDelayMs = 0;
clearRefreshRetry();
}
@@ -0,0 +1,190 @@
// Render the slide at the requested index within the active set.
function renderSlideAtIndex(sourceSlides, targetIndex) {
const availableSlides = Array.isArray(sourceSlides) ? sourceSlides : [];
if (!availableSlides.length) {
renderEmpty(slides.length ? 'No slides are scheduled for this time.' : 'No slides assigned to this screen yet.');
lastRenderedViewKey = getCurrentRenderKey(availableSlides);
return false;
}
let normalizedIndex = Number(targetIndex || 0);
if (!Number.isFinite(normalizedIndex) || normalizedIndex < 0 || normalizedIndex >= availableSlides.length) {
}
const slide = availableSlides[normalizedIndex];
if (!slide) {
renderEmpty(slides.length ? 'No slides are scheduled for this time.' : 'No slides assigned to this screen yet.');
return false;
}
index = normalizedIndex;
var markup = buildSlideMarkup(slide);
renderSlideMarkup(markup, currentPlaylistFadeBetweenSlides);
sendCommandState(slide);
if (!isPaused) {
scheduleSlideAdvance(Math.max(1, Number(slide.duration_seconds || 10)) * 1000);
}
lastRenderedViewKey = getCurrentRenderKey(availableSlides);
return true;
}
// Promote a deferred playlist update at the next safe point.
function applyPendingPlaylistUpdate() {
if (!pendingPlaylistUpdate) {
return false;
}
slides = pendingPlaylistUpdate.slides;
currentPlaylistSignature = pendingPlaylistUpdate.signature;
currentPlaylistFadeBetweenSlides = pendingPlaylistUpdate.fadeBetweenSlides;
pendingPlaylistUpdate = null;
clearActiveSlidesCache();
slideMarkupCache = Object.create(null);
templateLayoutCache = Object.create(null);
templateRenderPlanCache = Object.create(null);
renderCacheViewportKey = window.innerWidth + 'x' + window.innerHeight;
index = 0;
logDebug('Applied updated playlist on slide transition.');
return true;
}
// Render the current active slide or the empty state.
function showCurrent() {
clearSlideTimer();
applyPendingPlaylistUpdate();
const activeSlides = getCurrentActiveSlides();
syncWebpagePreloads(activeSlides, index);
if (typeof syncRtmpPreloads === 'function') {
syncRtmpPreloads(activeSlides, index);
}
if (!activeSlides.length) {
renderEmpty(slides.length ? 'No slides are scheduled for this time.' : 'No slides assigned to this screen yet.');
lastRenderedViewKey = getCurrentRenderKey(activeSlides);
return;
}
renderSlideAtIndex(activeSlides, index);
lastRenderedViewKey = getCurrentRenderKey(activeSlides);
}
// Fetch the latest playlist and queue any updates.
function refresh() {
var request = new XMLHttpRequest();
var url = window.location.origin + '/api/screens/' + encodeURIComponent(slug) + '/playlist?ts=' + Date.now();
request.open('GET', url, true);
request.timeout = 2500;
if (window.__pulsePageAuthToken) {
request.setRequestHeader('x-pulse-page-auth', window.__pulsePageAuthToken);
}
if (currentPlaylistEtag) {
request.setRequestHeader('If-None-Match', currentPlaylistEtag);
}
request.onreadystatechange = function () {
if (request.readyState !== 4) {
return;
}
if (request.status === 304) {
markRefreshHealthy();
setOfflineBannerVisible(false);
if (lastRenderedSlide && getCurrentActiveSlides().length < 2) {
scheduleSlideAdvance(Math.max(1, Number(lastRenderedSlide.duration_seconds || 10)) * 1000);
}
return;
}
if (request.status < 200 || request.status >= 300) {
setOfflineBannerVisible(true);
scheduleRefreshRetry();
logDebug(
'Screen not found or playlist unavailable.',
['URL: ' + url, 'Status: ' + request.status + ' ' + request.statusText, 'Response: ' + String(request.responseText || '').slice(0, 1000)].join(' | '),
'error'
);
return;
}
try {
const responseEtag = String(request.getResponseHeader('ETag') || '').trim();
const data = JSON.parse(request.responseText || '{}');
const nextSignature = getPlaylistRevision(data);
const nextSlides = Array.isArray(data.slides) ? data.slides.map(normalizeSlide) : [];
const nextFadeBetweenSlides = Boolean(data && data.playlist && data.playlist.fade_between_slides);
savePlaylistSnapshot({
slides: nextSlides,
signature: nextSignature,
fadeBetweenSlides: nextFadeBetweenSlides,
etag: responseEtag
});
markRefreshHealthy();
setOfflineBannerVisible(false);
const currentActiveSlides = getCurrentActiveSlides();
if (responseEtag) {
currentPlaylistEtag = responseEtag;
}
if (!currentPlaylistSignature) {
slides = nextSlides;
currentPlaylistSignature = nextSignature;
currentPlaylistFadeBetweenSlides = nextFadeBetweenSlides;
index = 0;
showCurrent();
sendCommandState(lastRenderedSlide);
return;
}
if (nextSignature === currentPlaylistSignature || (pendingPlaylistUpdate && nextSignature === pendingPlaylistUpdate.signature)) {
if (currentActiveSlides.length < 2 && lastRenderedSlide) {
scheduleSlideAdvance(Math.max(1, Number(lastRenderedSlide.duration_seconds || 10)) * 1000);
}
return;
}
syncWebpagePreloads(getActiveSlidesFrom(nextSlides), index);
if (typeof syncRtmpPreloads === 'function') {
syncRtmpPreloads(getActiveSlidesFrom(nextSlides), index);
}
if (currentActiveSlides.length < 2) {
slides = nextSlides;
currentPlaylistSignature = nextSignature;
currentPlaylistFadeBetweenSlides = nextFadeBetweenSlides;
pendingPlaylistUpdate = null;
index = 0;
showCurrent();
sendCommandState(lastRenderedSlide);
return;
}
pendingPlaylistUpdate = {
slides: nextSlides,
signature: nextSignature,
fadeBetweenSlides: nextFadeBetweenSlides
};
logDebug('Playlist update detected; applying on next slide transition.');
} catch (_error) {
logDebug(
'Unable to load screen playlist.',
['URL: ' + url, 'Response: ' + String(request.responseText || '').slice(0, 1000)].join(' | '),
'error'
);
setOfflineBannerVisible(true);
scheduleRefreshRetry();
}
};
request.onerror = function () {
logDebug(
'Unable to load screen playlist.',
['URL: ' + url, 'Network error during request.'].join(' | '),
'error'
);
setOfflineBannerVisible(true);
scheduleRefreshRetry();
if (lastRenderedSlide && getCurrentActiveSlides().length < 2) {
scheduleSlideAdvance(Math.max(1, Number(lastRenderedSlide.duration_seconds || 10)) * 1000);
}
};
request.ontimeout = function () {
logDebug(
'Playlist refresh timed out.',
['URL: ' + url, 'Timeout after ' + request.timeout + 'ms'].join(' | '),
'error'
);
setOfflineBannerVisible(true);
scheduleRefreshRetry();
if (lastRenderedSlide && getCurrentActiveSlides().length < 2) {
scheduleSlideAdvance(Math.max(1, Number(lastRenderedSlide.duration_seconds || 10)) * 1000);
}
};
request.send();
}
@@ -0,0 +1,216 @@
// Collect unique webpage URLs from the slide list.
function getWebpageUrls(sourceSlides) {
const urls = [];
(Array.isArray(sourceSlides) ? sourceSlides : []).forEach(function (slide) {
const content = slide && slide.content ? slide.content : {};
const regions = slide && slide.template && Array.isArray(slide.template.regions) ? slide.template.regions : [];
regions.forEach(function (region) {
if (region.region_type !== 'webpage') {
return;
}
const regionContent = content[region.region_key] || {};
const url = String(regionContent.value || '').trim();
if (url && urls.indexOf(url) === -1) {
urls.push(url);
}
});
});
return urls;
}
// Filter the slides down to those that are active right now.
function getActiveSlidesFrom(sourceSlides) {
const now = new Date();
return (Array.isArray(sourceSlides) ? sourceSlides : []).filter(function (slide) {
return isSlideActive(slide, now);
});
}
// Build a cache key for the active slide set.
function getActiveSlidesCacheKey() {
const now = new Date();
return [
currentPlaylistSignature || '',
now.getFullYear(),
now.getMonth(),
now.getDate(),
now.getHours(),
now.getMinutes(),
now.getSeconds()
].join('|');
}
// Return the cached active slide set for the current playlist and second.
function getCurrentActiveSlides() {
const cacheKey = getActiveSlidesCacheKey();
if (cacheKey !== activeSlidesCacheKey) {
activeSlidesCacheValue = getActiveSlidesFrom(slides);
activeSlidesCacheKey = cacheKey;
}
return activeSlidesCacheValue;
}
// Clear the cached active slide set.
function clearActiveSlidesCache() {
activeSlidesCacheKey = '';
activeSlidesCacheValue = [];
}
// Reset render caches when the viewport changes.
function syncRenderCacheViewport() {
var viewportKey = window.innerWidth + 'x' + window.innerHeight;
if (renderCacheViewportKey === viewportKey) {
return;
}
renderCacheViewportKey = viewportKey;
slideMarkupCache = Object.create(null);
templateLayoutCache = Object.create(null);
templateRenderPlanCache = Object.create(null);
}
// Build a signature for the currently rendered view.
function getCurrentRenderKey(activeSlides) {
const viewportKey = window.innerWidth + 'x' + window.innerHeight;
const availableSlides = Array.isArray(activeSlides) ? activeSlides : [];
if (!availableSlides.length) {
return [currentPlaylistSignature || '', viewportKey, 'empty', slides.length ? 'scheduled' : 'assigned'].join('|');
}
let normalizedIndex = Number(index || 0);
if (!Number.isFinite(normalizedIndex) || normalizedIndex < 0 || normalizedIndex >= availableSlides.length) {
normalizedIndex = 0;
}
const slide = availableSlides[normalizedIndex];
return [currentPlaylistSignature || '', viewportKey, 'slide', slide && slide.id ? slide.id : ''].join('|');
}
// Pick the current slide and the next slide for webpage preloading.
function getWebpagePreloadSlides(sourceSlides, targetIndex) {
const availableSlides = Array.isArray(sourceSlides) ? sourceSlides : [];
if (!availableSlides.length) {
return [];
}
let normalizedIndex = Number(targetIndex || 0);
if (!Number.isFinite(normalizedIndex) || normalizedIndex < 0 || normalizedIndex >= availableSlides.length) {
normalizedIndex = 0;
}
const preloadSlides = [];
const currentSlide = availableSlides[normalizedIndex];
const nextSlide = availableSlides[normalizedIndex + 1];
if (currentSlide) {
preloadSlides.push(currentSlide);
}
if (nextSlide && nextSlide !== currentSlide) {
preloadSlides.push(nextSlide);
}
return preloadSlides;
}
// Mount hidden iframe preloads for the chosen webpage URLs.
function syncWebpagePreloads(sourceSlides, targetIndex) {
const urls = getWebpageUrls(getWebpagePreloadSlides(sourceSlides, targetIndex));
const signature = urls.join('\n');
if (signature === preloadSignature) {
return;
}
if (!urls.length) {
preloadSignature = '';
if (preloadContainer) {
preloadContainer.innerHTML = '';
}
return;
}
if (!preloadContainer) {
preloadContainer = document.createElement('div');
preloadContainer.className = 'webpage-preloads';
preloadContainer.setAttribute('aria-hidden', 'true');
document.body.appendChild(preloadContainer);
}
preloadContainer.innerHTML = urls.map(function (url) {
return '<iframe class="webpage-preload-frame" src="' + escapeHtml(url) + '" title="Webpage preload" tabindex="-1" loading="eager" referrerpolicy="no-referrer" scrolling="no"></iframe>';
}).join('');
preloadSignature = signature;
}
// Return a stable client id for this browser session.
function getCommandClientId() {
if (commandClientId) {
return commandClientId;
}
try {
var storedClientId = window.localStorage.getItem(commandClientStorageKey);
if (storedClientId) {
commandClientId = storedClientId;
return commandClientId;
}
} catch (_error) {
// fall through to ephemeral ID generation
}
commandClientId = (window.crypto && window.crypto.randomUUID ? window.crypto.randomUUID() : 'client-' + Date.now() + '-' + Math.random().toString(16).slice(2));
try {
window.localStorage.setItem(commandClientStorageKey, commandClientId);
} catch (_error2) {
// ignore storage errors
}
return commandClientId;
}
// Load the most recent playlist snapshot from browser storage.
function loadPlaylistSnapshot() {
try {
var raw = window.localStorage.getItem(playlistSnapshotStorageKey);
if (!raw) {
return null;
}
var parsed = JSON.parse(raw);
if (!parsed || !Array.isArray(parsed.slides)) {
return null;
}
return {
slides: parsed.slides.map(normalizeSlide),
signature: String(parsed.signature || ''),
fadeBetweenSlides: Boolean(parsed.fadeBetweenSlides),
etag: String(parsed.etag || '')
};
} catch (_error) {
return null;
}
}
// Save the latest playlist snapshot for offline recovery.
function savePlaylistSnapshot(data) {
try {
window.localStorage.setItem(playlistSnapshotStorageKey, JSON.stringify({
slides: Array.isArray(data && data.slides) ? data.slides : [],
signature: String(data && data.signature || ''),
fadeBetweenSlides: Boolean(data && data.fadeBetweenSlides),
etag: String(data && data.etag || ''),
savedAt: new Date().toISOString()
}));
} catch (_error) {
// ignore storage errors
}
}
// Apply a playlist snapshot to the current in-memory state.
function applyPlaylistSnapshot(data) {
if (!data || !Array.isArray(data.slides)) {
return false;
}
slides = data.slides.map(normalizeSlide);
currentPlaylistSignature = String(data.signature || '');
currentPlaylistFadeBetweenSlides = Boolean(data.fadeBetweenSlides);
currentPlaylistEtag = String(data.etag || '');
pendingPlaylistUpdate = null;
clearActiveSlidesCache();
slideMarkupCache = Object.create(null);
templateLayoutCache = Object.create(null);
templateRenderPlanCache = Object.create(null);
renderCacheViewportKey = '';
index = 0;
return true;
}
@@ -0,0 +1,537 @@
// General sanitization and sizing helpers.
// Strip unsupported characters from a font family string.
function sanitizeFontFamily(value) {
return String(value || '').replace(/[^a-zA-Z0-9 ,\"-]/g, '').trim();
}
// Clamp font size to the supported range.
function sanitizeFontSize(value) {
return Math.max(8, Number(value || 0) || 24);
}
// Validate a text color and fall back when needed.
function sanitizeTextColor(value, fallback) {
var raw = String(value || '').trim();
if (/^#[0-9a-fA-F]{6}$/.test(raw) || /^#[0-9a-fA-F]{3}$/.test(raw)) {
return raw;
}
return fallback || '#000000';
}
// Read the template's canvas dimensions with safe defaults.
function getTemplateCanvasSize(template) {
return {
width: Math.max(1, Number(template.canvas_size_width || 1920)),
height: Math.max(1, Number(template.canvas_size_height || 1080))
};
}
// Read the server-supplied playlist revision, or fall back to the ETag.
function getPlaylistRevision(data) {
if (data && data.revision) {
return String(data.revision);
}
if (data && data.playlist && data.playlist.revision) {
return String(data.playlist.revision);
}
if (currentPlaylistEtag) {
return String(currentPlaylistEtag).replace(/^"|"$/g, '');
}
return String(Date.now());
}
// Scale a canvas to fit within the viewport.
function fitCanvasSize(canvasWidth, canvasHeight, maxWidth, maxHeight) {
var width = Math.max(1, Number(canvasWidth || 0) || 1920);
var height = Math.max(1, Number(canvasHeight || 0) || 1080);
var viewportWidth = Math.max(1, Number(maxWidth || 0) || width);
var viewportHeight = Math.max(1, Number(maxHeight || 0) || height);
var scale = Math.min(viewportWidth / width, viewportHeight / height);
return {
width: Math.round(width * scale),
height: Math.round(height * scale)
};
}
const ALLOWED_RICH_TEXT_TAGS = ['a', 'b', 'blockquote', 'br', 'code', 'div', 'em', 'figure', 'figcaption', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'li', 'ol', 'p', 'pre', 'span', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'th', 'thead', 'tr', 'u', 'ul'];
function sanitizeRichTextAttributes(tagName, attrText) {
const allowedAttributes = {
a: ['href', 'title', 'target', 'rel', 'class', 'style'],
blockquote: ['class', 'style'],
div: ['class', 'style'],
figure: ['class', 'style'],
figcaption: ['class', 'style'],
h1: ['class', 'style'],
h2: ['class', 'style'],
h3: ['class', 'style'],
h4: ['class', 'style'],
h5: ['class', 'style'],
h6: ['class', 'style'],
li: ['class', 'style'],
ol: ['class', 'style', 'start'],
p: ['class', 'style'],
pre: ['class', 'style'],
span: ['class', 'style'],
table: ['class', 'style'],
td: ['class', 'style', 'colspan', 'rowspan'],
th: ['class', 'style', 'colspan', 'rowspan', 'scope'],
tr: ['class', 'style'],
ul: ['class', 'style']
};
const allowed = allowedAttributes[tagName] || [];
if (!allowed.length) {
return '';
}
const attrs = [];
String(attrText || '').replace(/([a-zA-Z0-9:-]+)(?:\s*=\s*("([^"]*)"|'([^']*)'|([^\s"'>/=`]+)))?/g, (_full, key, _valuePart, doubleQuoted, singleQuoted, bareValue) => {
const lowerKey = String(key || '').toLowerCase();
if (!allowed.includes(lowerKey)) {
return '';
}
const value = doubleQuoted !== undefined ? doubleQuoted : singleQuoted !== undefined ? singleQuoted : bareValue !== undefined ? bareValue : '';
if (lowerKey === 'href' && /^(?:\s*javascript:|\s*data:)/i.test(String(value || ''))) {
return '';
}
if (lowerKey === 'style' && /(?:expression\s*\(|javascript:|url\s*\()/i.test(String(value || ''))) {
return '';
}
if (lowerKey === 'target') {
const targetValue = String(value || '').trim();
if (targetValue === '_blank') {
attrs.push(' target="_blank"');
if (!attrs.includes(' rel="noreferrer noopener"')) {
attrs.push(' rel="noreferrer noopener"');
}
return '';
}
}
attrs.push(' ' + lowerKey + '="' + escapeHtml(value) + '"');
return '';
});
return attrs.join('');
}
// Remove unsafe markup while preserving richer CKEditor formatting.
function sanitizeRichText(html) {
var output = String(html || '');
output = output.replace(/<script[\s\S]*?<\/script>/gi, '');
output = output.replace(/<style[\s\S]*?<\/style>/gi, '');
return output.replace(/<[^>]+>/g, function (tag) {
var match = tag.match(/^<\s*(\/?)\s*([a-z0-9]+)([\s\S]*?)(\/?)>$/i);
if (!match) {
return '';
}
var closing = Boolean(match[1]);
var name = String(match[2] || '').toLowerCase();
var attrText = String(match[3] || '');
if (ALLOWED_RICH_TEXT_TAGS.indexOf(name) === -1) {
return '';
}
if (closing) {
return '</' + name + '>';
}
return '<' + name + sanitizeRichTextAttributes(name, attrText) + '>';
});
}
// Render a single Editor.js block to HTML.
function renderEditorJsBlock(block) {
if (!block || !block.type || !block.data) {
return '';
}
if (block.type === 'header') {
var level = Math.max(1, Math.min(6, Number(block.data.level || 2)));
return '<h' + level + '>' + sanitizeRichText(block.data.text || '') + '</h' + level + '>';
}
if (block.type === 'list') {
var tag = block.data.style === 'ordered' ? 'ol' : 'ul';
var items = Array.isArray(block.data.items) ? block.data.items : [];
return '<' + tag + ' style="list-style-type:' + (tag === 'ol' ? 'decimal' : 'disc') + ';padding-left:1.4em;">' + items.map(function (item) { return renderEditorJsListItem(item, tag); }).join('') + '</' + tag + '>';
}
if (block.type === 'delimiter') {
return '<hr />';
}
if (block.type === 'code') {
return '<pre><code>' + escapeHtml(block.data.code || '') + '</code></pre>';
}
if (block.type === 'table') {
return renderEditorJsTable(block.data);
}
if (block.type === 'paragraph') {
return '<p>' + sanitizeRichText(block.data.text || '') + '</p>';
}
return '';
}
// Render a list item and any nested sub-items.
function renderEditorJsListItem(item, tag) {
if (item && typeof item === 'object') {
var content = item.content !== undefined ? item.content : (item.text !== undefined ? item.text : item.value !== undefined ? item.value : '');
var children = Array.isArray(item.items) ? item.items : Array.isArray(item.subItems) ? item.subItems : Array.isArray(item.children) ? item.children : [];
var nested = children.length ? '<' + tag + ' style="list-style-type:' + (tag === 'ol' ? 'decimal' : 'disc') + ';padding-left:1.4em;">' + children.map(function (child) { return renderEditorJsListItem(child, tag); }).join('') + '</' + tag + '>' : '';
return '<li>' + sanitizeRichText(content || '') + nested + '</li>';
}
return '<li>' + sanitizeRichText(item || '') + '</li>';
}
// Render an Editor.js table block.
function renderEditorJsTable(data) {
var rows = Array.isArray(data.content) ? data.content : Array.isArray(data.rows) ? data.rows : [];
if (!rows.length) {
return '';
}
var hasHeadings = Boolean(data.withHeadings);
var tableRows = rows.map(function (row, rowIndex) {
var cells = Array.isArray(row) ? row : [];
var cellTag = hasHeadings && rowIndex === 0 ? 'th' : 'td';
var cellAttrs = cellTag === 'th' ? ' scope="col"' : '';
return '<tr>' + cells.map(function (cell) {
return '<' + cellTag + cellAttrs + '>' + sanitizeRichText(cell || '') + '</' + cellTag + '>';
}).join('') + '</tr>';
}).join('');
return '<table class="ck-content-table">' + tableRows + '</table>';
}
// Render Editor.js JSON or plain content safely.
function renderEditorJsContent(value) {
if (value && typeof value === 'object') {
if (Array.isArray(value.blocks)) {
return value.blocks.map(renderEditorJsBlock).join('');
}
if (value.value !== undefined) {
return renderEditorJsContent(value.value);
}
}
var raw = String(value || '');
try {
var parsed = JSON.parse(raw);
if (parsed && Array.isArray(parsed.blocks)) {
return parsed.blocks.map(renderEditorJsBlock).join('');
}
} catch (_error) {
// fall through to legacy HTML rendering
}
return sanitizeRichText(raw);
}
// Parse string values that look like JSON.
function parseMaybeJson(value) {
if (typeof value !== 'string') {
return value;
}
var raw = value.trim();
if (!raw) {
return value;
}
if (raw.charAt(0) !== '{' && raw.charAt(0) !== '[') {
return value;
}
try {
return JSON.parse(raw);
} catch (_error) {
return value;
}
}
// Normalize a slide region's stored content value.
function normalizeContentValue(value) {
var normalized;
if (!value || typeof value !== 'object') {
return {
type: 'text',
value: parseMaybeJson(value)
};
}
normalized = {};
Object.keys(value).forEach(function (key) {
normalized[key] = value[key];
});
if (normalized.value !== undefined) {
normalized.value = parseMaybeJson(normalized.value);
}
if (normalized.font_family !== undefined && normalized.font_family !== null) {
normalized.font_family = sanitizeFontFamily(normalized.font_family);
}
if (normalized.font_size !== undefined && normalized.font_size !== null) {
normalized.font_size = sanitizeFontSize(normalized.font_size);
}
if (normalized.font_color !== undefined && normalized.font_color !== null) {
normalized.font_color = sanitizeTextColor(normalized.font_color);
}
if (!normalized.type) {
normalized.type = 'text';
}
return normalized;
}
// Normalize a slide and its nested region content.
function normalizeSlide(slide) {
var normalized = {};
var content;
Object.keys(slide || {}).forEach(function (key) {
normalized[key] = slide[key];
});
normalized.content = {};
content = slide && slide.content && typeof slide.content === 'object' ? slide.content : {};
Object.keys(content).forEach(function (regionKey) {
normalized.content[regionKey] = normalizeContentValue(content[regionKey]);
});
return normalized;
}
// Parse the stored schedule-day list into numbers.
function parseScheduleDays(value) {
if (!value) {
return [];
}
if (Array.isArray(value)) {
return value.map(function (item) { return Number(item); }).filter(function (item) { return !Number.isNaN(item); });
}
try {
const parsed = JSON.parse(value);
return Array.isArray(parsed) ? parsed.map(function (item) { return Number(item); }).filter(function (item) { return !Number.isNaN(item); }) : [];
} catch (_error) {
return [];
}
}
// Convert a HH:MM time string to minutes since midnight.
function parseTimeToMinutes(value) {
const raw = String(value || '').trim();
if (!raw) {
return null;
}
const match = raw.match(/^(\d{2}):(\d{2})/);
if (!match) {
return null;
}
return Number(match[1]) * 60 + Number(match[2]);
}
// Determine whether a slide should be shown at the current time.
function isSlideActive(slide, now) {
const mode = String(slide.schedule_mode || 'always');
if (mode === 'always') {
return true;
}
if (mode === 'dates') {
const start = slide.schedule_start_datetime ? new Date(slide.schedule_start_datetime) : null;
const end = slide.schedule_end_datetime ? new Date(slide.schedule_end_datetime) : null;
if (!start || !end || Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) {
return false;
}
return now >= start && now <= end;
}
if (mode === 'times') {
const days = parseScheduleDays(slide.schedule_days_json);
if (!days.length) {
return false;
}
const day = now.getDay();
if (days.indexOf(day) === -1) {
return false;
}
const startMinutes = parseTimeToMinutes(slide.schedule_start_time);
const endMinutes = parseTimeToMinutes(slide.schedule_end_time);
if (startMinutes === null || endMinutes === null) {
return false;
}
const nowMinutes = now.getHours() * 60 + now.getMinutes();
if (startMinutes <= endMinutes) {
return nowMinutes >= startMinutes && nowMinutes <= endMinutes;
}
return nowMinutes >= startMinutes || nowMinutes <= endMinutes;
}
return true;
}
// Build the cache key for a template layout.
function getTemplateLayoutCacheKey(template) {
var viewportKey = window.innerWidth + 'x' + window.innerHeight;
return [currentPlaylistSignature || '', template && template.id ? template.id : '', viewportKey].join('|');
}
// Build or reuse layout metadata for a template.
function getTemplateLayout(template) {
if (!template || !template.id) {
return null;
}
syncRenderCacheViewport();
var cacheKey = getTemplateLayoutCacheKey(template);
if (Object.prototype.hasOwnProperty.call(templateLayoutCache, cacheKey)) {
return templateLayoutCache[cacheKey];
}
var templateCanvas = getTemplateCanvasSize(template);
var canvasSize = fitCanvasSize(templateCanvas.width, templateCanvas.height, window.innerWidth, window.innerHeight);
var canvasScale = canvasSize.width / templateCanvas.width;
var regions = (template.regions || []).map(function (region) {
var left = (Number(region.x) / templateCanvas.width) * 100;
var top = (Number(region.y) / templateCanvas.height) * 100;
var width = (Number(region.width) / templateCanvas.width) * 100;
var height = (Number(region.height) / templateCanvas.height) * 100;
var baseStyle = 'left:' + left + '%;top:' + top + '%;width:' + width + '%;height:' + height + '%;z-index:' + Number(region.z_index || 0) + ';';
var pixelWidth = Math.max(1, Math.round(Number(region.width || 0) || 1));
var pixelHeight = Math.max(1, Math.round(Number(region.height || 0) || 1));
return {
regionKey: region.region_key,
regionType: region.region_type,
label: region.label,
baseStyle: baseStyle,
pixelWidth: pixelWidth,
pixelHeight: pixelHeight,
fontFamily: region.font_family || null,
fontSize: region.font_size || null,
fontColor: region.font_color || null,
canvasScale: canvasScale
};
});
var layout = {
canvasWidth: canvasSize.width,
canvasHeight: canvasSize.height,
background: template.background_image_path ? '<img class="template-background" src="' + escapeHtml(template.background_image_path) + '" alt="" />' : '',
backgroundColor: template.background_color || '#111111',
regions: regions
};
templateLayoutCache[cacheKey] = layout;
return layout;
}
// Build the cache key for a template render plan.
function getTemplateRenderPlanCacheKey(template) {
return getTemplateLayoutCacheKey(template);
}
// Build or reuse the render plan for a template.
function getTemplateRenderPlan(template) {
if (!template || !template.id) {
return null;
}
syncRenderCacheViewport();
var cacheKey = getTemplateRenderPlanCacheKey(template);
if (Object.prototype.hasOwnProperty.call(templateRenderPlanCache, cacheKey)) {
return templateRenderPlanCache[cacheKey];
}
var layout = getTemplateLayout(template);
var plan = {
layout: layout,
renderRegion: function (region, regionContent) {
if (region.regionType === 'image') {
return renderImageRegion(region, regionContent);
}
if (region.regionType === 'webpage') {
return renderWebpageRegion(region, regionContent);
}
if (region.regionType === 'rtmp') {
return renderRtmpRegion(region, regionContent);
}
if (region.regionType === 'rss') {
return renderRssRegion(region, regionContent);
}
if (region.regionType === 'api') {
return renderApiRegion(region, regionContent);
}
if (region.regionType === 'html') {
return renderHtmlRegion(region, regionContent);
}
return renderTextRegion(region, regionContent);
}
};
templateRenderPlanCache[cacheKey] = plan;
return plan;
}
// Render a template-based slide using the cached layout.
function renderTemplateSlideMarkup(slide) {
const template = slide.template;
const content = slide.content || {};
const plan = getTemplateRenderPlan(template);
const layout = plan ? plan.layout : null;
const regions = layout ? layout.regions.map(function (region) {
const regionContent = content[region.regionKey] || {};
return plan.renderRegion(region, regionContent);
}).join('') : '';
const stageStyle = layout ? 'background-color:' + escapeHtml(layout.backgroundColor || '#111111') + ';' : '';
return renderSlideShell(slide, '', (layout ? layout.canvasWidth : 0) + 'px', (layout ? layout.canvasHeight : 0) + 'px', '<div class="template-stage" style="' + stageStyle + '">' + (layout ? layout.background : '') + regions + '</div>');
}
// Media rendering helpers.
// Build the direct media element for a slide.
function renderMediaSlideContent(slide) {
if (slide.kind === 'image') {
return '<img src="' + escapeHtml(slide.media_url) + '" alt="slide" />';
}
return '';
}
// Render a slide that contains direct media content.
function renderMediaSlideMarkup(slide) {
const canvasSize = fitCanvasSize(16, 9, window.innerWidth, window.innerHeight);
const media = renderMediaSlideContent(slide);
return renderSlideShell(slide, 'slide-media', canvasSize.width + 'px', canvasSize.height + 'px', media);
}
// Render the shared slide shell around slide-specific inner content.
function renderSlideShell(slide, canvasClass, canvasWidth, canvasHeight, innerHtml) {
const body = slide.body ? '<div class="body">' + escapeHtml(slide.body) + '</div>' : '';
const className = canvasClass ? 'slide-canvas ' + canvasClass : 'slide-canvas';
return '<div class="slide"><div class="' + className + '" style="width:' + canvasWidth + ';height:' + canvasHeight + ';">' + innerHtml + body + '</div></div>';
}
// Build the cache key for rendered slide markup.
function getSlideMarkupCacheKey(slide) {
var viewportKey = window.innerWidth + 'x' + window.innerHeight;
return [currentPlaylistSignature || '', slide && slide.id ? slide.id : '', slide && slide.template_id ? slide.template_id : '', viewportKey].join('|');
}
// Look up a previously rendered slide in the cache.
function getCachedSlideMarkup(slide) {
var cacheKey = getSlideMarkupCacheKey(slide);
return Object.prototype.hasOwnProperty.call(slideMarkupCache, cacheKey) ? slideMarkupCache[cacheKey] : null;
}
// Store rendered slide markup in the cache.
function setCachedSlideMarkup(slide, markup) {
syncRenderCacheViewport();
slideMarkupCache[getSlideMarkupCacheKey(slide)] = markup;
}
// Slide rendering and markup cache helpers.
// Choose the right slide renderer and cache the result.
function buildSlideMarkup(slide) {
lastRenderedSlide = slide || null;
syncBlackoutState();
var cachedMarkup = getCachedSlideMarkup(slide);
if (cachedMarkup) {
return cachedMarkup;
}
var markup = '';
if (slide.template_id && slide.template) {
markup = renderTemplateSlideMarkup(slide);
setCachedSlideMarkup(slide, markup);
return markup;
}
markup = renderMediaSlideMarkup(slide);
setCachedSlideMarkup(slide, markup);
return markup;
}
+141
View File
@@ -0,0 +1,141 @@
const CACHE_VERSION = 'v1';
const PAGE_CACHE = `pulse-signage-player-pages-${CACHE_VERSION}`;
const ASSET_CACHE = `pulse-signage-player-assets-${CACHE_VERSION}`;
const MEDIA_CACHE = `pulse-signage-player-media-${CACHE_VERSION}`;
const PLAYLIST_CACHE = `pulse-signage-player-playlists-${CACHE_VERSION}`;
function normalizeRequest(request) {
const url = new URL(request.url);
return new Request(`${url.origin}${url.pathname}`, {
method: 'GET',
headers: request.headers,
mode: 'same-origin',
credentials: 'same-origin'
});
}
async function cacheResponse(cacheName, request, response, cacheKeyRequest) {
if (!response || !response.ok) {
return;
}
const cache = await caches.open(cacheName);
await cache.put(cacheKeyRequest || request, response.clone());
}
async function networkFirst(request, cacheName, cacheKeyRequest) {
try {
const response = await fetch(request);
if (response && response.ok) {
await cacheResponse(cacheName, request, response, cacheKeyRequest);
return response;
}
if (response && response.status === 304) {
const cached = await caches.match(cacheKeyRequest || request);
if (cached) {
return cached;
}
}
const cached = await caches.match(cacheKeyRequest || request);
if (cached) {
return cached;
}
return response;
} catch (_error) {
const cached = await caches.match(cacheKeyRequest || request);
if (cached) {
return cached;
}
throw _error;
}
}
async function cacheFirst(request, cacheName) {
const cached = await caches.match(request);
if (cached) {
return cached;
}
const response = await fetch(request);
if (response && response.ok) {
await cacheResponse(cacheName, request, response);
}
return response;
}
async function staleWhileRevalidate(request, cacheName) {
const cached = await caches.match(request);
const networkPromise = fetch(request).then(async function (response) {
if (response && response.ok) {
await cacheResponse(cacheName, request, response);
}
return response;
}).catch(function () {
return null;
});
if (cached) {
networkPromise.catch(function () {
return null;
});
return cached;
}
const networkResponse = await networkPromise;
if (networkResponse) {
return networkResponse;
}
return new Response('', { status: 504, statusText: 'Offline' });
}
self.addEventListener('install', function (event) {
self.skipWaiting();
event.waitUntil(Promise.resolve());
});
self.addEventListener('activate', function (event) {
event.waitUntil((async function () {
const expected = [PAGE_CACHE, ASSET_CACHE, MEDIA_CACHE, PLAYLIST_CACHE];
const keys = await caches.keys();
await Promise.all(keys.filter(function (key) {
return expected.indexOf(key) === -1;
}).map(function (key) {
return caches.delete(key);
}));
await self.clients.claim();
})());
});
self.addEventListener('fetch', function (event) {
const request = event.request;
if (request.method !== 'GET') {
return;
}
const url = new URL(request.url);
if (url.origin !== self.location.origin) {
return;
}
if (url.pathname === '/sw.js') {
return;
}
if (url.pathname.startsWith('/assets/')) {
event.respondWith(cacheFirst(request, ASSET_CACHE));
return;
}
if (url.pathname.startsWith('/media/')) {
event.respondWith(staleWhileRevalidate(request, MEDIA_CACHE));
return;
}
if (request.mode === 'navigate' || url.pathname === '/' || url.pathname === '/onboard' || /^\/screen\/[^/]+$/.test(url.pathname)) {
event.respondWith(networkFirst(request, PAGE_CACHE, normalizeRequest(request)));
return;
}
if (url.pathname.startsWith('/api/screens/') && url.pathname.endsWith('/playlist')) {
event.respondWith(networkFirst(request, PLAYLIST_CACHE, normalizeRequest(request)));
}
});