PageSourceSearch

https://convivier.com/wp-content/plugins/wp-simple-consent/assets/js/banner.js?ver=1.3.1-1790393429

js convivier.com collected 2026-09-26 03:31:23 UTC 16,342 bytes, 444 lines download raw bytes

1/**
2 * WP Simple Consent - Banner UI JavaScript
3 * 
4 * Logica UI del banner (mostra/nascondi, gestione pulsanti, preferenze)
5 */
6
7(function() {
8    'use strict';
9
10    // Check dependencies
11    if (typeof WPSConsent === 'undefined' || typeof wpscData === 'undefined') {
12        console.warn('WP Simple Consent: Required dependencies not available');
13        return;
14    }
15
16    const banner = document.getElementById('wpsc-banner');
17    const preferencesModal = document.getElementById('wpsc-preferences-modal');
18    const manageCookiesBtn = document.getElementById('wpsc-manage-cookies');
19    
20    // Flag to prevent banner from showing again after consent is saved
21    let consentSaved = false;
22
23    if (!banner || !preferencesModal) {
24        console.warn('WP Simple Consent: Banner or modal elements not found');
25        return;
26    }
27    
28    // IMMEDIATELY hide banner on load to prevent flash - will be shown by JS if needed
29    hideBanner();
30    
31    // Ensure manage cookies button is always hidden
32    if (manageCookiesBtn) {
33        manageCookiesBtn.style.display = 'none';
34        manageCookiesBtn.style.visibility = 'hidden';
35    }
36
37    /**
38     * Show banner - FORCE VISIBILITY
39     */
40    function showBanner() {
41        if (!banner) return;
42        
43        // Don't show banner if consent has already been saved
44        if (consentSaved) {
45            hideBanner();
46            return;
47        }
48        
49        // Force banner visibility using setProperty with important
50        banner.style.setProperty('display', 'block', 'important');
51        banner.style.setProperty('visibility', 'visible', 'important');
52        banner.style.setProperty('opacity', '1', 'important');
53        banner.style.setProperty('position', 'fixed', 'important');
54        banner.style.setProperty('bottom', '0', 'important');
55        banner.style.setProperty('z-index', '999999', 'important');
56        
57        // Hide manage cookies button if it exists
58        if (manageCookiesBtn) {
59            manageCookiesBtn.style.setProperty('display', 'none', 'important');
60            manageCookiesBtn.style.setProperty('visibility', 'hidden', 'important');
61        }
62        
63        // Force visibility of buttons and actions container
64        const actionsContainer = banner.querySelector('.wpsc-banner-actions');
65        if (actionsContainer) {
66            actionsContainer.style.setProperty('display', 'flex', 'important');
67            actionsContainer.style.setProperty('visibility', 'visible', 'important');
68            actionsContainer.style.setProperty('opacity', '1', 'important');
69            actionsContainer.style.setProperty('width', '100%', 'important');
70        }
71        
72        const buttons = banner.querySelectorAll('.wpsc-btn, button[data-wpsc-action]');
73        buttons.forEach(function(btn) {
74            btn.style.setProperty('display', 'inline-block', 'important');
75            btn.style.setProperty('visibility', 'visible', 'important');
76            btn.style.setProperty('opacity', '1', 'important');
77            btn.style.setProperty('pointer-events', 'auto', 'important');
78            btn.style.setProperty('position', 'relative', 'important');
79            btn.style.setProperty('z-index', '1', 'important');
80            btn.style.setProperty('height', 'auto', 'important');
81            btn.style.setProperty('width', 'auto', 'important');
82            btn.style.setProperty('max-width', 'none', 'important');
83            btn.style.setProperty('max-height', 'none', 'important');
84            btn.style.setProperty('min-height', 'auto', 'important');
85            btn.style.setProperty('overflow', 'visible', 'important');
86            btn.style.setProperty('clip', 'auto', 'important');
87            btn.style.setProperty('clip-path', 'none', 'important');
88        });
89        
90        // Ensure buttons are properly rendered
91        if (buttons.length === 0) {
92            console.error('WP Simple Consent: No buttons found in banner!');
93        } else {
94            console.log('WP Simple Consent: Found', buttons.length, 'buttons, forcing visibility');
95        }
96        
97        // Force visibility of message and content
98        const message = banner.querySelector('.wpsc-banner-message');
99        if (message) {
100            message.style.setProperty('display', 'block', 'important');
101            message.style.setProperty('visibility', 'visible', 'important');
102        }
103        
104        const content = banner.querySelector('.wpsc-banner-content');
105        if (content) {
106            content.style.setProperty('display', 'flex', 'important');
107            content.style.setProperty('visibility', 'visible', 'important');
108        }
109        
110        // Focus management
111        const firstButton = banner.querySelector('.wpsc-btn-accept-all, .wpsc-btn');
112        if (firstButton) {
113            setTimeout(function() {
114                firstButton.focus();
115            }, 100);
116        }
117    }
118
119    /**
120     * Hide banner - FORCE HIDDEN with !important
121     */
122    function hideBanner() {
123        if (!banner) return;
124        
125        // Force banner hidden using setProperty with important
126        banner.style.setProperty('display', 'none', 'important');
127        banner.style.setProperty('visibility', 'hidden', 'important');
128        banner.style.setProperty('opacity', '0', 'important');
129        
130        // Do NOT show manage cookies button - it should never be visible
131        if (manageCookiesBtn) {
132            manageCookiesBtn.style.setProperty('display', 'none', 'important');
133            manageCookiesBtn.style.setProperty('visibility', 'hidden', 'important');
134        }
135    }
136
137    /**
138     * Show preferences modal
139     */
140    function showPreferences() {
141        preferencesModal.style.display = 'block';
142        document.body.style.overflow = 'hidden';
143        
144        // Set checkbox states from current consent
145        const consent = WPSConsent.getConsent();
146        const analyticsCheckbox = document.getElementById('wpsc-pref-analytics');
147        const marketingCheckbox = document.getElementById('wpsc-pref-marketing');
148        
149        if (analyticsCheckbox) {
150            analyticsCheckbox.checked = consent.a === true;
151        }
152        if (marketingCheckbox) {
153            marketingCheckbox.checked = consent.m === true;
154        }
155
156        // Focus management
157        const firstCheckbox = preferencesModal.querySelector('input[type="checkbox"]');
158        if (firstCheckbox) {
159            firstCheckbox.focus();
160        }
161    }
162
163    /**
164     * Hide preferences modal
165     */
166    function hidePreferences() {
167        preferencesModal.style.display = 'none';
168        document.body.style.overflow = '';
169    }
170
171    /**
172     * Accept all cookies
173     */
174    function acceptAll() {
175        // Accept both analytics and marketing cookies (true, true)
176        const consent = WPSConsent.setConsent(true, true);
177        
178        // Verify consent was set correctly
179        if (!consent || consent.a !== true || consent.m !== true) {
180            console.error('WP Simple Consent: Failed to accept all cookies');
181            return;
182        }
183        
184        // Mark consent as saved to prevent banner from showing again
185        consentSaved = true;
186        
187        // Hide banner immediately and ensure it stays hidden
188        hideBanner();
189        
190        // Double-check after a short delay to ensure banner stays hidden
191        setTimeout(function() {
192            hideBanner();
193        }, 100);
194    }
195
196    /**
197     * Reject all cookies
198     */
199    function rejectAll() {
200        WPSConsent.setConsent(false, false);
201        // Mark consent as saved to prevent banner from showing again
202        consentSaved = true;
203        hideBanner();
204    }
205
206    /**
207     * Save preferences
208     */
209    function savePreferences() {
210        const analyticsCheckbox = document.getElementById('wpsc-pref-analytics');
211        const marketingCheckbox = document.getElementById('wpsc-pref-marketing');
212        
213        const analytics = analyticsCheckbox ? analyticsCheckbox.checked : false;
214        const marketing = marketingCheckbox ? marketingCheckbox.checked : false;
215        
216        WPSConsent.setConsent(analytics, marketing);
217        // Mark consent as saved to prevent banner from showing again
218        consentSaved = true;
219        hidePreferences();
220        hideBanner();
221    }
222
223    /**
224     * Handle iframe placeholders
225     */
226    function handleIframePlaceholders() {
227        const placeholders = document.querySelectorAll('.wpsc-iframe-placeholder');
228        
229        placeholders.forEach(function(placeholder) {
230            const acceptBtn = placeholder.querySelector('.wpsc-iframe-accept');
231            if (acceptBtn) {
232                acceptBtn.addEventListener('click', function() {
233                    if (WPSConsent.hasMarketing()) {
234                        restoreIframe(placeholder);
235                    } else {
236                        // Show preferences to accept marketing
237                        showPreferences();
238                    }
239                });
240            }
241        });
242    }
243
244    /**
245     * Restore iframe from placeholder
246     */
247    function restoreIframe(placeholder) {
248        const src = placeholder.getAttribute('data-wpsc-src');
249        const type = placeholder.getAttribute('data-wpsc-type');
250        
251        if (!src) {
252            return;
253        }
254
255        const iframe = document.createElement('iframe');
256        iframe.src = src;
257        iframe.setAttribute('frameborder', '0');
258        iframe.setAttribute('allowfullscreen', '');
259        iframe.setAttribute('loading', 'lazy');
260        
261        // Copy any additional attributes from placeholder
262        Array.from(placeholder.attributes).forEach(function(attr) {
263            if (attr.name.startsWith('data-wpsc-')) {
264                return; // Skip our data attributes
265            }
266            if (['class', 'id', 'style'].indexOf(attr.name) === -1) {
267                iframe.setAttribute(attr.name, attr.value);
268            }
269        });
270
271        placeholder.parentNode.replaceChild(iframe, placeholder);
272    }
273
274    /**
275     * Check if banner should be shown
276     */
277    function shouldShowBanner() {
278        const consent = WPSConsent.getConsent();
279        
280        // Show if no consent or version mismatch
281        if (consent.state === 'unknown') {
282            return true;
283        }
284        
285        return false;
286    }
287
288    /**
289     * Check and restore iframes on page load if consent already granted
290     */
291    function checkExistingConsent() {
292        if (WPSConsent.hasMarketing()) {
293            const placeholders = document.querySelectorAll('.wpsc-iframe-placeholder');
294            placeholders.forEach(function(placeholder) {
295                restoreIframe(placeholder);
296            });
297        }
298    }
299
300    /**
301     * Initialize banner
302     */
303    function initBanner() {
304        // Check if banner should be shown
305        const consent = WPSConsent.getConsent();
306        
307        // Only show banner if consent is unknown (no consent saved yet)
308        // This means: no cookie exists OR cookie version doesn't match
309        if (consent.state === 'unknown') {
310            consentSaved = false;
311            // Small delay to ensure DOM is ready and avoid flash
312            setTimeout(function() {
313                if (!consentSaved) {
314                    showBanner();
315                }
316            }, 100);
317        } else {
318            // Consent exists and is valid, mark as saved and hide banner
319            consentSaved = true;
320            hideBanner();
321        }
322
323        // FORCE buttons visibility after a short delay to ensure DOM is ready
324        setTimeout(function() {
325            const allButtons = document.querySelectorAll('#wpsc-banner .wpsc-btn, #wpsc-banner button[data-wpsc-action]');
326            allButtons.forEach(function(btn) {
327                btn.style.setProperty('display', 'inline-block', 'important');
328                btn.style.setProperty('visibility', 'visible', 'important');
329                btn.style.setProperty('opacity', '1', 'important');
330                btn.style.setProperty('height', 'auto', 'important');
331                btn.style.setProperty('width', 'auto', 'important');
332                btn.style.setProperty('max-width', 'none', 'important');
333                btn.style.setProperty('max-height', 'none', 'important');
334                btn.style.setProperty('overflow', 'visible', 'important');
335                btn.style.setProperty('clip', 'auto', 'important');
336                btn.style.setProperty('clip-path', 'none', 'important');
337            });
338            
339            const actionsContainer = document.querySelector('#wpsc-banner .wpsc-banner-actions');
340            if (actionsContainer) {
341                actionsContainer.style.setProperty('display', 'flex', 'important');
342                actionsContainer.style.setProperty('visibility', 'visible', 'important');
343                actionsContainer.style.setProperty('opacity', '1', 'important');
344            }
345        }, 200);
346
347        // Handle iframe placeholders
348        handleIframePlaceholders();
349        
350        // Check existing consent for iframes
351        checkExistingConsent();
352
353        // Listen for consent updates to restore iframes and hide banner
354        document.addEventListener('consentUpdated', function(e) {
355            // Mark consent as saved to prevent banner from showing again
356            consentSaved = true;
357            
358            // Hide banner when consent is updated (user made a choice)
359            hideBanner();
360            
361            // Restore iframes if marketing consent granted
362            if (e.detail.marketing) {
363                const placeholders = document.querySelectorAll('.wpsc-iframe-placeholder');
364                placeholders.forEach(function(placeholder) {
365                    restoreIframe(placeholder);
366                });
367            }
368        });
369    }
370
371    /**
372     * Event delegation for banner actions
373     */
374    function setupEventListeners() {
375        // Banner buttons - use event delegation for better reliability
376        document.addEventListener('click', function(e) {
377            const action = e.target.getAttribute('data-wpsc-action') || 
378                          e.target.closest('[data-wpsc-action]')?.getAttribute('data-wpsc-action');
379            
380            if (!action) return;
381            
382            if (action === 'accept-all') {
383                e.preventDefault();
384                acceptAll();
385            } else if (action === 'reject-all') {
386                e.preventDefault();
387                rejectAll();
388            } else if (action === 'preferences') {
389                e.preventDefault();
390                showPreferences();
391            }
392        });
393
394        // Modal actions
395        preferencesModal.addEventListener('click', function(e) {
396            const action = e.target.getAttribute('data-wpsc-action') || 
397                          e.target.closest('[data-wpsc-action]')?.getAttribute('data-wpsc-action');
398            
399            if (action === 'close-modal') {
400                hidePreferences();
401            } else if (action === 'save-preferences') {
402                savePreferences();
403            }
404        });
405
406        // Manage cookies button - removed, not needed
407
408        // Keyboard navigation
409        document.addEventListener('keydown', function(e) {
410            // ESC to close modal
411            if (e.key === 'Escape' && preferencesModal.style.display === 'block') {
412                hidePreferences();
413            }
414        });
415    }
416
417    // Initialize when DOM is ready - with delay to ensure all scripts loaded
418    function initializeBanner() {
419        // Wait a bit to ensure WPSConsent is available
420        if (typeof WPSConsent === 'undefined') {
421            setTimeout(initializeBanner, 100);
422            return;
423        }
424        
425        // Ensure banner is hidden before checking consent
426        hideBanner();
427        
428        // Small delay to ensure cookie is read correctly
429        setTimeout(function() {
430            // Then check if it should be shown
431            initBanner();
432            setupEventListeners();
433        }, 50);
434    }
435    
436    if (document.readyState === 'loading') {
437        document.addEventListener('DOMContentLoaded', initializeBanner);
438    } else {
439        // If DOM already loaded, wait a bit for scripts
440        setTimeout(initializeBanner, 50);
441    }
442
443})();
444

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.