-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
103 lines (87 loc) · 3.42 KB
/
scripts.js
File metadata and controls
103 lines (87 loc) · 3.42 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
const inputEl = document.querySelector("#password")
const upperCaseCheckEl = document.querySelector("#uppercase-check")
const numberCheckEl = document.querySelector("#number-check")
const symbolCheckEl = document.querySelector("#symbol-check")
const securityIndicatorBarEl = document.querySelector("#security-indicator-bar")
let passwordLength = 16
function generatePassword() {
let chars = "abcdefghjkmnpqrstuvwxyz"
const upperCaseChars = "ABCDEFGHJKLMNPQRSTUVWXYZ"
const numberChars = "123456789"
const symbolChars = "?!@&*()[]"
if (upperCaseCheckEl.checked) {
chars += upperCaseChars
}
if (numberCheckEl.checked) {
chars += numberChars
}
if (symbolCheckEl.checked) {
chars += symbolChars
}
let password = ""
for (let i = 0; i < passwordLength; i++) {
const randomNumber = Math.floor(Math.random() * chars.length)
password += chars.substring(randomNumber, randomNumber + 1)
}
inputEl.value = password
calculateQuality()
calculateFontSize()
}
function calculateQuality(){
const percent = Math.round((passwordLength / 64) * 25 + (upperCaseCheckEl.checked ? 15 : 0) + (numberCheckEl.checked ? 25 : 0) + (symbolCheckEl.checked ? 35 : 0))
console.log(percent)
securityIndicatorBarEl.style.width = `${percent}%`
if(percent > 69){
securityIndicatorBarEl.classList.remove('critical')
securityIndicatorBarEl.classList.remove('warning')
securityIndicatorBarEl.classList.add('safe')
}else if(percent > 50){
securityIndicatorBarEl.classList.remove('critical')
securityIndicatorBarEl.classList.add('warning')
securityIndicatorBarEl.classList.remove('safe')
}else{
securityIndicatorBarEl.classList.add('critical')
securityIndicatorBarEl.classList.remove('warning')
securityIndicatorBarEl.classList.remove('safe')
}
if(percent >= 100){
securityIndicatorBarEl.classList.add('completed')
}else{
securityIndicatorBarEl.classList.remove('completed')
}
}
function calculateFontSize(){
if(passwordLength > 45){
inputEl.classList.remove('font-sm')
inputEl.classList.remove('font-xs')
inputEl.classList.add('font-xxs')
}else if(passwordLength > 32){
inputEl.classList.remove('font-sm')
inputEl.classList.add('font-xs')
inputEl.classList.remove('font-xxs')
}else if(passwordLength > 22){
inputEl.classList.add('font-sm')
inputEl.classList.remove('font-xs')
inputEl.classList.remove('font-xxs')
}else{
inputEl.classList.remove('font-sm')
inputEl.classList.remove('font-xs')
inputEl.classList.remove('font-xxs')
}
}
function copy() {
navigator.clipboard.writeText(inputEl.value)
}
const passwordLengthEl = document.querySelector("#password-length")
passwordLengthEl.addEventListener("input", function () {
passwordLength = passwordLengthEl.value
document.querySelector("#password-length-text").innerText = passwordLength;
generatePassword()
})
upperCaseCheckEl.addEventListener("click", generatePassword)
numberCheckEl.addEventListener("click", generatePassword)
symbolCheckEl.addEventListener("click", generatePassword)
document.querySelector("#copy-1").addEventListener("click", copy)
document.querySelector("#copy-2").addEventListener("click", copy)
document.querySelector("#renew").addEventListener("click", generatePassword)
generatePassword()