// SPDX-License-Identifier: GPL-3.0-or-later // Lights Off Timer: sleep-timer button in the Jellyfin Web video player, next to the volume control. (function () { 'use strict'; if (window.__lightsOffTimer) { return; } window.__lightsOffTimer = true; const ICON = ''; const css = document.createElement('style'); css.textContent = ` .btnLightsOffTimer { position: relative; margin-left: .8em; overflow: visible !important; } .btnLightsOffTimer .lotIcon { font-size: 1.66em; display: inline-flex; } .btnLightsOffTimer.lotActive { color: #ffcc66; } .btnLightsOffTimer .lotBadge { position: absolute; left: 50%; bottom: -0.15em; transform: translateX(-50%); font-size: 0.55em; font-weight: 700; letter-spacing: .02em; white-space: nowrap; background: #ffcc66; color: #111; border-radius: 0.6em; padding: 0 0.4em; line-height: 1.45em; pointer-events: none; } .lotMenu { position: fixed; z-index: 99999; min-width: 17em; white-space: nowrap; background: rgba(20,20,20,.97); color: #fff; border-radius: .5em; box-shadow: 0 .4em 1.6em rgba(0,0,0,.6); padding: .4em 0; font-size: .95em; } .lotMenu .lotHead { padding: .35em 1em .45em; opacity: .7; font-size: .85em; } .lotMenu button { display: flex; justify-content: space-between; width: 100%; padding: .55em 1em; border: 0; background: none; color: inherit; font: inherit; text-align: left; cursor: pointer; } .lotMenu button:hover, .lotMenu button:focus { background: rgba(255,255,255,.12); outline: none; } .lotMenu button.lotOn { color: #ffcc66; } .lotMenu .lotHint { opacity: .55; font-size: .85em; margin-left: 1em; } `; document.head.appendChild(css); let state = null; let menu = null; function api(method, path, body) { const client = window.ApiClient; if (!client) { return Promise.reject(new Error('ApiClient not ready')); } const opts = { type: method, url: client.getUrl('LightsOffTimer/' + path), dataType: 'json' }; if (body) { opts.data = JSON.stringify(body); opts.contentType = 'application/json'; } return client.ajax(opts); } function endLabel() { return state && state.itemType === 'Episode' ? 'End of this episode' : (state && state.itemType === 'Movie' ? 'End of this movie' : 'End of this episode/movie'); } function badgeText() { if (!state || state.mode === 'off') { return ''; } if (state.mode === 'end') { return 'END'; } return state.remainingSeconds != null ? Math.ceil(state.remainingSeconds / 60) + 'm' : ''; } function render() { document.querySelectorAll('.btnLightsOffTimer').forEach(btn => { const on = state && state.mode !== 'off'; btn.classList.toggle('lotActive', !!on); btn.title = on ? (state.mode === 'end' ? 'Sleep timer: ' + endLabel().toLowerCase() : 'Sleep timer: ' + badgeText() + ' left') : 'Sleep timer'; let badge = btn.querySelector('.lotBadge'); const text = badgeText(); if (text && !badge) { badge = document.createElement('span'); badge.className = 'lotBadge'; btn.appendChild(badge); } if (badge) { badge.textContent = text; badge.style.display = text ? '' : 'none'; } }); } function refresh() { return api('GET', 'State').then(s => { state = s; render(); }).catch(() => { /* no session yet */ }); } function set(mode, minutes) { closeMenu(); return api('POST', 'Set', { mode: mode, minutes: minutes || null }).then(s => { state = s; render(); }).catch(err => { console.warn('[LightsOffTimer] set failed', err); }); } function closeMenu() { if (menu) { menu.remove(); menu = null; document.removeEventListener('pointerdown', outside, true); } } function outside(e) { if (menu && !menu.contains(e.target) && !e.target.closest('.btnLightsOffTimer')) { closeMenu(); } } function openMenu(btn) { closeMenu(); const cool = state ? state.cooldownMinutes : 15; menu = document.createElement('div'); menu.className = 'lotMenu'; const head = document.createElement('div'); head.className = 'lotHead'; head.textContent = 'Stop playback, then lights off ' + cool + ' min later'; menu.appendChild(head); const items = [{ mode: 'end', label: endLabel(), hint: 'default' }]; ((state && state.presets) || [15, 30, 45, 60, 90]).forEach(m => items.push({ mode: 'minutes', minutes: m, label: m + ' minutes' })); items.push({ mode: 'off', label: 'Off' }); items.forEach(it => { const b = document.createElement('button'); const active = state && state.mode === it.mode && (it.mode !== 'minutes' || state.minutes === it.minutes); if (active && it.mode !== 'off') { b.classList.add('lotOn'); } b.innerHTML = ''; b.firstChild.textContent = (active && it.mode !== 'off' ? '✓ ' : '') + it.label; b.lastChild.textContent = active && it.mode === 'minutes' && state.remainingSeconds != null ? Math.ceil(state.remainingSeconds / 60) + ' min left' : (it.hint || ''); b.addEventListener('click', e => { e.stopPropagation(); set(it.mode, it.minutes); }); menu.appendChild(b); }); document.body.appendChild(menu); const r = btn.getBoundingClientRect(); const w = menu.offsetWidth, h = menu.offsetHeight; menu.style.left = Math.max(8, Math.min(window.innerWidth - w - 8, r.left + r.width / 2 - w / 2)) + 'px'; menu.style.top = Math.max(8, r.top - h - 8) + 'px'; document.addEventListener('pointerdown', outside, true); (menu.querySelector('.lotOn') || menu.querySelector('button')).focus(); } function addButton(osd) { if (osd.querySelector('.btnLightsOffTimer')) { return; } const volume = osd.querySelector('.osdVolumeSliderContainer'); const mute = osd.querySelector('.buttonMute'); const anchor = volume || mute || osd.querySelector('.btnVideoOsdSettings'); if (!anchor) { return; } const btn = document.createElement('button'); btn.type = 'button'; btn.setAttribute('is', 'paper-icon-button-light'); btn.className = ((mute && mute.className) || 'autoSize paper-icon-button-light').replace(/\bbuttonMute\b/, '') + ' btnLightsOffTimer'; btn.innerHTML = '' + ICON + ''; btn.addEventListener('click', e => { e.preventDefault(); e.stopPropagation(); if (menu) { closeMenu(); return; } refresh().then(() => openMenu(btn)); }); if (volume) { volume.insertAdjacentElement('afterend', btn); } else { anchor.insertAdjacentElement('beforebegin', btn); } refresh(); } function scan() { document.querySelectorAll('.videoOsdBottom-maincontrols, .videoOsdBottom .buttons').forEach(addButton); } new MutationObserver(scan).observe(document.documentElement, { childList: true, subtree: true }); scan(); // Keep the badge current while a player is open. setInterval(() => { if (document.querySelector('.btnLightsOffTimer')) { refresh(); } }, 15000); })();