-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (69 loc) · 3.3 KB
/
script.js
File metadata and controls
82 lines (69 loc) · 3.3 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
const inputNumber = document.getElementById("inputNumber");
const inputRange = document.getElementById("inputRange");
let finalPassword = document.getElementById("passw");
inputNumber.addEventListener("input", syncNumber);
inputRange.addEventListener("input", syncNumber);
// Sync range and password length inputs
function syncNumber(e){
let uniValue = e.target.value;
inputNumber.value = uniValue;
inputRange.value = uniValue;
return uniValue;
}
// Get charcode values from ASCII table, through arrayFromLowToHigh function
const uppercase_char_codes = arrayFromLowToHigh(65, 90);
const lowercase_char_codes = arrayFromLowToHigh(97, 122);
const numbers_char_codes = arrayFromLowToHigh(48, 57);
const symbols_char_codes = arrayFromLowToHigh(33,47)
.concat(arrayFromLowToHigh(58,64)
.concat(arrayFromLowToHigh(91,96)
.concat(arrayFromLowToHigh(168,175)
.concat(arrayFromLowToHigh(123,126)))))
const generatePasswordBtn = document.getElementById("generatePasswordBtn");
const form = document.getElementById("form");
form.addEventListener("submit", e => {
e.preventDefault();
const characterAmount = inputNumber.value;
const includeUpper = document.getElementById("includeUpper").checked;
const includeSymbols = document.getElementById("includeSymbols").checked;
const includeNumbers = document.getElementById("includeNumbers").checked;
// Assign values of generatePassword function to variable
generatePassword(characterAmount, includeUpper, includeSymbols, includeNumbers)
})
function generatePassword(characterAmount, includeUpper, includeSymbols, includeNumbers){
const copyBtn = document.getElementById("copyBtn")
copyBtn.classList.remove("text-white", "bg-black")
copyBtn.classList.add("text-black", "bg-white")
copyBtn.innerText = "Copy"
characterAmount = inputNumber.value;
let defaultCharCodes = lowercase_char_codes;
if(includeUpper) defaultCharCodes = defaultCharCodes.concat(uppercase_char_codes)
if(includeNumbers) defaultCharCodes = defaultCharCodes.concat(numbers_char_codes)
if(includeSymbols) defaultCharCodes = defaultCharCodes.concat(symbols_char_codes)
const passwordCharacters = [];
for (let i = 0; i < characterAmount; i++) {
const character = defaultCharCodes[Math.floor(Math.random() * defaultCharCodes.length)]
passwordCharacters.push(String.fromCharCode(character))
}
finalPassword.innerText = passwordCharacters.join('');
}
// Create function that loops through fields in ASCII table, based on a lowest number all the way to the high target number in the table.
// Push values to an array and return its values.
function arrayFromLowToHigh(low, high){
const array = [];
for (let i = low; i <= high; i++) {
array.push(i);
}
return array;
}
function copyPassword() {
const password = document.getElementById("passw");
const textToCopy = password.innerText
navigator.clipboard.writeText(textToCopy);
const copyBtn = document.getElementById("copyBtn")
copyBtn.classList.remove("text-black", "bg-white")
copyBtn.classList.add("text-white", "bg-black")
copyBtn.innerText = "✓ Copied!"
}
const copyBtn = document.getElementById("copyBtn")
copyBtn.addEventListener("click", copyPassword)