-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (55 loc) · 1.92 KB
/
Copy pathscript.js
File metadata and controls
66 lines (55 loc) · 1.92 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
function generatePassword() {
const length = parseInt(document.getElementById('length').value);
const includeUppercase = document.getElementById('includeUppercase').checked;
const includeLowercase = document.getElementById('includeLowercase').checked;
const includeNumbers = document.getElementById('includeNumbers').checked;
const includeSymbols = document.getElementById('includeSymbols').checked;
if (isNaN(length) || length <= 0) {
showToast("Please enter a valid password length greater than 0.");
return;
}
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lower = "abcdefghijklmnopqrstuvwxyz";
const numbers = "0123456789";
const symbols = "!@#$%^&*()_+-=[]{}|;:',.<>?/";
let allChars = "";
if (includeUppercase) allChars += upper;
if (includeLowercase) allChars += lower;
if (includeNumbers) allChars += numbers;
if (includeSymbols) allChars += symbols;
if (allChars === "") {
showToast("Please select at least one character type.");
return;
}
let password = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * allChars.length);
password += allChars[randomIndex];
}
const resultBox = document.getElementById('result');
resultBox.value = password;
}
function copyPassword() {
const resultBox = document.getElementById('result');
if (!resultBox.value) {
showToast("Nothing to copy!");
return;
}
resultBox.select();
resultBox.setSelectionRange(0, 99999);
navigator.clipboard.writeText(resultBox.value)
.then(() => {
showToast("Password copied to clipboard!");
})
.catch(() => {
showToast("Failed to copy password.");
});
}
function showToast(message) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.classList.add("show");
setTimeout(() => {
toast.classList.remove("show");
}, 2000);
}