-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (63 loc) · 2.68 KB
/
Copy pathscript.js
File metadata and controls
76 lines (63 loc) · 2.68 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
const characterAmountNumber = document.querySelector("#characterAmountNumber");
const form = document.querySelector("#passwordGeneratorForm");
const includeUppercaseElement = document.querySelector("#includeUppercase");
const includeNumbersElement = document.querySelector("#includeNumbers");
const includeSymbolsElement = document.querySelector("#includeSymbols");
const passwordElement = document.querySelector("#result");
const arrayFromLowToHigh = (low, high) => {
array = [];
for (let i = low; i <= high; i++) {
array.push(i);
}
return array;
}
const UPPER_CHAR_CODES = arrayFromLowToHigh(65,90);
const LOWER_CHAR_CODES = arrayFromLowToHigh(97,122);
const NUMBER_CHAR_CODES = arrayFromLowToHigh(48,57);
const SYMBOL_CHAR_CODES = arrayFromLowToHigh(33,47).concat(arrayFromLowToHigh(58,64)).concat(arrayFromLowToHigh(91,96)).concat(arrayFromLowToHigh(123,126));
const generatePassword = (characterAmount, includeUppercase, includeNumbers, includeSymbols) => {
let charCodes = LOWER_CHAR_CODES;
if (includeUppercase) {
charCodes = charCodes.concat(UPPER_CHAR_CODES);
}
if (includeNumbers) {
charCodes = charCodes.concat(NUMBER_CHAR_CODES);
}
if (includeSymbols) {
charCodes = charCodes.concat(SYMBOL_CHAR_CODES);
}
const passwordCharacters = [];
for (let i = 0; i < characterAmount; i++) {
let characterCode = charCodes[Math.floor(Math.random() * charCodes.length)]
passwordCharacters.push(String.fromCharCode(characterCode))
}
return passwordCharacters.join('')
}
form.addEventListener('submit', e => {
e.preventDefault();
const characterAmount = characterAmountNumber.value;
const includeUppercase = includeUppercaseElement.checked;
const includeNumbers = includeNumbersElement.checked;
const includeSymbols = includeSymbolsElement.checked;
const password = generatePassword(characterAmount, includeUppercase, includeNumbers, includeSymbols)
passwordElement.innerText = password;
})
// Password copy snackbar
const copyPassword = () => {
password = passwordElement.innerText;
if (password.length > 0) {
let x = document.getElementById("snackbar");
// Add the "show" class to DIV
x.className = "show";
// After 3 seconds, remove the show class from DIV
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
const textarea = document.createElement("textarea");
textarea.value = password;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
}
}
const clipboard = document.querySelector("#clipboard")
clipboard.addEventListener('click', copyPassword)