-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
87 lines (72 loc) · 2.87 KB
/
Copy pathscript.js
File metadata and controls
87 lines (72 loc) · 2.87 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
document.addEventListener("DOMContentLoaded", () => {
const resultEl = document.getElementById('result');
const lengthEl = document.getElementById('length');
const uppercaseE1 = document.getElementById('uppercase');
const lowercaseE1 = document.getElementById('lowercase');
const numbersE1 = document.getElementById('number');
const symbolsE1 = document.getElementById('symbol');
const generateE1 = document.getElementById('generate');
const clipboardE1 = document.getElementById('clipboard');
const randomFunc = {
lower: getRandomLowercase,
upper: getRandomUppercase,
number: getRandomNumbers,
symbol: getRandomSymbols
};
generateE1.addEventListener('click', () => {
const length = +lengthEl.value;
const hasLower = lowercaseE1.checked;
const hasUpper = uppercaseE1.checked;
const hasNumber = numbersE1.checked;
const hasSymbol = symbolsE1.checked;
resultEl.innerText = generatePassword(hasLower, hasUpper, hasNumber, hasSymbol, length);
});
clipboardE1.addEventListener('click', copyToClipboard);
function copyToClipboard() {
const password = resultEl.innerText;
if (!password) {
alert('No password to copy!');
return;
}
navigator.clipboard.writeText(password)
.then(() => {
alert('Password copied to clipboard!');
})
.catch(err => {
console.error('Failed to copy:', err);
});
}
function generatePassword(lower, upper, number, symbol, length) {
let generatedPassword = '';
let typeCount = lower + upper + number + symbol;
const typeArr = [
{ key: 'lower', enabled: lower },
{ key: 'upper', enabled: upper },
{ key: 'number', enabled: number },
{ key: 'symbol', enabled: symbol }
].filter(item => item.enabled);
if (typeCount === 0) {
return '';
}
for (let i = 0; i < length; i += typeCount) {
typeArr.forEach(type => {
const keyFromRandomFun = type.key;
generatedPassword += randomFunc[keyFromRandomFun]();
});
}
return generatedPassword.slice(0, length);
}
function getRandomLowercase() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
function getRandomUppercase() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}
function getRandomNumbers() {
return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}
function getRandomSymbols() {
const symbols = "!@#$%&*(){}[]=<>/,.";
return symbols[Math.floor(Math.random() * symbols.length)];
}
});