Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
179 lines (151 loc) · 4.77 KB
/
Copy pathscript.js
File metadata and controls
179 lines (151 loc) · 4.77 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const cityInput = document.getElementById('cityInput');
const searchBtn = document.getElementById('searchBtn');
const loading = document.getElementById('loading');
const weatherInfo = document.getElementById('weatherInfo');
const error = document.getElementById('error');
const dateTimeEl = document.getElementById('dateTime');
const bgVideo = document.getElementById('bgVideo');
const overlay = document.querySelector('.overlay');
const themeToggle = document.getElementById('themeToggle');
const themeIcon = themeToggle.querySelector('.theme-icon');
const cityNameEl = document.getElementById('cityName');
const weatherIconEl = document.getElementById('weatherIcon');
const temperatureEl = document.getElementById('temperature');
const conditionEl = document.getElementById('condition');
const humidityEl = document.getElementById('humidity');
const windSpeedEl = document.getElementById('windSpeed');
bgVideo.src = 'vedio/5643474-hd_1920_1080_30fps.mp4';
window.addEventListener('load', () => {
const playVideo = () => {
bgVideo.play().then(() => {
bgVideo.playbackRate = 0.5;
}).catch(e => {
setTimeout(playVideo, 500);
});
};
playVideo();
});
setInterval(() => {
if (bgVideo.paused) {
bgVideo.play().catch(() => {});
}
bgVideo.playbackRate = 0.5;
}, 1000);
function updateBackground(isNight) {
if (isNight) {
bgVideo.src = 'vedio/5643474-hd_1920_1080_30fps.mp4';
} else {
bgVideo.src = 'vedio/5643474-hd_1920_1080_30fps.mp4';
}
}
const weatherIcons = {
'01d': '☀️',
'01n': '🌙',
'02d': '⛅',
'02n': '☁️',
'03d': '☁️',
'03n': '☁️',
'04d': '☁️',
'04n': '☁️',
'09d': '🌧️',
'09n': '🌧️',
'10d': '🌦️',
'10n': '🌧️',
'11d': '⛈️',
'11n': '⛈️',
'13d': '❄️',
'13n': '❄️',
'50d': '🌫️',
'50n': '🌫️'
};
function updateDateTime() {
const now = new Date();
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
};
dateTimeEl.textContent = now.toLocaleDateString('en-US', options);
}
function showLoading() {
loading.classList.add('active');
weatherInfo.classList.remove('active');
error.classList.remove('active');
}
function hideLoading() {
loading.classList.remove('active');
}
function showError(message) {
hideLoading();
weatherInfo.classList.remove('active');
error.textContent = message;
error.classList.add('active');
}
function displayWeather(data) {
hideLoading();
error.classList.remove('active');
cityNameEl.textContent = data.city;
const iconCode = data.icon;
weatherIconEl.textContent = weatherIcons[iconCode] || '🌤️';
temperatureEl.innerHTML = `${Math.round(data.temp)}<span>°C</span>`;
conditionEl.textContent = data.condition;
humidityEl.textContent = `${data.humidity}%`;
windSpeedEl.textContent = `${data.wind} m/s`;
if (iconCode.endsWith('n')) {
if (document.body.classList.contains('dark-mode')) {
overlay.style.background = 'rgba(0, 0, 0, 0.7)';
} else {
overlay.style.background = 'rgba(0, 0, 0, 0.5)';
}
} else {
if (document.body.classList.contains('dark-mode')) {
overlay.style.background = 'rgba(0, 0, 0, 0.5)';
} else {
overlay.style.background = 'rgba(0, 0, 0, 0.3)';
}
}
weatherInfo.classList.add('active');
}
async function fetchWeather(city) {
showLoading();
try {
const response = await fetch(`/api/weather?city=${encodeURIComponent(city)}`);
if (!response.ok) {
const errData = await response.json();
throw new Error(errData.error || 'Failed to fetch weather data');
}
const data = await response.json();
displayWeather(data);
} catch (err) {
showError(err.message || 'Something went wrong. Please try again.');
}
}
function handleSearch() {
const city = cityInput.value.trim();
if (!city) {
showError('Please enter a city name');
return;
}
fetchWeather(city);
}
searchBtn.addEventListener('click', handleSearch);
cityInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
handleSearch();
}
});
updateDateTime();
setInterval(updateDateTime, 60000);
cityInput.focus();
themeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-mode');
document.body.classList.toggle('light-mode');
if (document.body.classList.contains('light-mode')) {
themeIcon.src = 'vedio/sun.png';
} else {
themeIcon.src = 'vedio/moon.png';
}
});