-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
116 lines (85 loc) · 2.75 KB
/
popup.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
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
104
105
106
107
108
109
110
111
112
document.addEventListener('DOMContentLoaded', function () {
const generate = document.getElementById('generate');
generate.addEventListener('click', generatePassword);
});
document.addEventListener('DOMContentLoaded', function () {
const copy = document.getElementById('copy');
copy.addEventListener('click', returnPassword);
});
function returnPassword (){
const textarea = document.createElement("textarea");
const password = result.innerText;
if (!password) {
return;
}
textarea.value = password;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
const btnCopty = document.getElementById('copy');
btnCopty.innerText = "Copied!"
}
function generatePassword() {
const result = document.getElementById("result");
const lenght = document.getElementById("lenght");
const upper = document.getElementById("upper");
const lower = document.getElementById("lower");
const number = document.getElementById("number");
const symbol = document.getElementById("symbol");
const upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowerLetters = "abcdefghijklmnopqrstuvwxyz";
const numbers = "0123456789";
const symbols = "!@#$%^&*()_+=";
if(!upper.checked && !lower.checked && !number.checked && !symbol.checked){
result.innerText = 'Select one!';
return;
}
let password = "";
if (upper.checked) {
password += getUppercase();
}
if (lower.checked) {
password += getLowercase();
}
if (number.checked) {
password += getNumber();
}
if (symbol.checked) {
password += getSymbol();
}
for (let i = password.length; i < lenght.value; i++) {
const x = generateX();
password += x;
}
result.innerText = password;
function getLowercase() {
return lowerLetters[Math.floor(Math.random() * lowerLetters.length)];
}
function getUppercase() {
return upperLetters[Math.floor(Math.random() * upperLetters.length)];
}
function getNumber() {
return numbers[Math.floor(Math.random() * numbers.length)];
}
function getSymbol() {
return symbols[Math.floor(Math.random() * symbols.length)];
}
function generateX() {
const xs = [];
if (upper.checked) {
xs.push(getUppercase());
}
if (lower.checked) {
xs.push(getLowercase());
}
if (number.checked) {
xs.push(getNumber());
}
if (symbol.checked) {
xs.push(getSymbol());
}
if (xs.length === 0) return "";
return xs[Math.floor(Math.random() * xs.length)];
}
}