June 25 - Optimize codes

This commit is contained in:
2026-06-25 16:40:10 -04:00
parent b4cb292afa
commit ea3648ad90
10 changed files with 85 additions and 56 deletions
+8 -32
View File
@@ -183,6 +183,7 @@ document.addEventListener('DOMContentLoaded', () => {
const SESSION_MS = 30 * 60 * 1000;
var warningTimer = null, expireTimer = null, countdownInterval = null;
var _lastPing = 0;
function isWarningShowing() {
var m = document.getElementById('modal-session-warning');
@@ -208,8 +209,15 @@ document.addEventListener('DOMContentLoaded', () => {
}
function resetTimers() {
var now = Date.now();
clearTimeout(warningTimer); clearTimeout(expireTimer); clearCountdown();
if (isWarningShowing()) closeModal('modal-session-warning');
// Ping the server at most once per minute so the server-side session
// stays alive while the user is active on the page.
if (now - _lastPing > 60000) {
_lastPing = now;
fetch('/ping', { credentials: 'same-origin' }).catch(function(){});
}
warningTimer = setTimeout(function() {
openModal('modal-session-warning');
@@ -311,38 +319,6 @@ document.addEventListener('DOMContentLoaded', function() {
});
});
/* ── Password strength meter ───────────────────────────────── */
function _pwStrengthLevel(pw) {
var score = 0;
if (pw.length >= 8) score++;
if (pw.length >= 12) score++;
if (/[A-Z]/.test(pw)) score++;
if (/[0-9]/.test(pw)) score++;
if (/[^A-Za-z0-9]/.test(pw)) score++;
var map = ['pw-weak','pw-weak','pw-fair','pw-strong','pw-great','pw-great'];
var lbl = ['Weak','Weak','Fair','Strong','Very strong','Very strong'];
return { cls: map[score], label: lbl[score] };
}
function attachPasswordStrength(inputId, fillId, labelId) {
var input = document.getElementById(inputId);
var fill = document.getElementById(fillId);
var lbl = document.getElementById(labelId);
if (!input || !fill || !lbl) return;
input.addEventListener('input', function() {
if (!this.value) {
fill.className = 'pw-strength-fill';
lbl.className = 'pw-strength-label';
lbl.textContent = '';
return;
}
var r = _pwStrengthLevel(this.value);
fill.className = 'pw-strength-fill ' + r.cls;
lbl.className = 'pw-strength-label ' + r.cls;
lbl.textContent = r.label;
});
}
/* ── HTML escape (safe for attributes and text nodes) ──────── */
function esc(str) {
return String(str || '').replace(/[&<>"']/g, function(c) {