|
| 1 | +$(document).ready(function () { |
| 2 | + const $askAiButton = $("#ask-ai-button"); |
| 3 | + const currentUrl = window.location.href; |
| 4 | + let lastCookieValue = null; |
| 5 | + |
| 6 | + const getCookie = (name) => { |
| 7 | + const value = `; ${document.cookie}`; |
| 8 | + const parts = value.split(`; ${name}=`); |
| 9 | + if (parts.length === 2) return parts.pop().split(";").shift(); |
| 10 | + return null; |
| 11 | + }; |
| 12 | + |
| 13 | + // Initialize tooltips with HTML support |
| 14 | + $('[data-toggle="tooltip"]').tooltip({ |
| 15 | + html: true, |
| 16 | + }); |
| 17 | + |
| 18 | + // Check if user has accepted cookie policy |
| 19 | + const hasCookieConsent = () => { |
| 20 | + const cookieValue = getCookie("cookiePolicy"); |
| 21 | + if (cookieValue && cookieValue.split(",").includes("2")) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + return false; |
| 25 | + }; |
| 26 | + |
| 27 | + // Update button state based on cookie consent |
| 28 | + const updateButtonState = () => { |
| 29 | + if (hasCookieConsent()) { |
| 30 | + $askAiButton.prop("disabled", false); |
| 31 | + $askAiButton.removeAttr("title"); |
| 32 | + $askAiButton.tooltip("dispose"); |
| 33 | + } else { |
| 34 | + $askAiButton.prop("disabled", true); |
| 35 | + $askAiButton.tooltip("dispose").tooltip({ |
| 36 | + html: true, |
| 37 | + }); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + // Watch for cookie changes using MutationObserver |
| 42 | + const createCookieObserver = () => { |
| 43 | + // Store original document.cookie descriptor |
| 44 | + const originalCookieDescriptor = |
| 45 | + Object.getOwnPropertyDescriptor(Document.prototype, "cookie") || |
| 46 | + Object.getOwnPropertyDescriptor(HTMLDocument.prototype, "cookie"); |
| 47 | + |
| 48 | + if (originalCookieDescriptor && originalCookieDescriptor.configurable) { |
| 49 | + Object.defineProperty(document, "cookie", { |
| 50 | + get() { |
| 51 | + return originalCookieDescriptor.get.call(this); |
| 52 | + }, |
| 53 | + set(value) { |
| 54 | + const result = originalCookieDescriptor.set.call(this, value); |
| 55 | + |
| 56 | + // Check if cookiePolicy was changed |
| 57 | + if (value.includes("cookiePolicy")) { |
| 58 | + setTimeout(updateButtonState, 100); // Small delay to ensure cookie is set |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | + }, |
| 63 | + configurable: true, |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + // Also observe DOM changes for cookie banners |
| 68 | + const observer = new MutationObserver(() => { |
| 69 | + const currentCookieValue = getCookie("cookiePolicy"); |
| 70 | + if (currentCookieValue !== lastCookieValue) { |
| 71 | + lastCookieValue = currentCookieValue; |
| 72 | + updateButtonState(); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + // Observe cookie banner or body changes |
| 77 | + observer.observe(document.body, { |
| 78 | + childList: true, |
| 79 | + subtree: true, |
| 80 | + attributes: true, |
| 81 | + attributeFilter: ["class", "style"], |
| 82 | + }); |
| 83 | + |
| 84 | + return observer; |
| 85 | + }; |
| 86 | + |
| 87 | + lastCookieValue = getCookie("cookiePolicy"); |
| 88 | + updateButtonState(); |
| 89 | + |
| 90 | + // Create cookie observer instead of interval |
| 91 | + const observer = createCookieObserver(); |
| 92 | + |
| 93 | + window.askAiButtonCleanup = function () { |
| 94 | + if (observer) { |
| 95 | + observer.disconnect(); |
| 96 | + } |
| 97 | + }; |
| 98 | +}); |
0 commit comments