-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
101 lines (75 loc) · 2.64 KB
/
script.js
File metadata and controls
101 lines (75 loc) · 2.64 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
let wordsList = [];
let wordsLoaded = false;
function loadWordsChunk() {
fetch('words.txt')
.then(response => response.text())
.then(data => {
wordsList = data.split('\n').map(word => word.trim()).filter(word => word.length > 0);
wordsLoaded = true;
})
.catch(error => console.error('Error loading words:', error));
}
loadWordsChunk();
function getRandomWord(hasupper,minLength, maxLength) {
if (!wordsLoaded || wordsList.length === 0) return '';
let filteredWords = wordsList.filter(word => word.length >= minLength && word.length <= maxLength);
if (filteredWords.length === 0) return '';
found=filteredWords[Math.floor(Math.random() * filteredWords.length)];
if(hasupper==1){
if(Math.random()>0.75){
index=Math.floor(Math.random() * found.length);
found = found.slice(0, index) + found[index].toUpperCase() + found.slice(index + 1);
}
}
return found;
}
function getRandomNumber() {
return +String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}
function getRandomSymbol() {
const symbols = "!@#$%^&*(){}[]=<>/,.";
return symbols[Math.floor(Math.random() * symbols.length)];
}
const randomFunc = {
number: getRandomNumber,
symbol: getRandomSymbol,
};
const generate = document.getElementById("generateBtn");
generate.addEventListener("click", () => {
const length = document.getElementById("Passwordlength").value;
const hasUpper = document.getElementById("uppercase").value;
const hasNumber = document.getElementById("numbers").checked;
const hasSymbol = document.getElementById("symbols").checked;
const result = document.getElementById("PasswordResult");
result.innerText = generatePassword(
hasUpper,
hasNumber,
hasSymbol,
length
);
});
function generatePassword( upper,number, symbol, length) {
let generatedPassword = "";
const typesCount = number + symbol;
const typesArr = [{ number }, { symbol }].filter(
(item) => Object.values(item)[0]
);
for (let i = 0; i < length; i += 1) {
minim=Math.floor(Math.random() * 2) + 3;
generatedPassword+=getRandomWord(upper,minim,minim+3);
typesArr.forEach((type) => {
const funcName = Object.keys(type)[0];
generatedPassword += randomFunc[funcName]();
});
}
return generatedPassword;
}
let button = document.getElementById("clipboardBtn");
button.addEventListener("click", (e) => {
e.preventDefault();
document.execCommand(
"copy",
false,
document.getElementById("PasswordResult").select()
);
});