June 25 - Optimize codes
This commit is contained in:
+8
-32
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/* ── Password strength meter (shared by base.html and standalone pages) ── */
|
||||
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;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user