-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
666 lines (573 loc) · 19.6 KB
/
script.js
File metadata and controls
666 lines (573 loc) · 19.6 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
// ====================
// SecurePass Vault - Main JavaScript
// ====================
'use strict';
// ====================
// Theme Toggle Functionality
// ====================
const themeToggle = document.getElementById('theme-toggle');
const body = document.body;
// Check for saved theme preference or default to light mode
const currentTheme = localStorage.getItem('theme') || 'light';
if (currentTheme === 'dark') {
body.classList.add('dark-theme');
themeToggle.textContent = '☀️';
}
// Toggle theme on button click
themeToggle.addEventListener('click', () => {
body.classList.toggle('dark-theme');
// Update button icon and save preference
if (body.classList.contains('dark-theme')) {
themeToggle.textContent = '☀️';
localStorage.setItem('theme', 'dark');
} else {
themeToggle.textContent = '🌙';
localStorage.setItem('theme', 'light');
}
});
// ====================
// Mobile Navigation
// ====================
const hamburger = document.querySelector('.hamburger');
const navMenu = document.querySelector('.nav-menu');
// Toggle mobile menu
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
navMenu.classList.toggle('active');
});
// Close mobile menu when a link is clicked
document.querySelectorAll('.nav-menu a').forEach(link => {
link.addEventListener('click', () => {
hamburger.classList.remove('active');
navMenu.classList.remove('active');
});
});
// Close mobile menu when clicking outside
document.addEventListener('click', (e) => {
if (!hamburger.contains(e.target) && !navMenu.contains(e.target)) {
hamburger.classList.remove('active');
navMenu.classList.remove('active');
}
});
// ====================
// Smooth Scrolling for Navigation Links
// ====================
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
const headerOffset = 80; // Height of fixed header
const elementPosition = target.getBoundingClientRect().top;
const offsetPosition = elementPosition + window.pageYOffset - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
}
});
});
// ====================
// Scroll to Top Button
// ====================
const scrollTopBtn = document.createElement('button');
scrollTopBtn.className = 'scroll-top';
scrollTopBtn.innerHTML = '↑';
scrollTopBtn.setAttribute('aria-label', 'Scroll to top');
document.body.appendChild(scrollTopBtn);
// Show/hide scroll to top button based on scroll position
window.addEventListener('scroll', () => {
if (window.pageYOffset > 300) {
scrollTopBtn.classList.add('visible');
} else {
scrollTopBtn.classList.remove('visible');
}
});
// Scroll to top when button is clicked
scrollTopBtn.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
// ====================
// Header Shadow on Scroll
// ====================
window.addEventListener('scroll', () => {
const header = document.querySelector('header');
if (window.scrollY > 100) {
header.style.boxShadow = '0 4px 20px rgba(0,0,0,0.15)';
} else {
header.style.boxShadow = '0 2px 10px rgba(0,0,0,0.1)';
}
});
// ====================
// Active Navigation Link Highlighting
// ====================
window.addEventListener('scroll', () => {
let current = '';
const sections = document.querySelectorAll('section');
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (pageYOffset >= (sectionTop - 200)) {
current = section.getAttribute('id');
}
});
// Remove active class from all links
document.querySelectorAll('.nav-menu a').forEach(link => {
link.classList.remove('active');
// Add active class to current section link
if (link.getAttribute('href').slice(1) === current) {
link.classList.add('active');
}
});
});
// ====================
// Scroll Animation Observer
// ====================
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// Add fade-in animation
entry.target.style.opacity = '0';
entry.target.style.transform = 'translateY(20px)';
setTimeout(() => {
entry.target.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}, 100);
// Stop observing after animation
observer.unobserve(entry.target);
}
});
}, observerOptions);
// Observe elements for scroll animation
document.querySelectorAll('.feature-card, .step, .security-item, .reason, .testimonial').forEach(el => {
observer.observe(el);
});
// ====================
// Copy to Clipboard Function
// ====================
function copyToClipboard(text) {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(() => {
showNotification('Copied to clipboard!', 'success');
}).catch(err => {
console.error('Failed to copy:', err);
showNotification('Failed to copy', 'error');
});
} else {
// Fallback for older browsers
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
showNotification('Copied to clipboard!', 'success');
} catch (err) {
console.error('Failed to copy:', err);
showNotification('Failed to copy', 'error');
}
document.body.removeChild(textArea);
}
}
// ====================
// Notification System
// ====================
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `notification notification-${type}`;
notification.textContent = message;
// Styling
notification.style.cssText = `
position: fixed;
top: 100px;
right: 20px;
background: ${type === 'success' ? '#10b981' : type === 'error' ? '#ef4444' : '#3b82f6'};
color: white;
padding: 1rem 1.5rem;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
z-index: 10000;
animation: slideIn 0.3s ease;
min-width: 250px;
font-weight: 500;
`;
document.body.appendChild(notification);
// Remove after 3 seconds
setTimeout(() => {
notification.style.animation = 'fadeOut 0.3s ease';
setTimeout(() => notification.remove(), 300);
}, 3000);
}
// ====================
// Form Validation (if forms are added)
// ====================
function validateEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function handleFormSubmit(e) {
e.preventDefault();
const form = e.target;
const email = form.querySelector('input[type="email"]');
if (email && !validateEmail(email.value)) {
showNotification('Please enter a valid email address', 'error');
email.focus();
return;
}
// Show success message
showNotification('Thank you for your submission!', 'success');
form.reset();
}
// Add form submit listeners if forms exist
document.querySelectorAll('form').forEach(form => {
form.addEventListener('submit', handleFormSubmit);
});
// ====================
// Lazy Loading Images
// ====================
if ('IntersectionObserver' in window) {
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
if (img.dataset.src) {
img.src = img.dataset.src;
img.removeAttribute('data-src');
}
if (img.dataset.srcset) {
img.srcset = img.dataset.srcset;
img.removeAttribute('data-srcset');
}
observer.unobserve(img);
}
});
});
// Observe all images with data-src attribute
document.querySelectorAll('img[data-src]').forEach(img => {
imageObserver.observe(img);
});
}
// ====================
// Detect Browser & Device
// ====================
function detectBrowser() {
const userAgent = navigator.userAgent;
let browser = 'Unknown';
if (userAgent.indexOf('Firefox') > -1) {
browser = 'Firefox';
} else if (userAgent.indexOf('Chrome') > -1) {
browser = 'Chrome';
} else if (userAgent.indexOf('Safari') > -1) {
browser = 'Safari';
} else if (userAgent.indexOf('Edge') > -1) {
browser = 'Edge';
} else if (userAgent.indexOf('Opera') > -1 || userAgent.indexOf('OPR') > -1) {
browser = 'Opera';
}
return browser;
}
function isMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}
function isTablet() {
return /(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(navigator.userAgent);
}
// Log device info (for debugging)
console.log('Browser:', detectBrowser());
console.log('Mobile:', isMobile());
console.log('Tablet:', isTablet());
// ====================
// Performance Monitoring
// ====================
window.addEventListener('load', () => {
// Log page load time
if (window.performance && window.performance.timing) {
const loadTime = window.performance.timing.loadEventEnd - window.performance.timing.navigationStart;
console.log(`Page loaded in ${loadTime}ms`);
}
// Log DOM content loaded time
if (window.performance && window.performance.timing) {
const domTime = window.performance.timing.domContentLoadedEventEnd - window.performance.timing.navigationStart;
console.log(`DOM ready in ${domTime}ms`);
}
});
// ====================
// Keyboard Navigation Shortcuts
// ====================
document.addEventListener('keydown', (e) => {
// ESC key - Close mobile menu
if (e.key === 'Escape') {
hamburger.classList.remove('active');
navMenu.classList.remove('active');
}
// Ctrl/Cmd + K - Quick search (if search is implemented)
if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
e.preventDefault();
// Implement search functionality here
console.log('Search shortcut triggered');
}
// Arrow Up - Scroll to top
if (e.key === 'Home') {
e.preventDefault();
window.scrollTo({ top: 0, behavior: 'smooth' });
}
// Arrow Down - Scroll to bottom
if (e.key === 'End') {
e.preventDefault();
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
}
});
// ====================
// Auto-update Copyright Year
// ====================
document.addEventListener('DOMContentLoaded', () => {
const yearElement = document.querySelector('.footer-bottom p');
if (yearElement) {
const currentYear = new Date().getFullYear();
yearElement.innerHTML = yearElement.innerHTML.replace(/\d{4}/, currentYear);
}
});
// ====================
// Print Page Function
// ====================
function printPage() {
window.print();
}
// ====================
// Share Page Function
// ====================
async function sharePage() {
const shareData = {
title: document.title,
text: 'Check out SecurePass Vault - A secure password manager',
url: window.location.href
};
// Check if Web Share API is supported
if (navigator.share) {
try {
await navigator.share(shareData);
console.log('Page shared successfully');
} catch (err) {
if (err.name !== 'AbortError') {
console.log('Error sharing:', err);
copyToClipboard(window.location.href);
}
}
} else {
// Fallback - Copy URL to clipboard
copyToClipboard(window.location.href);
}
}
// ====================
// Stats Counter Animation
// ====================
function animateCounter(element, target, duration = 2000) {
const start = 0;
const increment = target / (duration / 16); // 60 FPS
let current = start;
const timer = setInterval(() => {
current += increment;
if (current >= target) {
element.textContent = target.toLocaleString();
clearInterval(timer);
} else {
element.textContent = Math.floor(current).toLocaleString();
}
}, 16);
}
// Animate stat numbers when they come into view
const statsObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const statNumber = entry.target.querySelector('.stat-number');
if (statNumber && !statNumber.dataset.animated) {
const targetValue = parseInt(statNumber.textContent.replace(/,/g, ''));
if (!isNaN(targetValue)) {
animateCounter(statNumber, targetValue);
statNumber.dataset.animated = 'true';
}
}
}
});
}, { threshold: 0.5 });
// Observe stat items
document.querySelectorAll('.stat-item').forEach(item => {
statsObserver.observe(item);
});
// ====================
// Prevent Animation on Page Load
// ====================
window.addEventListener('load', () => {
document.body.classList.add('loaded');
});
// ====================
// External Links - Open in New Tab
// ====================
document.querySelectorAll('a[href^="http"]').forEach(link => {
if (link.hostname !== window.location.hostname) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
}
});
// ====================
// Prevent Empty Anchor Links
// ====================
document.querySelectorAll('a[href="#"]').forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
});
});
// ====================
// Service Worker Registration (for PWA support)
// ====================
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
// Uncomment when you create a service worker file
// navigator.serviceWorker.register('/service-worker.js')
// .then(registration => {
// console.log('ServiceWorker registered:', registration);
// })
// .catch(error => {
// console.log('ServiceWorker registration failed:', error);
// });
});
}
// ====================
// Online/Offline Detection
// ====================
window.addEventListener('online', () => {
showNotification('You are back online!', 'success');
});
window.addEventListener('offline', () => {
showNotification('You are offline', 'error');
});
// ====================
// Console Welcome Message
// ====================
console.log('%c🔐 SecurePass Vault', 'color: #667eea; font-size: 24px; font-weight: bold;');
console.log('%cOpen-source password manager with zero-knowledge encryption', 'color: #764ba2; font-size: 14px;');
console.log('%cBuilt with ❤️ and security in mind', 'color: #666; font-size: 12px;');
console.log('%c\nInterested in the code? Check out our GitHub repository!', 'color: #10b981; font-size: 12px;');
// ====================
// Debug Mode (remove in production)
// ====================
const DEBUG_MODE = false; // Set to false for production
if (DEBUG_MODE) {
console.log('Debug mode enabled');
// Log all clicks
document.addEventListener('click', (e) => {
console.log('Clicked:', e.target);
});
// Log all scroll events
let scrollTimeout;
window.addEventListener('scroll', () => {
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(() => {
console.log('Scroll position:', window.pageYOffset);
}, 100);
});
}
// ====================
// Feature Detection
// ====================
const features = {
localStorage: typeof(Storage) !== 'undefined',
serviceWorker: 'serviceWorker' in navigator,
webShare: 'share' in navigator,
intersectionObserver: 'IntersectionObserver' in window,
clipboard: navigator.clipboard !== undefined
};
if (DEBUG_MODE) {
console.log('Browser features:', features);
}
// ====================
// Error Handling
// ====================
window.addEventListener('error', (e) => {
console.error('Global error:', e.error);
// In production, you might want to send errors to a logging service
// sendErrorToLoggingService(e.error);
});
window.addEventListener('unhandledrejection', (e) => {
console.error('Unhandled promise rejection:', e.reason);
// In production, you might want to send errors to a logging service
// sendErrorToLoggingService(e.reason);
});
// ====================
// Page Visibility API - Pause animations when tab is hidden
// ====================
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
console.log('Tab is hidden');
// Pause heavy animations or operations
} else {
console.log('Tab is visible');
// Resume animations or operations
}
});
// ====================
// Utility Functions
// ====================
// Debounce function for performance optimization
function debounce(func, wait = 300) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Throttle function for performance optimization
function throttle(func, limit = 300) {
let inThrottle;
return function(...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
// Example usage of debounce on window resize
const handleResize = debounce(() => {
console.log('Window resized:', window.innerWidth, 'x', window.innerHeight);
}, 300);
window.addEventListener('resize', handleResize);
// ====================
// Initialize Everything
// ====================
document.addEventListener('DOMContentLoaded', () => {
console.log('DOM fully loaded and parsed');
// Add any initialization code here
// Check if user prefers reduced motion
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
console.log('User prefers reduced motion');
// Disable or reduce animations
}
// Log initial theme
console.log('Current theme:', localStorage.getItem('theme') || 'light');
});
// ====================
// Export functions (if using modules)
// ====================
// Uncomment if using ES6 modules
// export {
// copyToClipboard,
// showNotification,
// validateEmail,
// sharePage,
// printPage
// };