Elffuss BraidHub

kikocis/continue public

main · 12.6 KiBRawPermalink
1// Cookie Banner Implementation for Continue Documentation
2// This script implements a cookie consent banner in offline mode
3
4(function () {
5 "use strict";
6
7 // Configuration
8 const COOKIE_NAME = "continue_cookie_consent";
9 const COOKIE_EXPIRY_DAYS = 365;
10
11 // Default consent state
12 const defaultConsent = {
13 necessary: true,
14 analytics: false,
15 marketing: false,
16 };
17
18 // Wait for the page to fully load
19 if (document.readyState === "loading") {
20 document.addEventListener("DOMContentLoaded", initializeCookieBanner);
21 } else {
22 initializeCookieBanner();
23 }
24
25 function initializeCookieBanner() {
26 // Check if banner is already initialized
27 if (window.cookieBannerInitialized) {
28 return;
29 }
30 window.cookieBannerInitialized = true;
31
32 // Get stored consent
33 const storedConsent = getStoredConsent();
34
35 // Apply stored consent or show banner
36 if (storedConsent) {
37 applyConsent(storedConsent);
38 } else {
39 // Set default consent (deny all except necessary)
40 applyConsent(defaultConsent);
41 showCookieBanner();
42 }
43 }
44
45 function getStoredConsent() {
46 const cookie = document.cookie
47 .split(";")
48 .find((c) => c.trim().startsWith(COOKIE_NAME + "="));
49 if (cookie) {
50 try {
51 return JSON.parse(decodeURIComponent(cookie.split("=")[1]));
52 } catch (e) {
53 console.error("Failed to parse consent cookie:", e);
54 }
55 }
56 return null;
57 }
58
59 function setConsentCookie(consent) {
60 const date = new Date();
61 date.setTime(date.getTime() + COOKIE_EXPIRY_DAYS * 24 * 60 * 60 * 1000);
62 const expires = "expires=" + date.toUTCString();
63 document.cookie =
64 COOKIE_NAME +
65 "=" +
66 encodeURIComponent(JSON.stringify(consent)) +
67 ";" +
68 expires +
69 ";path=/;SameSite=Lax";
70 }
71
72 function applyConsent(consent) {
73 // Apply Google Analytics consent
74 if (window.gtag) {
75 window.gtag("consent", "update", {
76 analytics_storage: consent.analytics ? "granted" : "denied",
77 ad_user_data: consent.marketing ? "granted" : "denied",
78 ad_personalization: consent.marketing ? "granted" : "denied",
79 });
80 }
81
82 // Apply PostHog consent
83 if (window.posthog) {
84 if (consent.analytics) {
85 window.posthog.opt_in_capturing();
86 } else {
87 window.posthog.opt_out_capturing();
88 }
89 }
90
91 // Store consent
92 setConsentCookie(consent);
93 }
94
95 function showCookieBanner() {
96 // Create banner (no overlay - users can continue browsing)
97 const banner = document.createElement("div");
98 banner.className = "cookie-banner";
99 banner.innerHTML = `
100 <div class="cookie-banner-content">
101 <div class="cookie-banner-text">
102 <p>We use cookies to enhance your experience on our documentation site. By continuing to browse, you agree to our use of cookies.</p>
103 </div>
104 <div class="cookie-banner-actions">
105 <button class="cookie-btn cookie-btn-secondary" onclick="window.showPreferencesDialog()">Manage Preferences</button>
106 <button class="cookie-btn cookie-btn-primary" onclick="window.acceptAllCookies()">Accept All</button>
107 </div>
108 </div>
109 `;
110
111 document.body.appendChild(banner);
112
113 // Make functions globally available
114 window.acceptAllCookies = function () {
115 const consent = {
116 necessary: true,
117 analytics: true,
118 marketing: true,
119 };
120 applyConsent(consent);
121 hideCookieBanner();
122 };
123
124 window.showPreferencesDialog = showPreferencesDialog;
125 }
126
127 function hideCookieBanner() {
128 const banner = document.querySelector(".cookie-banner");
129 if (banner) banner.remove();
130 }
131
132 function showPreferencesDialog() {
133 hideCookieBanner();
134
135 const currentConsent = getStoredConsent() || defaultConsent;
136
137 // Create dialog overlay
138 const dialogOverlay = document.createElement("div");
139 dialogOverlay.className = "cookie-dialog-overlay";
140 dialogOverlay.onclick = function (e) {
141 if (e.target === dialogOverlay) {
142 hidePreferencesDialog();
143 }
144 };
145
146 // Create dialog
147 const dialog = document.createElement("div");
148 dialog.className = "cookie-dialog";
149 dialog.innerHTML = `
150 <div class="cookie-dialog-header">
151 <h2>Cookie Preferences</h2>
152 <button class="cookie-dialog-close" onclick="window.hidePreferencesDialog()">×</button>
153 </div>
154 <div class="cookie-dialog-body">
155 <p>We use cookies and similar technologies to help personalize content and offer a better experience. You can customize your preferences below:</p>
156
157 <div class="cookie-category">
158 <div class="cookie-category-header">
159 <label class="cookie-switch">
160 <input type="checkbox" id="cookie-necessary" checked disabled>
161 <span class="cookie-slider"></span>
162 </label>
163 <div class="cookie-category-info">
164 <h3>Necessary Cookies</h3>
165 <p>These cookies are essential for the website to function properly and cannot be disabled.</p>
166 </div>
167 </div>
168 </div>
169
170 <div class="cookie-category">
171 <div class="cookie-category-header">
172 <label class="cookie-switch">
173 <input type="checkbox" id="cookie-analytics" ${currentConsent.analytics ? "checked" : ""}>
174 <span class="cookie-slider"></span>
175 </label>
176 <div class="cookie-category-info">
177 <h3>Analytics Cookies</h3>
178 <p>These cookies help us understand how visitors interact with our website by collecting and reporting information anonymously.</p>
179 </div>
180 </div>
181 </div>
182
183 <div class="cookie-category">
184 <div class="cookie-category-header">
185 <label class="cookie-switch">
186 <input type="checkbox" id="cookie-marketing" ${currentConsent.marketing ? "checked" : ""}>
187 <span class="cookie-slider"></span>
188 </label>
189 <div class="cookie-category-info">
190 <h3>Identification Cookies</h3>
191 <p>These cookies help us understand how you use our documentation and allow us to recognize you across visits. We use this data to improve your experience and help you get the most value from our products.</p>
192 </div>
193 </div>
194 </div>
195
196 </div>
197 <div class="cookie-dialog-footer">
198 <button class="cookie-btn cookie-btn-secondary" onclick="window.rejectAllCookies()">Reject All</button>
199 <button class="cookie-btn cookie-btn-primary" onclick="window.savePreferences()">Save Preferences</button>
200 </div>
201 `;
202
203 dialogOverlay.appendChild(dialog);
204 document.body.appendChild(dialogOverlay);
205
206 // Make functions globally available
207 window.hidePreferencesDialog = hidePreferencesDialog;
208
209 window.rejectAllCookies = function () {
210 const consent = {
211 necessary: true,
212 analytics: false,
213 marketing: false,
214 };
215 applyConsent(consent);
216 hidePreferencesDialog();
217 };
218
219 window.savePreferences = function () {
220 const consent = {
221 necessary: true,
222 analytics: document.getElementById("cookie-analytics").checked,
223 marketing: document.getElementById("cookie-marketing").checked,
224 };
225 applyConsent(consent);
226 hidePreferencesDialog();
227 };
228 }
229
230 function hidePreferencesDialog() {
231 const dialogOverlay = document.querySelector(".cookie-dialog-overlay");
232 if (dialogOverlay) dialogOverlay.remove();
233 }
234
235 // Add styles
236 const style = document.createElement("style");
237 style.textContent = `
238
239 .cookie-banner {
240 position: fixed;
241 bottom: 0;
242 left: 0;
243 right: 0;
244 background: #1a1a1a;
245 color: #ffffff;
246 padding: 1.5rem;
247 z-index: 9999;
248 box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
249 }
250
251 .cookie-banner-content {
252 max-width: 1200px;
253 margin: 0 auto;
254 display: flex;
255 align-items: center;
256 justify-content: space-between;
257 gap: 2rem;
258 flex-wrap: wrap;
259 }
260
261 .cookie-banner-text {
262 flex: 1;
263 min-width: 300px;
264 }
265
266 .cookie-banner-text p {
267 margin: 0;
268 font-size: 14px;
269 line-height: 1.5;
270 }
271
272 .cookie-banner-actions {
273 display: flex;
274 gap: 1rem;
275 flex-wrap: wrap;
276 }
277
278 .cookie-btn {
279 padding: 0.5rem 1.5rem;
280 border: none;
281 border-radius: 4px;
282 font-size: 14px;
283 font-weight: 500;
284 cursor: pointer;
285 transition: all 0.2s;
286 white-space: nowrap;
287 }
288
289 .cookie-btn-primary {
290 background: #6b7280;
291 color: #ffffff;
292 }
293
294 .cookie-btn-primary:hover {
295 background: #4b5563;
296 }
297
298 .cookie-btn-secondary {
299 background: transparent;
300 color: #ffffff;
301 border: 1px solid #6b7280;
302 }
303
304 .cookie-btn-secondary:hover {
305 background: #6b7280;
306 }
307
308 /* Dialog-specific button styles */
309 .cookie-dialog-footer .cookie-btn-secondary {
310 color: #6b7280;
311 border-color: #6b7280;
312 }
313
314 .cookie-dialog-footer .cookie-btn-secondary:hover {
315 color: #ffffff;
316 background: #6b7280;
317 }
318
319 .cookie-dialog-overlay {
320 position: fixed;
321 top: 0;
322 left: 0;
323 right: 0;
324 bottom: 0;
325 background: rgba(0, 0, 0, 0.5);
326 z-index: 10000;
327 display: flex;
328 align-items: center;
329 justify-content: center;
330 padding: 1rem;
331 }
332
333 .cookie-dialog {
334 background: #ffffff;
335 border-radius: 8px;
336 box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
337 max-width: 600px;
338 width: 100%;
339 max-height: 90vh;
340 overflow: hidden;
341 display: flex;
342 flex-direction: column;
343 }
344
345 .cookie-dialog-header {
346 padding: 1.5rem;
347 border-bottom: 1px solid #e5e7eb;
348 display: flex;
349 align-items: center;
350 justify-content: space-between;
351 }
352
353 .cookie-dialog-header h2 {
354 margin: 0;
355 font-size: 1.5rem;
356 color: #1a1a1a;
357 }
358
359 .cookie-dialog-close {
360 background: none;
361 border: none;
362 font-size: 1.5rem;
363 color: #6b7280;
364 cursor: pointer;
365 padding: 0;
366 width: 2rem;
367 height: 2rem;
368 display: flex;
369 align-items: center;
370 justify-content: center;
371 border-radius: 4px;
372 transition: all 0.2s;
373 }
374
375 .cookie-dialog-close:hover {
376 background: #f3f4f6;
377 }
378
379 .cookie-dialog-body {
380 padding: 1.5rem;
381 overflow-y: auto;
382 flex: 1;
383 }
384
385 .cookie-dialog-body > p {
386 margin: 0 0 1.5rem 0;
387 color: #6b7280;
388 line-height: 1.5;
389 }
390
391 .cookie-category {
392 margin-bottom: 1.5rem;
393 padding-bottom: 1.5rem;
394 border-bottom: 1px solid #e5e7eb;
395 }
396
397 .cookie-category:last-child {
398 margin-bottom: 0;
399 padding-bottom: 0;
400 border-bottom: none;
401 }
402
403 .cookie-category-header {
404 display: flex;
405 align-items: flex-start;
406 gap: 1rem;
407 }
408
409 .cookie-category-info {
410 flex: 1;
411 }
412
413 .cookie-category-info h3 {
414 margin: 0 0 0.5rem 0;
415 font-size: 1.1rem;
416 color: #1a1a1a;
417 }
418
419 .cookie-category-info p {
420 margin: 0;
421 color: #6b7280;
422 font-size: 14px;
423 line-height: 1.5;
424 }
425
426 .cookie-switch {
427 position: relative;
428 display: inline-block;
429 width: 48px;
430 height: 24px;
431 flex-shrink: 0;
432 }
433
434 .cookie-switch input {
435 opacity: 0;
436 width: 0;
437 height: 0;
438 }
439
440 .cookie-slider {
441 position: absolute;
442 cursor: pointer;
443 top: 0;
444 left: 0;
445 right: 0;
446 bottom: 0;
447 background-color: #e5e7eb;
448 transition: .4s;
449 border-radius: 24px;
450 }
451
452 .cookie-slider:before {
453 position: absolute;
454 content: "";
455 height: 16px;
456 width: 16px;
457 left: 4px;
458 bottom: 4px;
459 background-color: white;
460 transition: .4s;
461 border-radius: 50%;
462 }
463
464 .cookie-switch input:checked + .cookie-slider {
465 background-color: #6b7280;
466 }
467
468 .cookie-switch input:checked + .cookie-slider:before {
469 transform: translateX(24px);
470 }
471
472 .cookie-switch input:disabled + .cookie-slider {
473 background-color: #9ca3af;
474 cursor: not-allowed;
475 }
476
477 .cookie-dialog-footer {
478 padding: 1.5rem;
479 border-top: 1px solid #e5e7eb;
480 display: flex;
481 justify-content: flex-end;
482 gap: 1rem;
483 }
484
485 @media (max-width: 640px) {
486 .cookie-banner-content {
487 flex-direction: column;
488 align-items: stretch;
489 }
490
491 .cookie-banner-actions {
492 justify-content: stretch;
493 }
494
495 .cookie-btn {
496 flex: 1;
497 }
498
499 .cookie-dialog {
500 margin: 1rem;
501 }
502 }
503 `;
504 document.head.appendChild(style);
505})();