-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (77 loc) · 3.08 KB
/
Copy pathscript.js
File metadata and controls
96 lines (77 loc) · 3.08 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
const displayLength = document.getElementById("displayLength");
const lengthInput = document.getElementById("length");
const passwordOutput = document.getElementById("displayPassword");
const strengthBar = document.getElementById("strength");
const symbolsCheckbox = document.getElementById("symbols");
const uppersCheckbox = document.getElementById("uppers");
const lowersCheckbox = document.getElementById("lowers");
const numbersCheckbox = document.getElementById("numbers");
// Update displayed length value
lengthInput.addEventListener('input', () => {
displayLength.textContent = lengthInput.value;
updateStrengthBar();
});
// Update strength bar on checkbox changes
[symbolsCheckbox, uppersCheckbox, lowersCheckbox, numbersCheckbox].forEach(checkbox => {
checkbox.addEventListener('change', updateStrengthBar);
});
// Function to calculate and update the strength bar
function updateStrengthBar() {
const length = parseInt(lengthInput.value, 10);
const hasSymbols = symbolsCheckbox.checked;
const hasUppers = uppersCheckbox.checked;
const hasLowers = lowersCheckbox.checked;
const hasNumbers = numbersCheckbox.checked;
let strength = 0;
// Add points for length
if (length >= 8) strength += length / 10;
// Add points for character variety
if (hasSymbols) strength += 1;
if (hasUppers) strength += 1;
if (hasLowers) strength += 1;
if (hasNumbers) strength += 1;
// Cap strength to a maximum of 5
strength = Math.min(strength, 5);
// Update strength bar width and color
strengthBar.style.width = `${(strength / 5) * 100}%`;
strengthBar.style.backgroundColor = getStrengthColor(strength);
}
// Function to get color based on strength
function getStrengthColor(strength) {
if (strength < 2) return "#ff4d4d"; // Weak (Red)
if (strength < 4) return "#ffcc00"; // Medium (Yellow)
return "#4caf50"; // Strong (Green)
}
// Generate a new password
function generateNewPassword() {
const length = parseInt(lengthInput.value, 10);
const hasSymbols = symbolsCheckbox.checked;
const hasUppers = uppersCheckbox.checked;
const hasLowers = lowersCheckbox.checked;
const hasNumbers = numbersCheckbox.checked;
let charPool = "";
if (hasSymbols) charPool += "!@#$%^&*(){}[]=<>/,.";
if (hasUppers) charPool += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (hasLowers) charPool += "abcdefghijklmnopqrstuvwxyz";
if (hasNumbers) charPool += "0123456789";
if (!charPool) {
passwordOutput.value = "Please select at least one option.";
return;
}
let password = "";
for (let i = 0; i < length; i++) {
password += charPool.charAt(Math.floor(Math.random() * charPool.length));
}
passwordOutput.value = password;
updateStrengthBar();
}
// Copy password to clipboard
function copyPassword() {
passwordOutput.select();
document.execCommand("copy");
alert("Password copied to clipboard!");
}
// Initialize the year in the footer
document.getElementById("current-year").textContent = new Date().getFullYear();
// Initial update of the strength bar
updateStrengthBar();