-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
142 lines (120 loc) · 5.11 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"use strict";
const CNEMonitor = (() => {
console.log('CNEMonitor module initialized');
const targetDate1 = new Date('2024-07-30T22:00:00Z');
const targetDate2 = new Date('2024-08-29T02:00:00Z');
function updateCounter(elementId, targetDate) {
console.log(`Updating counter: ${elementId}`);
const counter = document.getElementById(elementId);
if (!counter) {
console.error(`Counter element with id ${elementId} not found`);
return;
}
const spans = ['days', 'hours', 'minutes', 'seconds'].map(unit => {
const span = document.createElement('span');
span.className = unit;
counter.appendChild(span);
return span;
});
function update() {
try {
const now = new Date();
const differenceInMs = now - targetDate;
const days = Math.floor(differenceInMs / (1000 * 60 * 60 * 24));
const hours = Math.floor((differenceInMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((differenceInMs % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((differenceInMs % (1000 * 60)) / 1000);
const units = [
{ value: days, singular: 'día', plural: 'días' },
{ value: hours, singular: 'hora', plural: 'horas' },
{ value: minutes, singular: 'minuto', plural: 'minutos' },
{ value: seconds, singular: 'segundo', plural: 'segundos' }
];
units.forEach(({ value, singular, plural }, index) => {
const span = spans[index];
const newText = `<span class="number">${value}</span> ${value === 1 ? singular : plural}`;
if (span.innerHTML !== newText) {
span.innerHTML = newText;
}
});
// Pulse effect removed
} catch (error) {
console.error('Error updating counter:', error);
}
requestAnimationFrame(update);
}
update();
console.log(`Counter ${elementId} update started`);
}
function toggleTheme() {
console.log('Toggling theme');
try {
document.body.classList.toggle('dark-mode');
const isDarkMode = document.body.classList.contains('dark-mode');
localStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
console.log(`Theme set to: ${isDarkMode ? 'dark' : 'light'}`);
} catch (error) {
console.error('Error toggling theme:', error);
}
}
function initializePage() {
console.log('Initializing page');
try {
updateCounter('counter1', targetDate1);
updateCounter('counter2', targetDate2);
const toggleThemeBtn = document.getElementById('toggleTheme');
if (toggleThemeBtn) {
toggleThemeBtn.addEventListener('click', toggleTheme);
console.log('Theme toggle button listener added');
} else {
console.warn('Theme toggle button not found');
}
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark') {
document.body.classList.add('dark-mode');
console.log('Dark mode applied from saved preference');
}
if (typeof AOS !== 'undefined') {
AOS.init({
duration: 1000,
once: true
});
console.log('AOS initialized');
} else {
console.warn('AOS library not found');
}
if (typeof gtag === 'function') {
console.log('Sending Google Analytics pageview event');
gtag('event', 'page_view', {
page_title: document.title,
page_location: window.location.href,
page_path: window.location.pathname
});
console.log('Google Analytics pageview event sent successfully');
} else {
console.warn('Google Analytics gtag function not found. Make sure the Google Analytics script is loaded correctly.');
}
console.log('Page initialization complete');
} catch (error) {
console.error('Error during page initialization:', error);
}
}
return {
init: initializePage,
toggleTheme: toggleTheme
};
})();
document.addEventListener('DOMContentLoaded', () => {
console.log('DOM fully loaded');
CNEMonitor.init();
});
window.addEventListener('error', (event) => {
console.error('Unhandled error:', event.error);
});
window.addEventListener('load', () => {
if (typeof gtag === 'undefined') {
console.warn('Google Analytics might be blocked by an ad blocker or not loaded correctly');
} else {
console.log('Google Analytics (gtag) is available');
}
});