/** * AKSRT Site Notice — Popup notice with cookie-based frequency control * * Identity-based policy check is done server-side (PHP). * JS only handles: show/hide animation, cookie dismissal, expiry. * * @package AKSRT */ (function () { 'use strict'; var el = document.getElementById('aksrt-site-notice'); if (!el) return; // 弹窗周期(小时),0=永不过期 var expiresHours = parseInt(el.dataset.expires || '24', 10) || 0; var cookieName = 'aksrt_notice_seen'; function getCookie(name) { var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); return match ? decodeURIComponent(match[2]) : null; } function setCookie(name, value, days) { var expires = ''; if (days) { var date = new Date(); date.setTime(date.getTime() + days * 86400000); expires = '; expires=' + date.toUTCString(); } document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=/; SameSite=Lax'; } function shouldShow() { if (expiresHours === 0) return true; // 永不过期,总是显示 var seen = getCookie(cookieName); if (!seen) return true; var seenTs = parseInt(seen, 10); if (!seenTs || seenTs <= 0) return true; var now = Math.floor(Date.now() / 1000); return (now - seenTs) >= (expiresHours * 3600); } function dismiss() { // 写入当前时间戳 var days = expiresHours > 0 ? Math.ceil(expiresHours / 24) : 365; setCookie(cookieName, String(Math.floor(Date.now() / 1000)), days); close(); } function close() { el.style.opacity = '0'; setTimeout(function () { el.style.display = 'none'; }, 300); } function show() { el.style.display = ''; el.style.opacity = '0'; requestAnimationFrame(function () { el.style.transition = 'opacity 0.3s'; el.style.opacity = '1'; }); } // Bind events var closeBtn = el.querySelector('.aksrt-site-notice__close'); var overlay = el.querySelector('.aksrt-site-notice__overlay'); // 所有 dismiss 按钮(包括 data-dismiss="notice" 的按钮和链接) var dismissBtns = el.querySelectorAll('[data-dismiss="notice"]'); if (closeBtn) closeBtn.addEventListener('click', dismiss); if (overlay) overlay.addEventListener('click', dismiss); for (var i = 0; i < dismissBtns.length; i++) { dismissBtns[i].addEventListener('click', dismiss); } document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && el.style.display !== 'none') { dismiss(); } }); // Show on load if needed if (shouldShow()) { // Delay slightly so it doesn't flash on fast navigations setTimeout(show, 500); } })();