-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
183 lines (145 loc) · 6.21 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
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
// DOM Elements
const inputSlider = document.getElementById("inputSlider");
const sliderValue = document.getElementById("sliderValue");
const passBox = document.getElementById("passBox");
const lowercaseEl = document.getElementById("lowercase");
const uppercaseEl = document.getElementById("uppercase");
const numbersEl = document.getElementById("numbers");
const symbolsEl = document.getElementById("symbols");
// New element for favorite words
const favoriteWordsInput = document.getElementById("favoriteWords");
const generateBtn = document.getElementById("genBtn");
const copyBtn = document.getElementById("copyIcon");
const passIndicator = document.getElementById("passIndicator");
const clearBtn = document.getElementById("delete")
// Character Sets
const lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
const uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numbers = "0123456789";
const symbols = "!@#$%^&*()_+-=[]{}\\|;':\",./<>?";
// Initialize Slider Value
sliderValue.textContent = inputSlider.value;
// Event Listeners
inputSlider.addEventListener("input", () => {
sliderValue.textContent = inputSlider.value;
});
generateBtn.addEventListener("click", generatePassword);
copyBtn.addEventListener("click", copyToClipboard);
clearBtn.addEventListener("click" , clear)
// Functions
function generatePassword() {
const length = inputSlider.value;
let characters = "";
let passwordArray = [];
// Include favorite words
const favoriteWordsInputValue = favoriteWordsInput.value;
const favoriteWords = favoriteWordsInputValue.split(',').map(word => word.trim()).filter(word => word);
// Process favorite words based on checkbox selections
if (favoriteWords.length > 0) {
if (lowercaseEl.checked) {
passwordArray.push(...favoriteWords.map(word => word.toLowerCase())); // Convert to lowercase
}
if (uppercaseEl.checked) {
passwordArray.push(...favoriteWords.map(word => word.toUpperCase())); // Convert to uppercase
}
if (!lowercaseEl.checked && !uppercaseEl.checked) {
passwordArray.push(...favoriteWords); // Keep original case if neither is checked
}
}
// Add character sets based on checkboxes
if (lowercaseEl.checked) characters += lowercaseLetters;
if (uppercaseEl.checked) characters += uppercaseLetters;
if (numbersEl.checked) characters += numbers;
if (symbolsEl.checked) characters += symbols;
// Ensure there's at least one character type selected
if (characters.length === 0 && passwordArray.length === 0) {
alert("Please select at least one character type or enter favorite words.");
return;
}
// Generate password from shuffled favorite words and other characters
while (passwordArray.length < length) {
passwordArray.push(characters.charAt(Math.floor(Math.random() * characters.length)));
}
// Shuffle the final password array for randomness
shuffleArray(passwordArray);
passBox.value = passwordArray.slice(0, length).join(''); // Limit to the desired length
updatePasswordIndicator();
}
function updatePasswordIndicator() {
const passwordStrength = getPasswordStrength(passBox.value);
passIndicator.className = "pass-indicator " + passwordStrength;
}
function getPasswordStrength(password) {
if (password.length <= 10) return "weak";
if (password.length <= 20) return "medium";
return "strong";
}
// Clipboard Functionality
function copyToClipboard() {
if (passBox.value !== "") {
navigator.clipboard.writeText(passBox.value);
copyBtn.innerText = "check";
setTimeout(() => {
copyBtn.innerHTML = "content_copy";
}, 3000);
}
}
// clear the favorite words
function clear(){
favoriteWords.value = "";
}
// Shuffle Array Function
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
}
}
// Confetti Creation Functionality
function createConfetti() {
const confetti = document.createElement('div');
// Get all elements on the page to avoid collisions
const elementsToAvoid = [passBox, generateBtn, copyBtn, ...document.querySelectorAll('.container')];
// Randomly position confetti anywhere on the screen but outside of the specified elements
let randomXPosition, randomYPosition;
do {
randomXPosition = Math.random() * window.innerWidth; // Random X position within viewport width
randomYPosition = Math.random() * window.innerHeight; // Random Y position within viewport height
} while (isColliding(randomXPosition, randomYPosition, elementsToAvoid));
confetti.style.left = `${randomXPosition}px`;
confetti.style.top = `${randomYPosition}px`;
// Set random colors
const colors = ['#FF5733', '#33FF57', '#3357FF', '#F1C40F', '#9B59B6'];
confetti.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
// Set random size
const size = Math.random() * 7 + 5; // Size between 5px and 15px
confetti.style.width = `${size}px`;
confetti.style.height = `${size}px`;
// Add a burst animation class
confetti.classList.add('confetti');
// Append to body and animate falling down
document.body.appendChild(confetti);
// Animate falling down and fading out
confetti.style.animation = `burst ${Math.random() * (3 - 1) + 1}s linear forwards`;
// Remove the shape after animation ends and create another one
confetti.addEventListener('animationend', () => {
confetti.remove();
createConfetti(); // Create another shape after one is removed
});
}
// Function to check for collisions with specified elements
function isColliding(x, y, elements) {
return elements.some(el => {
const rect = el.getBoundingClientRect();
return (
x >= rect.left && x <= rect.right &&
y >= rect.top && y <= rect.bottom
);
});
}
// Start creating confetti on load
window.onload = () => {
for (let i = 0; i < 150; i++) createConfetti(); // Adjust number of confetti pieces as needed
};
// Update password indicator on page load
window.addEventListener('DOMContentLoaded', updatePasswordIndicator);