-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (61 loc) · 1.83 KB
/
script.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
62
63
64
65
66
67
68
69
70
71
72
const pwEl = document.getElementById('pw');
const lengthEl = document.getElementById('length');
const lowerEl = document.getElementById('lower');
const upperEl = document.getElementById('upper');
const numberEl = document.getElementById('number');
const symbolEl = document.getElementById('symbol');
const generateEl = document.getElementById('generate');
const copyEl = document.getElementById('copy');
const lowers = 'abcdefghijklmnopqrstuvwxyz';
const uppers = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numbers = '0123456789';
const symbols = '!@#$%^&*()_+-=';
function getLower() {
return lowers[Math.floor(Math.random() * lowers.length)];
}
function getUpper() {
return uppers[Math.floor(Math.random() * uppers.length)];
}
function getNumber() {
return numbers[Math.floor(Math.random() * numbers.length)];
}
function getSymbol() {
return symbols[Math.floor(Math.random() * symbols.length)];
}
generateEl.addEventListener('click', () => {
len = lengthEl.value;
password = '';
for (let i = 0; i < len; i++) {
password += generateX();
}
pwEl.innerText = password;
});
function generateX() {
xs = [];
if (lowerEl.checked == true) {
xs.push(getLower());
}
if (upperEl.checked == true) {
xs.push(getUpper());
}
if (numberEl.checked == true) {
xs.push(getNumber());
}
if (symbolEl.checked == true) {
xs.push(getSymbol());
}
if (xs.length === 0) return '';
x = xs[Math.floor(Math.random() * xs.length)];
return x;
}
copyEl.addEventListener('click', () => {
let textarea = document.createElement('textarea');
textarea.innerText = pwEl.innerText;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
textarea.remove();
if (pwEl.innerText !== '') {
alert('Copied to clipboard');
}
});