-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (63 loc) · 1.93 KB
/
Copy pathscript.js
File metadata and controls
80 lines (63 loc) · 1.93 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
const pwEl = document.getElementById('pw');
const copyEl = document.getElementById('copy');
const lengthEl = document.getElementById('length');
const upperEl = document.getElementById('upper');
const lowerEl = document.getElementById('lower');
const numberEl = document.getElementById('numbers');
const symbolEl = document.getElementById('symbol');
const gBtnEl = document.getElementById('gBtn');
const upperLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const lowerLetters = 'abcdefghijklmnopqrstuvwxyz';
const numbers = '0123456789';
const symbols = '!@#$%^&*()_+=';
// Get random objects from letters, numbers, and symbols
function getLowercase(){
return lowerLetters[Math.floor(Math.random() * lowerLetters.length)]
}
function getUppercase(){
return upperLetters[Math.floor(Math.random() * upperLetters.length)]
}
function getNumbers(){
return numbers[Math.floor(Math.random() * numbers.length)]
}
function getSymbol(){
return symbols[Math.floor(Math.random() * symbols.length)]
}
//
function generatePassword(){
const len = lengthEl.value;
let password = '';
for(let i = 0; i < len; i++){
const x = genarateX();
password += x;
}
pwEl.innerText = password;
}
function genarateX(){
const xs = [];
if(upperEl.checked){
xs.push(getUppercase());
}
if(lowerEl.checked){
xs.push(getLowercase());
}
if(numberEl.checked){
xs.push(getNumbers());
}
if(symbolEl.checked){
xs.push(getSymbol());
}
if(xs.length == 0) return ''
return xs[Math.floor(Math.random() * xs.length)]
}
gBtnEl.addEventListener('click',generatePassword);
copyEl.addEventListener('click',()=>{
const textarea = document.createElement('textarea');
const password = pwEl.innerText;
if(!password) return;
textarea.value = password;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
})