new-site/site/public/js/pw-cookie-consent.js

53 lines
2.6 KiB
JavaScript

/* Performance West — lightweight cookie consent banner.
* CSP-safe (external file, no inline JS). Stores choice in localStorage.
* Non-blocking: analytics/chat already load; this provides notice + a choice
* and a link to the privacy policy, satisfying disclosure requirements without
* a heavy CMP. If declined, we set a flag other scripts can check.
*/
(function () {
"use strict";
var KEY = "pw_cookie_consent"; // values: "accepted" | "declined"
try {
if (localStorage.getItem(KEY)) return; // already chose
} catch (e) { return; } // no storage -> skip silently
function choose(val) {
try { localStorage.setItem(KEY, val); } catch (e) {}
var el = document.getElementById("pw-cookie-banner");
if (el) el.parentNode.removeChild(el);
if (val === "declined") { window.PW_COOKIE_DECLINED = true; }
}
function build() {
if (document.getElementById("pw-cookie-banner")) return;
var bar = document.createElement("div");
bar.id = "pw-cookie-banner";
bar.setAttribute("role", "dialog");
bar.setAttribute("aria-label", "Cookie notice");
bar.style.cssText = [
"position:fixed", "bottom:16px", "right:16px", "z-index:2147483000",
"max-width:380px", "width:calc(100% - 32px)", "background:#0f172a",
"color:#e2e8f0", "border:1px solid #1e293b", "border-radius:14px",
"box-shadow:0 18px 40px rgba(0,0,0,0.35)", "padding:16px 18px",
"font-family:Inter,system-ui,sans-serif", "font-size:13px",
"line-height:1.55"
].join(";");
bar.innerHTML =
'<p style="margin:0 0 12px">We use cookies for essential site functionality, ' +
'analytics, and live chat. See our ' +
'<a href="/privacy" style="color:#5eead4;text-decoration:underline">Privacy Policy</a>.</p>' +
'<div style="display:flex;gap:8px;justify-content:flex-end">' +
'<button type="button" id="pw-cookie-decline" style="cursor:pointer;border:1px solid #334155;background:transparent;color:#cbd5e1;font-weight:600;font-size:12px;padding:8px 14px;border-radius:8px">Decline</button>' +
'<button type="button" id="pw-cookie-accept" style="cursor:pointer;border:0;background:#14b8a6;color:#04211d;font-weight:700;font-size:12px;padding:8px 16px;border-radius:8px">Accept</button>' +
"</div>";
document.body.appendChild(bar);
document.getElementById("pw-cookie-accept").addEventListener("click", function () { choose("accepted"); });
document.getElementById("pw-cookie-decline").addEventListener("click", function () { choose("declined"); });
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", build);
} else {
build();
}
})();