-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
78 lines (65 loc) · 2.83 KB
/
popup.js
File metadata and controls
78 lines (65 loc) · 2.83 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
const uppercaseChars = 'ABCDEFGHJKMNPQRSTUVWXYZ';
const lowercaseChars = 'abcdefghjkmnpqrstuvwxyz';
const alphabeticChars = uppercaseChars + lowercaseChars + 'iolIOL';
const digitChars = '0123456789';
const specialChars = '!#$%&()*+.:;<=>?@[]^';
function generateRandomNumbers() {
const passwordLength = document.getElementById('password-length').value;
const randomValues = window.crypto.getRandomValues(new Uint8Array(passwordLength));
return randomValues;
}
function generateRandomPassword() {
let charList = lowercaseChars;
charList = document.getElementById('capital').checked ? charList + uppercaseChars : charList;
charList = document.getElementById('numbers').checked ? charList + digitChars : charList;
charList = document.getElementById('symbols').checked ? charList + specialChars : charList;
const randomValues = generateRandomNumbers();
const password = Array.from(randomValues)
.map((val) => charList[val % charList.length])
.join('');
return password
}
function displayRandomPassword() {
let password = generateRandomPassword();
while (!checkPasswordCriteria(password)) {
password = generateRandomPassword();
}
document.getElementById('password-1').textContent = password;
}
function checkPasswordCriteria(password) {
let capital = document.getElementById('capital').checked;
let numbers = document.getElementById('numbers').checked;
let symbols = document.getElementById('symbols').checked;
if (capital && !(/[A-Z]/).test(password)) return false;
if (numbers && !(/[0-9]/).test(password)) return false;
if (symbols && !(/[!#$%&()*+.:;<=>?@\[\]^]/).test(password)) return false;
return true;
}
function displayPasswordLength() {
const passwordLength = document.getElementById('password-length').value;
document.getElementById('password-length-label').textContent = passwordLength + ' characters';
}
function copyPassword() {
const password = document.getElementById('password-1').textContent;
navigator.clipboard.writeText(password).catch(error => {
console.error(error.message);
});
}
function setupEventListeners() {
const passwordLength = document.getElementById('password-length');
passwordLength.addEventListener('input', displayRandomPassword);
passwordLength.addEventListener('input', displayPasswordLength);
const checkbox = document.getElementById('checkbox');
checkbox.addEventListener('change', displayRandomPassword);
const regenerate = document.getElementById('regenerate');
regenerate.addEventListener('click', displayRandomPassword);
const password = document.getElementById('password-1');
password.addEventListener('click', copyPassword);
}
function displayDefault() {
displayRandomPassword();
}
document.addEventListener('DOMContentLoaded', function() {
displayDefault();
setupEventListeners();
});