-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (54 loc) · 2.76 KB
/
index.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
const 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","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?",
"/"];
//1. Declare 2 variables to get the fields where the password will be stored
let pwOne = document.getElementById("pw-One")
let pwTwo = document.getElementById("pw-Two")
//2. Declare a variable getting the generate password button
let generatePwBtn = document.getElementById("generate-btn")
//3. Generate new passwords according to the set password length and whether there should be numbers and symbols or just letters.
generatePwBtn.addEventListener("click", function(){
if (document.getElementById("checkbox").checked === true){
pwOne.textContent = ""
for (let i = 0; i < document.getElementById("length").value; i++){
let randomIndexOne = Math.floor( Math.random() * characters.length )
let randomChar = characters[randomIndexOne]
pwOne.textContent += randomChar
}
pwTwo.textContent = ""
for (let i = 0; i < document.getElementById("length").value; i++){
let randomIndexTwo = Math.floor( Math.random() * characters.length )
let randomChar = characters[randomIndexTwo]
pwTwo.textContent += randomChar
}
} else {
pwOne.textContent = ""
for (let i = 0; i < document.getElementById("length").value; i++){
let randomIndexOne = Math.floor( Math.random() * 52 )
let randomChar = characters[randomIndexOne]
pwOne.textContent += randomChar
}
pwTwo.textContent = ""
for (let i = 0; i < document.getElementById("length").value; i++){
let randomIndexTwo = Math.floor( Math.random() * 52 )
let randomChar = characters[randomIndexTwo]
pwTwo.textContent += randomChar
}
}
})
//4. Copy password to clipboard when the copy button is clicked and get an alert.
function copyPWOne(){
const range = document.createRange()
range.selectNode(document.getElementById("pw-One"))
window.getSelection().removeAllRanges()
window.getSelection().addRange(range)
document.execCommand("copy")
window.getSelection().removeAllRanges()
}
function copyPWTwo(){
const range = document.createRange()
range.selectNode(document.getElementById("pw-Two"))
window.getSelection().removeAllRanges()
window.getSelection().addRange(range)
document.execCommand("copy")
window.getSelection().removeAllRanges()
}