Something went wrong. Try again.
A fork of https://github.com/crosspoint-reader/crosspoint-reader
Something went wrong. Try again.
26 kB · 836 lines
HTML
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837<!DOCTYPE html><html><head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Settings - CrossPoint Reader</title> <style> :root { --font-color: #333; --bg: #f5f5f5; --title-color: #2c3e50; --card-bg: #FFF; --label-color: #7f8c8d; --border-color: #eee; --accent-color: rgb(110, 154, 130); --accent-hover-color: #5a8c73; --toggle-bg: #ccc; } @media (prefers-color-scheme: dark) { :root { --font-color: #f5f5f5; --bg: #333; --title-color: #ecf0f1; --card-bg: #444; --label-color: #bdc3c7; --border-color: #555; --toggle-bg: #666; color-scheme: dark; } } body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; background-color: var(--bg); color: var(--font-color); } h1 { color: var(--title-color); border-bottom: 2px solid var(--accent-color); padding-bottom: 10px; } h2 { color: var(--title-color); margin-top: 0; } .card { background: var(--card-bg); border-radius: 8px; padding: 20px; margin: 15px 0; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .nav-links { margin: 20px 0; display: flex; gap: 10px; } .nav-links a { padding: 10px 20px; color: var(--font-color); text-decoration: none; border-radius: 4px; } .nav-links a.active { background-color: var(--accent-color); color: white; } .nav-links a:not(.active):hover { background-color: var(--accent-hover-color); color: white; } .setting-row { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--border-color); } .setting-row:last-child { border-bottom: none; } .setting-name { font-weight: 500; color: var(--label-color); flex: 1; min-width: 0; padding-right: 12px; } .setting-control { flex-shrink: 0; } .setting-control select, .setting-control input[type="number"], .setting-control input[type="text"], .setting-control input[type="password"] { padding: 6px 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 0.95em; background: var(--card-bg); } .setting-control select { min-width: 160px; } .setting-control input[type="text"], .setting-control input[type="password"] { width: 220px; } .setting-control input[type="number"] { width: 80px; } /* Toggle switch */ .toggle-switch { display: inline-block; position: relative; width: 48px; height: 26px; } .toggle-switch input { opacity: 0; width: 0; height: 0; } .toggle-slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: var(--toggle-bg); border-radius: 26px; transition: 0.3s; } .toggle-slider:before { position: absolute; content: ""; height: 20px; width: 20px; left: 3px; bottom: 3px; background-color: white; border-radius: 50%; transition: 0.3s; } .toggle-switch input:checked + .toggle-slider { background-color: var(--accent-color); } .toggle-switch input:checked + .toggle-slider:before { transform: translateX(22px); } .save-container { text-align: center; margin: 20px 0; } .save-btn { background-color: #27ae60; color: white; padding: 12px 40px; border: none; border-radius: 4px; cursor: pointer; font-size: 1.1em; font-weight: 600; } .save-btn:hover { background-color: #219a52; } .save-btn:disabled { background-color: #95a5a6; cursor: not-allowed; } .message { padding: 12px; border-radius: 4px; margin: 15px 0; text-align: center; display: none; } .message.success { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; } .message.error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } .loader-container { display: flex; justify-content: center; align-items: center; margin: 20px 0; } .loader { width: 48px; height: 48px; border: 5px solid #AAA; border-bottom-color: transparent; border-radius: 50%; display: inline-block; box-sizing: border-box; animation: rotation 1s linear infinite; } @keyframes rotation { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } .opds-server { border: 1px solid var(--border-color); border-radius: 6px; padding: 12px; margin: 10px 0; } .opds-server .setting-row:last-child { border-bottom: none; } .opds-actions { display: flex; gap: 8px; margin-top: 8px; } .btn-small { padding: 6px 14px; border: none; border-radius: 4px; cursor: pointer; font-size: 0.9em; } .btn-add { background-color: var(--accent-color); color: white; } .btn-add:hover { background-color: var(--accent-hover-color); } .btn-delete { background-color: #e74c3c; color: white; } .btn-delete:hover { background-color: #c0392b; } .btn-save-server { background-color: #27ae60; color: white; } .btn-save-server:hover { background-color: #219a52; } @media (max-width: 600px) { body { padding: 10px; font-size: 14px; } .card { padding: 12px; margin: 10px 0; } h1 { font-size: 1.3em; } .nav-links a { padding: 8px 12px; font-size: 0.9em; } .setting-row { flex-wrap: wrap; gap: 6px; } .setting-control select, .setting-control input[type="text"], .setting-control input[type="password"] { min-width: 0; width: unset; } } </style></head><body> <h1>📚 CrossPoint Reader</h1>
<div class="nav-links"> <a href="/">Home</a> <a href="/files">File Manager</a> <a href="/settings" class="active">Settings</a> <a href="/fonts">Fonts</a> </div>
<div id="message" class="message"></div>
<div id="settings-container"> <div class="loader-container"> <span class="loader"></span> </div> </div>
<div class="save-container" id="save-container" style="display:none;"> <button class="save-btn" id="saveBtn" onclick="saveSettings()">Save Settings</button> </div>
<div id="wifi-container"></div> <div id="opds-container"></div>
<div class="card"> <p style="text-align: center; color: #95a5a6; margin: 0;"> CrossPoint E-Reader • Open Source </p> </div>
<script> let allSettings = []; let originalValues = {}; let preserveQuickResumeTimeoutOn = false; let quickResumeTimeoutAutoEnabled = false; const SLEEP_SCREEN_MODE = { QUICK_RESUME: 6 };
function escapeHtml(unsafe) { return unsafe .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """) .replaceAll("'", "'"); }
function showMessage(text, isError) { const msg = document.getElementById('message'); msg.textContent = text; msg.className = 'message ' + (isError ? 'error' : 'success'); msg.style.display = 'block'; setTimeout(function() { msg.style.display = 'none'; }, 4000); }
function renderControl(setting) { const id = 'setting-' + setting.key;
if (setting.type === 'toggle') { const checked = setting.value ? 'checked' : ''; return '<label class="toggle-switch">' + '<input type="checkbox" id="' + id + '" ' + checked + ' onchange="handleSettingChanged(\'' + setting.key + '\')">' + '<span class="toggle-slider"></span></label>'; }
if (setting.type === 'enum') { let html = '<select id="' + id + '" onchange="handleSettingChanged(\'' + setting.key + '\')">'; setting.options.forEach(function(opt, idx) { const selected = idx === setting.value ? ' selected' : ''; html += '<option value="' + idx + '"' + selected + '>' + escapeHtml(opt) + '</option>'; }); html += '</select>'; return html; }
if (setting.type === 'value') { return '<input type="number" id="' + id + '" value="' + setting.value + '"' + ' min="' + setting.min + '" max="' + setting.max + '" step="' + setting.step + '"' + ' onchange="handleSettingChanged(\'' + setting.key + '\')">'; }
if (setting.type === 'string') { const inputType = setting.name.toLowerCase().includes('password') ? 'password' : 'text'; const val = setting.value || ''; return '<input type="' + inputType + '" id="' + id + '" value="' + escapeHtml(val) + '"' + ' oninput="handleSettingChanged(\'' + setting.key + '\')">'; }
return ''; }
function getValue(setting) { const el = document.getElementById('setting-' + setting.key); if (!el) return undefined;
if (setting.type === 'toggle') { return el.checked ? 1 : 0; } if (setting.type === 'enum') { return parseInt(el.value, 10); } if (setting.type === 'value') { return parseInt(el.value, 10); } if (setting.type === 'string') { return el.value; } return undefined; }
function markChanged() { document.getElementById('saveBtn').disabled = false; }
function findSetting(key) { return allSettings.find(function(s) { return s.key === key; }); }
function getControl(key) { return document.getElementById('setting-' + key); }
function getControlValue(key) { const setting = findSetting(key); const el = getControl(key); if (!setting || !el) return undefined;
if (setting.type === 'toggle') { return el.checked ? 1 : 0; } if (setting.type === 'enum' || setting.type === 'value') { return parseInt(el.value, 10); } return el.value; }
function setControlValue(key, value) { const setting = findSetting(key); const el = getControl(key); if (!setting || !el) return false;
if (setting.type === 'toggle') { const checked = !!value; if (el.checked === checked) return false; el.checked = checked; return true; }
const nextValue = String(value); if (el.value === nextValue) return false; el.value = nextValue; return true; }
function isQuickResumeSleepScreenSelected() { const sleepScreen = findSetting('sleepScreen'); const sleepScreenValue = getControlValue('sleepScreen'); if (!sleepScreen || sleepScreenValue === undefined || !Number.isFinite(sleepScreenValue)) return false;
let selectedValue = sleepScreenValue; if (Array.isArray(sleepScreen.values)) { selectedValue = Number(sleepScreen.values[sleepScreenValue]); } else if (Array.isArray(sleepScreen.options)) { const selectedOption = sleepScreen.options[sleepScreenValue]; if (selectedOption && typeof selectedOption === 'object' && 'value' in selectedOption) { selectedValue = Number(selectedOption.value); } }
return selectedValue === SLEEP_SCREEN_MODE.QUICK_RESUME; }
function syncQuickResumeTimeoutForSleepScreen(sleepScreenChanged, quickResumeTimeoutChanged) { const timeoutValue = getControlValue('quickResumeSleepScreen'); if (timeoutValue === undefined) return false;
let changed = false; if (quickResumeTimeoutChanged) { preserveQuickResumeTimeoutOn = timeoutValue === 1; quickResumeTimeoutAutoEnabled = false; }
if (isQuickResumeSleepScreenSelected()) { if (timeoutValue !== 1) { changed = setControlValue('quickResumeSleepScreen', 1); quickResumeTimeoutAutoEnabled = !preserveQuickResumeTimeoutOn; } else if (sleepScreenChanged && !preserveQuickResumeTimeoutOn) { quickResumeTimeoutAutoEnabled = true; } return changed; }
if (sleepScreenChanged && quickResumeTimeoutAutoEnabled && !preserveQuickResumeTimeoutOn) { changed = setControlValue('quickResumeSleepScreen', 0); quickResumeTimeoutAutoEnabled = false; } return changed; }
function updateSettingsVisibility() { const shortPwrBtnVal = getControlValue('shortPwrBtn'); const pwrBtnFootnoteBackRow = document.getElementById('row-setting-pwrBtnFootnoteBack'); if (pwrBtnFootnoteBackRow) { if (shortPwrBtnVal === 4) { pwrBtnFootnoteBackRow.style.display = ''; } else { pwrBtnFootnoteBackRow.style.display = 'none'; } } }
function handleSettingChanged(key) { syncQuickResumeTimeoutForSleepScreen(key === 'sleepScreen', key === 'quickResumeSleepScreen'); if (key === 'shortPwrBtn') { updateSettingsVisibility(); } markChanged(); }
async function loadSettings() { try { const response = await fetch('/api/settings'); if (!response.ok) { throw new Error('Failed to load settings: ' + response.status); } allSettings = await response.json();
// Store original values originalValues = {}; allSettings.forEach(function(s) { originalValues[s.key] = s.value; });
// Group by category const groups = {}; allSettings.forEach(function(s) { if (!groups[s.category]) groups[s.category] = []; groups[s.category].push(s); });
const container = document.getElementById('settings-container'); let html = '';
for (const category in groups) { html += '<div class="card"><h2>' + escapeHtml(category) + '</h2>'; groups[category].forEach(function(s) { html += '<div class="setting-row" id="row-setting-' + s.key + '">' + '<span class="setting-name">' + escapeHtml(s.name) + '</span>' + '<span class="setting-control">' + renderControl(s) + '</span>' + '</div>'; }); html += '</div>'; }
container.innerHTML = html; updateSettingsVisibility(); document.getElementById('save-container').style.display = ''; document.getElementById('saveBtn').disabled = true; preserveQuickResumeTimeoutOn = getControlValue('quickResumeSleepScreen') === 1; quickResumeTimeoutAutoEnabled = false; if (syncQuickResumeTimeoutForSleepScreen(true, false)) { markChanged(); } } catch (e) { console.error(e); document.getElementById('settings-container').innerHTML = '<div class="card"><p style="text-align:center;color:#e74c3c;">Failed to load settings</p></div>'; } }
async function saveSettings() { const btn = document.getElementById('saveBtn'); btn.disabled = true; btn.textContent = 'Saving...';
// Collect only changed values const changes = {}; allSettings.forEach(function(s) { const current = getValue(s); if (current !== undefined && current !== originalValues[s.key]) { changes[s.key] = current; } });
if (Object.keys(changes).length === 0) { showMessage('No changes to save.', false); btn.textContent = 'Save Settings'; return; }
try { const response = await fetch('/api/settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(changes) });
if (!response.ok) { const text = await response.text(); throw new Error(text || 'Save failed'); }
// Update original values to new values for (const key in changes) { originalValues[key] = changes[key]; }
showMessage('Settings saved successfully!', false); } catch (e) { console.error(e); showMessage('Error: ' + e.message, true); }
btn.textContent = 'Save Settings'; }
// --- Wi-Fi Network Management --- // Renders an editable list of saved Wi-Fi networks using /api/wifi endpoints. // Password fields are never pre-filled; when left blank during edit, existing // passwords remain unchanged server-side. let wifiNetworks = [];
function renderWifiNetwork(net, idx) { const isNew = idx === -1; const id = isNew ? 'new' : idx; const lastConnected = net.isLastConnected ? '<div style="margin-top:8px;color:var(--label-color);font-size:0.9em;">Last connected network</div>' : '';
return '<div class="opds-server" id="wifi-' + id + '">' + '<div class="setting-row">' + '<span class="setting-name">SSID</span>' + '<span class="setting-control"><input type="text" id="wifi-ssid-' + id + '" value="' + escapeHtml(net.ssid || '') + '"></span>' + '</div>' + '<div class="setting-row">' + '<span class="setting-name">Password</span>' + '<span class="setting-control"><input type="password" id="wifi-pass-' + id + '" placeholder="' + (net.hasPassword ? '(unchanged)' : '') + '"></span>' + '</div>' + lastConnected + '<div class="opds-actions">' + '<button class="btn-small btn-save-server" onclick="saveWifiNetwork(' + idx + ')">Save</button>' + (isNew ? '' : '<button class="btn-small btn-delete" onclick="deleteWifiNetwork(' + idx + ')">Delete</button>') + '</div>' + '</div>'; }
function renderWifiSection() { const container = document.getElementById('wifi-container'); let html = '<div class="card"><h2>Wi-Fi Networks</h2>';
if (wifiNetworks.length === 0) { html += '<p style="color:var(--label-color);text-align:center;">No Wi-Fi networks saved</p>'; } else { wifiNetworks.forEach(function(net, idx) { html += renderWifiNetwork(net, idx); }); }
html += '<div style="margin-top:12px;text-align:center;">' + '<button class="btn-small btn-add" onclick="addWifiNetwork()">+ Add Network</button>' + '</div></div>'; container.innerHTML = html; }
async function loadWifiNetworks() { try { const resp = await fetch('/api/wifi'); if (!resp.ok) throw new Error('Failed to load'); wifiNetworks = await resp.json(); renderWifiSection(); } catch (e) { console.error('Wi-Fi load error:', e); } }
function addWifiNetwork() { const container = document.getElementById('wifi-container'); const card = container.querySelector('.card'); const addBtn = card.querySelector('.btn-add').parentElement; // Prevent multiple unsaved new-network forms at once (idx -1 -> id "new") if (document.getElementById('wifi-new')) return; addBtn.insertAdjacentHTML('beforebegin', renderWifiNetwork({ssid:'',hasPassword:false,isLastConnected:false}, -1)); }
async function saveWifiNetwork(idx) { const id = idx === -1 ? 'new' : idx; const ssid = document.getElementById('wifi-ssid-' + id).value.trim(); if (!ssid) { showMessage('SSID is required.', true); return; }
const data = { ssid: ssid }; // Only include password when the user actually typed something; omitting it // tells the server to preserve an existing password. const pass = document.getElementById('wifi-pass-' + id).value; if (pass) data.password = pass; if (idx >= 0) data.index = idx;
try { const resp = await fetch('/api/wifi', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }); if (!resp.ok) throw new Error(await resp.text()); showMessage('Wi-Fi network saved!', false); await loadWifiNetworks(); } catch (e) { showMessage('Error: ' + e.message, true); } }
async function deleteWifiNetwork(idx) { if (!confirm('Delete this Wi-Fi network?')) return; try { const resp = await fetch('/api/wifi/delete', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({index: idx}) }); if (!resp.ok) throw new Error(await resp.text()); showMessage('Wi-Fi network deleted', false); await loadWifiNetworks(); } catch (e) { showMessage('Error: ' + e.message, true); } }
// --- OPDS Server Management --- // Dynamically renders an editable list of OPDS servers, communicating with the // /api/opds REST endpoints. Password fields are never pre-filled for security; // the "(unchanged)" placeholder indicates an existing password is preserved on save. let opdsServers = [];
function renderOpdsServer(srv, idx) { const isNew = idx === -1; const id = isNew ? 'new' : idx; return '<div class="opds-server" id="opds-' + id + '">' + '<div class="setting-row">' + '<span class="setting-name">Server Name</span>' + '<span class="setting-control"><input type="text" id="opds-name-' + id + '" value="' + escapeHtml(srv.name || '') + '"></span>' + '</div>' + '<div class="setting-row">' + '<span class="setting-name">URL</span>' + '<span class="setting-control"><input type="text" id="opds-url-' + id + '" value="' + escapeHtml(srv.url || '') + '"></span>' + '</div>' + '<div class="setting-row">' + '<span class="setting-name">Username</span>' + '<span class="setting-control"><input type="text" id="opds-user-' + id + '" value="' + escapeHtml(srv.username || '') + '"></span>' + '</div>' + '<div class="setting-row">' + '<span class="setting-name">Password</span>' + '<span class="setting-control"><input type="password" id="opds-pass-' + id + '" placeholder="' + (srv.hasPassword ? '(unchanged)' : '') + '"></span>' + '</div>' + '<div class="opds-actions">' + '<button class="btn-small btn-save-server" onclick="saveOpdsServer(' + idx + ')">Save</button>' + (isNew ? '' : '<button class="btn-small btn-delete" onclick="deleteOpdsServer(' + idx + ')">Delete</button>') + '</div>' + '</div>'; }
function renderOpdsSection() { const container = document.getElementById('opds-container'); let html = '<div class="card"><h2>OPDS Servers</h2>';
if (opdsServers.length === 0) { html += '<p style="color:var(--label-color);text-align:center;">No OPDS servers configured</p>'; } else { opdsServers.forEach(function(srv, idx) { html += renderOpdsServer(srv, idx); }); }
html += '<div style="margin-top:12px;text-align:center;">' + '<button class="btn-small btn-add" onclick="addOpdsServer()">+ Add Server</button>' + '</div></div>'; container.innerHTML = html; }
async function loadOpdsServers() { try { const resp = await fetch('/api/opds'); if (!resp.ok) throw new Error('Failed to load'); opdsServers = await resp.json(); renderOpdsSection(); } catch (e) { console.error('OPDS load error:', e); } }
function addOpdsServer() { const container = document.getElementById('opds-container'); const card = container.querySelector('.card'); const addBtn = card.querySelector('.btn-add').parentElement; // Prevent multiple unsaved new-server forms at once (idx -1 → id "new") if (document.getElementById('opds-new')) return; addBtn.insertAdjacentHTML('beforebegin', renderOpdsServer({name:'',url:'',username:'',hasPassword:false}, -1)); }
async function saveOpdsServer(idx) { const id = idx === -1 ? 'new' : idx; const data = { name: document.getElementById('opds-name-' + id).value, url: document.getElementById('opds-url-' + id).value, username: document.getElementById('opds-user-' + id).value, }; // Only include password in payload when the user actually typed something; // omitting it tells the server to keep the existing password. const pass = document.getElementById('opds-pass-' + id).value; if (pass) data.password = pass; if (idx >= 0) data.index = idx;
try { const resp = await fetch('/api/opds', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }); if (!resp.ok) throw new Error(await resp.text()); showMessage('OPDS server saved!', false); await loadOpdsServers(); } catch (e) { showMessage('Error: ' + e.message, true); } }
async function deleteOpdsServer(idx) { if (!confirm('Delete this OPDS server?')) return; try { const resp = await fetch('/api/opds/delete', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({index: idx}) }); if (!resp.ok) throw new Error(await resp.text()); showMessage('OPDS server deleted', false); await loadOpdsServers(); } catch (e) { showMessage('Error: ' + e.message, true); } }
// Sequential, not concurrent: the device's web server handles one client // connection at a time, and three simultaneous fetches on page load can // stall long enough inside a single response to trip the firmware watchdog. (async () => { await loadSettings(); await loadWifiNetworks(); await loadOpdsServers(); })();</script></body></html>