This repository was archived by the owner on Mar 27, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
831 lines (715 loc) · 27.2 KB
/
Copy pathscript.js
File metadata and controls
831 lines (715 loc) · 27.2 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/**
* Web Application with multiple features:
* - Message display with color change
* - Counter with increment/decrement
* - Todo list with Firestore persistence
* - Weather information retrieval from API
* - User directory using JSONPlaceholder API
* - Task tracker with time tracking functionality
*/
// This function shows a message when called
function showMessage() {
document.getElementById("message").textContent = "You clicked the button!";
// Let's make it a bit more interactive
const colors = ['#e74c3c', '#3498db', '#2ecc71', '#f39c12', '#9b59b6'];
const randomColor = colors[Math.floor(Math.random() * colors.length)];
document.getElementById("message").style.color = randomColor;
}
// Counter functionality
let count = 0;
function setupCounter() {
document.getElementById('increment').addEventListener('click', function() {
count++;
updateCounter();
});
document.getElementById('decrement').addEventListener('click', function() {
count--;
updateCounter();
});
document.getElementById('reset').addEventListener('click', function() {
count = 0;
updateCounter();
});
}
function updateCounter() {
document.getElementById('counter').textContent = count;
// Change color based on value
if (count > 0) {
document.getElementById('counter').style.color = '#2ecc71';
} else if (count < 0) {
document.getElementById('counter').style.color = '#e74c3c';
} else {
document.getElementById('counter').style.color = '#2980b9';
}
}
// To-Do List functionality
function setupTodoList() {
const taskInput = document.getElementById('taskInput');
const addTaskBtn = document.getElementById('addTask');
const taskList = document.getElementById('taskList');
const clearTasksBtn = document.getElementById('clearTasks');
// Track tasks in an array
let tasks = [];
// Function to save tasks to Firestore
function saveTasks() {
// Use Firebase Firestore to save tasks
const db = firebase.firestore();
db.collection("tasks").doc("user1").set({
tasks: tasks
})
.then(() => {
console.log("Tasks saved to Firestore");
})
.catch((error) => {
console.error("Error saving tasks: ", error);
// Fallback to localStorage
localStorage.setItem('todoTasks', JSON.stringify(tasks));
});
}
// Function to load tasks from Firestore
function loadTasks() {
const db = firebase.firestore();
db.collection("tasks").doc("user1").get()
.then((doc) => {
if (doc.exists && doc.data().tasks) {
tasks = doc.data().tasks;
renderTasks();
} else {
// Fallback to localStorage if no data in Firestore
const savedTasks = localStorage.getItem('todoTasks');
if (savedTasks) {
tasks = JSON.parse(savedTasks);
renderTasks();
}
}
})
.catch((error) => {
console.error("Error loading tasks: ", error);
// Fallback to localStorage
const savedTasks = localStorage.getItem('todoTasks');
if (savedTasks) {
tasks = JSON.parse(savedTasks);
renderTasks();
}
});
}
// Function to render tasks to the DOM
function renderTasks() {
// Clear the current list
taskList.innerHTML = '';
// Add each task to the list
tasks.forEach(function(task) {
addTaskToDOM(task);
});
}
// Function to add a task to the DOM
function addTaskToDOM(task) {
// Create new list item
const li = document.createElement('li');
// Create checkbox for completion
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.className = 'task-checkbox';
checkbox.checked = task.completed;
checkbox.addEventListener('change', function() {
task.completed = this.checked;
if (this.checked) {
taskSpan.style.textDecoration = 'line-through';
taskSpan.style.color = '#7f8c8d';
} else {
taskSpan.style.textDecoration = 'none';
taskSpan.style.color = 'black';
}
saveTasks();
});
// Create task text span
const taskSpan = document.createElement('span');
taskSpan.textContent = task.text;
taskSpan.className = 'task-text';
// Apply styling based on completion status
if (task.completed) {
taskSpan.style.textDecoration = 'line-through';
taskSpan.style.color = '#7f8c8d';
}
// Create delete button
const deleteBtn = document.createElement('button');
deleteBtn.textContent = 'Delete';
deleteBtn.className = 'delete-task';
deleteBtn.addEventListener('click', function() {
// Remove from DOM
li.remove();
// Remove from array
const index = tasks.findIndex(t => t.id === task.id);
if (index !== -1) {
tasks.splice(index, 1);
saveTasks();
}
});
// Add elements to list item
li.appendChild(checkbox);
li.appendChild(taskSpan);
li.appendChild(deleteBtn);
// Add list item to task list
taskList.appendChild(li);
}
// Add event listeners
addTaskBtn.addEventListener('click', function() {
addNewTask();
});
// Allow pressing Enter to add a task
taskInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
addNewTask();
}
});
// Function to add a new task
function addNewTask() {
const taskText = taskInput.value.trim();
if (taskText !== '') {
// Create a new task object
const task = {
id: Date.now(), // Use timestamp as a unique ID
text: taskText,
completed: false
};
// Add to our array
tasks.push(task);
// Save to Firestore
saveTasks();
// Add to DOM
addTaskToDOM(task);
// Clear the input
taskInput.value = '';
// Focus back on the input
taskInput.focus();
}
}
// Add Clear All functionality if you have that button
if (clearTasksBtn) {
clearTasksBtn.addEventListener('click', function() {
// Confirm before clearing
if (tasks.length > 0 && confirm('Are you sure you want to delete all tasks?')) {
// Clear the array
tasks = [];
// Save empty array to Firestore
saveTasks();
// Clear the DOM
taskList.innerHTML = '';
}
});
}
// Load tasks when the page loads
loadTasks();
}
// Weather API functionality
function setupWeather() {
const cityInput = document.getElementById('cityInput');
const getWeatherBtn = document.getElementById('getWeather');
const weatherResult = document.getElementById('weatherResult');
// API key for OpenWeatherMap
const apiKey = 'dcc4929166ab5bbb8e5dde80ca25f24b';
getWeatherBtn.addEventListener('click', function() {
fetchWeatherData();
});
// Allow pressing Enter to search
cityInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
fetchWeatherData();
}
});
function fetchWeatherData() {
const city = cityInput.value.trim();
if (city === '') {
// If no city is entered, show an error
weatherResult.style.display = 'block';
weatherResult.innerHTML = '<p class="weather-error">Please enter a city name</p>';
return;
}
// Show loading message
weatherResult.style.display = 'block';
weatherResult.innerHTML = '<p class="weather-loading">Loading weather data...</p>';
// Create the API URL with the city and API key
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
console.log("Fetching from:", apiUrl);
// Fetch data from the API
fetch(apiUrl)
.then(function(response) {
console.log("Response status:", response.status);
// Check if the response is okay (status code 200-299)
if (!response.ok) {
if (response.status === 404) {
throw new Error('City not found. Please check the spelling.');
} else if (response.status === 401) {
throw new Error('API key issue. Please try again later.');
} else {
throw new Error('Server error. Please try again later.');
}
}
// Parse the JSON data from the response
return response.json();
})
.then(function(data) {
console.log("Received data:", data);
// We got the data! Now display it
displayWeatherData(data);
})
.catch(function(error) {
// Handle any errors that occurred
console.error("Error:", error);
weatherResult.innerHTML = `<p class="weather-error">Error: ${error.message}</p>`;
});
}
function displayWeatherData(data) {
// Extract relevant data from the API response
const cityName = data.name;
const country = data.sys.country;
const temperature = Math.round(data.main.temp); // Round to whole number
const feelsLike = Math.round(data.main.feels_like);
const description = data.weather[0].description;
const humidity = data.main.humidity;
const windSpeed = data.wind.speed;
// Create the HTML to display the weather
const weatherHTML = `
<h3>${cityName}, ${country}</h3>
<div class="weather-data">
<div class="weather-temp">${temperature}°C</div>
<div class="weather-details">
<p>${description}</p>
<p>Feels like: ${feelsLike}°C</p>
<p>Humidity: ${humidity}%</p>
<p>Wind: ${windSpeed} m/s</p>
</div>
</div>
`;
// Update the weather result div with the HTML
weatherResult.innerHTML = weatherHTML;
}
// Test API function
function testAPI() {
const testUrl = `https://api.openweathermap.org/data/2.5/weather?q=London&appid=${apiKey}&units=metric`;
console.log("Testing API with:", testUrl);
fetch(testUrl)
.then(response => {
console.log("Test response status:", response.status);
console.log("Test response ok:", response.ok);
return response.json();
})
.then(data => {
console.log("Test data received:", data);
})
.catch(error => {
console.error("Test error:", error);
});
}
// Call the test function when the page loads
testAPI();
}
// User Directory functionality using JSONPlaceholder API
function setupUserDirectory() {
const loadUsersBtn = document.getElementById('loadUsers');
const usersList = document.getElementById('usersList');
loadUsersBtn.addEventListener('click', function() {
loadUsers();
});
function loadUsers() {
// Show loading message
usersList.innerHTML = '<p>Loading users...</p>';
// Fetch users from JSONPlaceholder API
fetch('https://jsonplaceholder.typicode.com/users')
.then(function(response) {
console.log("Users API response status:", response.status);
if (!response.ok) {
throw new Error('Failed to load users');
}
return response.json();
})
.then(function(users) {
console.log("Received users data:", users);
displayUsers(users);
})
.catch(function(error) {
console.error("Error loading users:", error);
usersList.innerHTML = `<p class="weather-error">Error: ${error.message}</p>`;
});
}
function displayUsers(users) {
// Clear previous content
usersList.innerHTML = '';
// Create a card for each user
users.forEach(function(user) {
const userCard = document.createElement('div');
userCard.className = 'user-card';
// Create user information HTML
userCard.innerHTML = `
<h3>${user.name}</h3>
<div class="user-details">
<p><strong>Username:</strong> ${user.username}</p>
<p><strong>Email:</strong> ${user.email}</p>
<p><strong>Phone:</strong> ${user.phone}</p>
<p><strong>Website:</strong> ${user.website}</p>
<p><strong>Company:</strong> ${user.company.name}</p>
<p><strong>Address:</strong> ${user.address.city}</p>
</div>
<button class="show-posts-btn" data-user-id="${user.id}">Show Posts</button>
<div class="user-posts" id="posts-${user.id}"></div>
`;
usersList.appendChild(userCard);
});
// Add event listeners to "Show Posts" buttons
document.querySelectorAll('.show-posts-btn').forEach(function(button) {
button.addEventListener('click', function() {
const userId = this.getAttribute('data-user-id');
loadUserPosts(userId);
});
});
}
function loadUserPosts(userId) {
const postsContainer = document.getElementById(`posts-${userId}`);
// Show loading message
postsContainer.innerHTML = '<p>Loading posts...</p>';
// Fetch posts for the specified user
fetch(`https://jsonplaceholder.typicode.com/posts?userId=${userId}`)
.then(function(response) {
console.log("Posts API response status:", response.status);
if (!response.ok) {
throw new Error('Failed to load posts');
}
return response.json();
})
.then(function(posts) {
console.log(`Received posts for user ${userId}:`, posts);
displayUserPosts(postsContainer, posts);
})
.catch(function(error) {
console.error(`Error loading posts for user ${userId}:`, error);
postsContainer.innerHTML = `<p class="weather-error">Error: ${error.message}</p>`;
});
}
function displayUserPosts(container, posts) {
// Clear previous content
container.innerHTML = '';
// Show message if no posts
if (posts.length === 0) {
container.innerHTML = '<p>No posts found for this user.</p>';
return;
}
// Create a list of posts
posts.forEach(function(post) {
const postElement = document.createElement('div');
postElement.className = 'post-item';
postElement.innerHTML = `
<div class="post-title">${post.title}</div>
<div class="post-body">${post.body}</div>
`;
container.appendChild(postElement);
});
}
}
// Task Tracker functionality
function setupTaskTracker() {
const taskInput = document.getElementById('taskInput');
const addTaskBtn = document.getElementById('addTask');
const activeTaskList = document.getElementById('activeTaskList');
const archivedTaskList = document.getElementById('archivedTaskList');
const showActiveBtn = document.getElementById('showActive');
const showArchivedBtn = document.getElementById('showArchived');
const clearTasksBtn = document.getElementById('clearTasks');
// Track tasks in an array
let tasks = [];
// Timer for updating running tasks
let timerInterval = null;
// Function to start the timer interval
function startTimerInterval() {
// Clear any existing interval
if (timerInterval) {
clearInterval(timerInterval);
}
// Update every second
timerInterval = setInterval(() => {
// Update time for any running tasks
const now = Date.now();
let updated = false;
tasks.forEach(task => {
if (task.isRunning && task.startTime) {
const elapsed = Math.floor((now - task.startTime) / 1000);
task.timeSpent += elapsed;
task.startTime = now;
updated = true;
}
});
// If any tasks were updated, save and render
if (updated) {
saveTasks();
renderTasks();
}
}, 1000);
}
// Function to save tasks to Firestore
function saveTasks() {
const db = firebase.firestore();
db.collection("timedTasks").doc("user1").set({
tasks: tasks
})
.then(() => {
console.log("Timed tasks saved to Firestore");
})
.catch((error) => {
console.error("Error saving timed tasks: ", error);
// Fallback to localStorage
localStorage.setItem('timeTrackerTasks', JSON.stringify(tasks));
});
}
// Function to load tasks from Firestore
function loadTasks() {
const db = firebase.firestore();
db.collection("timedTasks").doc("user1").get()
.then((doc) => {
if (doc.exists && doc.data().tasks) {
tasks = doc.data().tasks;
// Make sure running tasks are properly restarted
const now = Date.now();
tasks.forEach(task => {
if (task.isRunning) {
task.startTime = now;
}
});
renderTasks();
} else {
// Fallback to localStorage
const savedTasks = localStorage.getItem('timeTrackerTasks');
if (savedTasks) {
tasks = JSON.parse(savedTasks);
// Make sure running tasks are properly restarted
const now = Date.now();
tasks.forEach(task => {
if (task.isRunning) {
task.startTime = now;
}
});
renderTasks();
}
}
})
.catch((error) => {
console.error("Error loading timed tasks: ", error);
// Fallback to localStorage
const savedTasks = localStorage.getItem('timeTrackerTasks');
if (savedTasks) {
tasks = JSON.parse(savedTasks);
// Make sure running tasks are properly restarted
const now = Date.now();
tasks.forEach(task => {
if (task.isRunning) {
task.startTime = now;
}
});
renderTasks();
}
});
}
// Function to format time as HH:MM:SS
function formatTime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
return [
hours.toString().padStart(2, '0'),
minutes.toString().padStart(2, '0'),
secs.toString().padStart(2, '0')
].join(':');
}
// Function to render tasks to the DOM
function renderTasks() {
// Clear the current lists
activeTaskList.innerHTML = '';
archivedTaskList.innerHTML = '';
// Add each task to the appropriate list
tasks.forEach(task => {
const container = task.archived ? archivedTaskList : activeTaskList;
// Create task item
const taskEl = document.createElement('div');
taskEl.className = 'task-item';
// Add checkbox for completion
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = task.completed;
checkbox.addEventListener('change', () => {
task.completed = checkbox.checked;
if (task.isRunning && task.completed) {
task.isRunning = false;
task.startTime = null;
}
saveTasks();
renderTasks();
});
// Create task text
const taskText = document.createElement('span');
taskText.className = 'task-text';
if (task.completed) taskText.classList.add('completed');
taskText.textContent = task.text;
// Create time display
const timeDisplay = document.createElement('span');
timeDisplay.className = 'task-time';
timeDisplay.textContent = formatTime(task.timeSpent);
// Create timer buttons container
const timerButtons = document.createElement('div');
timerButtons.className = 'timer-buttons';
if (!task.archived) {
// Start button (only show if not running)
if (!task.isRunning && !task.completed) {
const startBtn = document.createElement('button');
startBtn.className = 'timer-button start-button';
startBtn.textContent = 'Start';
startBtn.addEventListener('click', () => {
task.isRunning = true;
task.startTime = Date.now();
saveTasks();
renderTasks();
});
timerButtons.appendChild(startBtn);
}
// Pause button (only show if running)
if (task.isRunning) {
const pauseBtn = document.createElement('button');
pauseBtn.className = 'timer-button pause-button';
pauseBtn.textContent = 'Pause';
pauseBtn.addEventListener('click', () => {
task.isRunning = false;
task.startTime = null;
saveTasks();
renderTasks();
});
timerButtons.appendChild(pauseBtn);
}
// Archive button
const archiveBtn = document.createElement('button');
archiveBtn.className = 'timer-button stop-button';
archiveBtn.textContent = 'Archive';
archiveBtn.addEventListener('click', () => {
task.archived = true;
if (task.isRunning) {
task.isRunning = false;
task.startTime = null;
}
saveTasks();
renderTasks();
});
timerButtons.appendChild(archiveBtn);
} else {
// Restore button (for archived tasks)
const restoreBtn = document.createElement('button');
restoreBtn.className = 'timer-button restore-button';
restoreBtn.textContent = 'Restore';
restoreBtn.addEventListener('click', () => {
task.archived = false;
saveTasks();
renderTasks();
});
timerButtons.appendChild(restoreBtn);
}
// Delete button
const deleteBtn = document.createElement('button');
deleteBtn.className = 'timer-button stop-button';
deleteBtn.textContent = 'Delete';
deleteBtn.addEventListener('click', () => {
tasks = tasks.filter(t => t.id !== task.id);
saveTasks();
renderTasks();
});
timerButtons.appendChild(deleteBtn);
// Assemble task item
taskEl.appendChild(checkbox);
taskEl.appendChild(taskText);
taskEl.appendChild(timeDisplay);
taskEl.appendChild(timerButtons);
// Add to appropriate container
container.appendChild(taskEl);
});
}
// Add event listeners
addTaskBtn.addEventListener('click', addTask);
taskInput.addEventListener('keypress', e => {
if (e.key === 'Enter') addTask();
});
// Show active tasks
showActiveBtn.addEventListener('click', () => {
activeTaskList.classList.remove('hidden');
archivedTaskList.classList.add('hidden');
showActiveBtn.classList.add('active');
showArchivedBtn.classList.remove('active');
});
// Show archived tasks
showArchivedBtn.addEventListener('click', () => {
activeTaskList.classList.add('hidden');
archivedTaskList.classList.remove('hidden');
showActiveBtn.classList.remove('active');
showArchivedBtn.classList.add('active');
});
// Clear all tasks
clearTasksBtn.addEventListener('click', () => {
if (confirm('Are you sure you want to delete all tasks?')) {
tasks = [];
saveTasks();
renderTasks();
}
});
// Function to add a new task
function addTask() {
const text = taskInput.value.trim();
if (text === '') return;
// Create a new task
const task = {
id: Date.now(),
text: text,
completed: false,
timeSpent: 0,
isRunning: false,
startTime: null,
archived: false
};
// Add to array
tasks.push(task);
// Save to Firestore
saveTasks();
// Render tasks
renderTasks();
// Clear input
taskInput.value = '';
taskInput.focus();
}
// Load saved tasks when the page loads
loadTasks();
// Start the timer update interval
startTimerInterval();
// Clean up interval when page unloads
window.addEventListener('beforeunload', () => {
if (timerInterval) {
clearInterval(timerInterval);
}
});
}
// Main initialization - Single event listener for all features
document.addEventListener('DOMContentLoaded', function() {
console.log("Website loaded!");
// Set header color
const header = document.querySelector('h1');
if (header) {
header.style.color = '#e74c3c';
}
// Initialize all components
// You can comment out any component you don't need
setupCounter();
// Detect which list feature to use - only enable one
const taskTrackerElements = document.getElementById('activeTaskList');
const todoListElements = document.getElementById('taskList');
if (taskTrackerElements) {
// Initialize task tracker if those elements exist
setupTaskTracker();
} else if (todoListElements) {
// Otherwise initialize todo list if those elements exist
setupTodoList();
}
// Initialize other components
setupWeather();
setupUserDirectory();
});