|
1 | | -// Wait for the page to be fully loaded and interactive |
2 | | -document.addEventListener("DOMContentLoaded", function () { |
3 | | - // Check if we already have consent to avoid showing the banner unnecessarily |
4 | | - if (document.cookie.indexOf("cookieyes-consent") !== -1) { |
5 | | - // If we already have consent, load the CookieYes script immediately |
6 | | - loadCookieScript(); |
7 | | - return; |
| 1 | +(function () { |
| 2 | + const COOKIEYES_ID = "cookieyes"; |
| 3 | + const COOKIEYES_SRC = |
| 4 | + "https://cdn-cookieyes.com/client_data/dedcd40fe7e8316d7512b294/script.js"; |
| 5 | + const FALLBACK_DELAY_MS = 2000; |
| 6 | + |
| 7 | + function hasCookie(name) { |
| 8 | + const cookies = document.cookie ? document.cookie.split(";") : []; |
| 9 | + const prefix = `${encodeURIComponent(name)}=`; |
| 10 | + |
| 11 | + for (const c of cookies) { |
| 12 | + const trimmed = c.trim(); |
| 13 | + if (trimmed.startsWith(prefix)) return true; |
| 14 | + } |
| 15 | + return false; |
8 | 16 | } |
9 | 17 |
|
10 | | - // If we don't have consent yet, delay loading the cookie banner |
11 | | - // This ensures it doesn't compete with critical content |
12 | | - setTimeout(function () { |
13 | | - loadCookieScript(); |
14 | | - }, 2000); // 2-second delay to improve LCP |
15 | | -}); |
16 | | - |
17 | | -// Function to load the CookieYes script |
18 | | -function loadCookieScript() { |
19 | | - const script = document.createElement("script"); |
20 | | - script.defer = true; |
21 | | - script.id = "cookieyes"; |
22 | | - script.src = |
23 | | - "https://cdn-cookieyes.com/client_data/dedcd40fe7e8316d7512b294/script.js"; |
| 18 | + function alreadyInjected() { |
| 19 | + return ( |
| 20 | + document.getElementById(COOKIEYES_ID) != null || |
| 21 | + Array.from(document.scripts).some((s) => s.src === COOKIEYES_SRC) |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + function loadCookieScript() { |
| 26 | + if (alreadyInjected()) return; |
| 27 | + |
| 28 | + const script = document.createElement("script"); |
| 29 | + script.id = COOKIEYES_ID; |
| 30 | + script.src = COOKIEYES_SRC; |
| 31 | + script.async = true; |
24 | 32 |
|
25 | | - document.body.appendChild(script); |
26 | | -} |
| 33 | + (document.head || document.documentElement).appendChild(script); |
| 34 | + } |
| 35 | + |
| 36 | + function scheduleLoad() { |
| 37 | + if (hasCookie("cookieyes-consent")) { |
| 38 | + loadCookieScript(); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Minimize impact on LCP/INP |
| 43 | + if (typeof window.requestIdleCallback === "function") { |
| 44 | + window.requestIdleCallback(() => loadCookieScript(), { |
| 45 | + timeout: FALLBACK_DELAY_MS, |
| 46 | + }); |
| 47 | + } else { |
| 48 | + window.setTimeout(loadCookieScript, FALLBACK_DELAY_MS); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Handle all states safely |
| 53 | + if (document.readyState === "loading") { |
| 54 | + document.addEventListener("DOMContentLoaded", scheduleLoad, { once: true }); |
| 55 | + } else { |
| 56 | + scheduleLoad(); |
| 57 | + } |
| 58 | +})(); |
0 commit comments