-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (59 loc) · 2.66 KB
/
index.js
File metadata and controls
69 lines (59 loc) · 2.66 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
let characters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?",
"/"];
let originalCharacters = [...characters]
const genBtn = document.getElementById("gen-btn")
let pwdElOne = document.getElementById("pwd-el-one")
let pwdElTwo = document.getElementById("pwd-el-two")
const pwdLength = document.querySelector(".settings")
const checkboxes = document.querySelectorAll(".switch input")
const includeNumbers = document.querySelector("#include-numbers")
const includeSymbols = document.querySelector("#include-symbols")
// Listens for tweaks made by the user to how the password is generated
checkboxes.forEach(checkbox => {
checkbox.addEventListener("change", function() {
console.log(checkbox.id + " " + checkbox.checked)
tweak()
})
});
// Tweaks the characters array to user preference
function tweak() {
// Always reset to the full original set first, and only filter if necessary
characters = [...originalCharacters];
if (!includeNumbers.checked) {
characters = characters.filter(char => !/[0-9]/.test(char));
}
if (!includeSymbols.checked) {
characters = characters.filter(char => !/[~`!@#$%^&*()_\-+=\[\]{}|:;<>?,./]/.test(char));
}
// After tweaking, regenerate the password
generateRandomPassword();
}
// Sets the password length and listens to changes by user
let num = Number(pwdLength.min)
pwdLength.value = num
pwdLength.addEventListener("input", () =>{
num = Number(pwdLength.value)
num = Math.max(8, Math.min(15, parseInt(pwdLength.value, 10)));
} )
// Generates a random password by drawing from characters list and stores value
function generateRandomPassword() {
let randomPassword = ""
for (let i = 0; i < num; i++) {
randomPassword += characters[Math.floor(Math.random() * characters.length)]
}
return randomPassword
}
// Runs the above function twice upon clicking and displays them on screen
genBtn.addEventListener("click", function() {
pwdElOne.textContent = generateRandomPassword()
pwdElTwo.textContent = generateRandomPassword()
})
// Allows user to copy generated password to clipboard
pwdElOne.addEventListener("click", () => {
navigator.clipboard.writeText(pwdElOne.textContent)
pwdElOne.textContent = "Copied!"
})
pwdElTwo.addEventListener("click", () => {
navigator.clipboard.writeText(pwdElTwo.textContent)
pwdElTwo.textContent = "Copied!"
})