-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
125 lines (103 loc) · 3.86 KB
/
Copy pathscript.js
File metadata and controls
125 lines (103 loc) · 3.86 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Number of password segments to generate
let selectedSegments = 3;
/**
* Generates a password consisting of multiple segments of characters.
* Each segment follows a specific pattern: consonant-vowel-consonant-consonant-vowel-consonant.
* Some segments may include a number or an uppercase letter at random positions.
*
* @param {number} segments - Number of segments in the password.
* @returns {string} - The generated password.
*/
function generatePassword(segments) {
// Character sets for password generation
const chars = {
consonants: "bcdfghjklmnpqrstvwxyz",
vowels: "aeiou",
numbers: "0123456789",
};
// Function to get a random character from the specified type
const getRandomChar = (type) =>
chars[type][Math.floor(Math.random() * chars[type].length)];
// Random positions for a number and an uppercase letter in the password
const numberPos = Math.floor(Math.random() * segments);
const uppercasePos = Math.floor(Math.random() * segments);
// Function to create a single segment of the password
const createSegment = (index) => {
let segment = [
getRandomChar("consonants"),
getRandomChar("vowels"),
getRandomChar("consonants"),
getRandomChar("consonants"),
getRandomChar("vowels"),
getRandomChar("consonants"),
].join("");
// Pick a random position
const randomIndex = Math.floor(Math.random() * segment.length);
if (index === numberPos) {
segment =
segment.slice(0, randomIndex) + // Characters before the random index
getRandomChar("numbers") + // Insert random number
segment.slice(randomIndex + 1); // Characters after the replaced character
}
if (index === uppercasePos)
segment =
segment.slice(0, randomIndex) +
segment[randomIndex].toUpperCase() +
segment.slice(randomIndex + 1);
return segment;
};
// Generate the required number of segments and join them with a "-" (hyphen)
return Array.from({ length: segments }, (_, i) => createSegment(i)).join("-");
}
/**
* Resets the copy icon to its default state.
*/
function resetIcon() {
document.querySelector("#copy-icon").setAttribute("data-lucide", "copy");
lucide.createIcons();
}
/**
* Updates the displayed password with a newly generated one.
*/
const updatePassword = () => {
document.querySelector("#password").textContent =
generatePassword(selectedSegments);
resetIcon();
};
/**
* Handles tab button clicks to change the number of password segments.
* Updates the UI and regenerates the password.
*
* @param {Event} event - The click event from the tab button.
*/
const handleTabClick = (event) => {
document
.querySelectorAll(".tab")
.forEach((btn) => btn.classList.remove("active"));
event.currentTarget.classList.add("active");
selectedSegments = parseInt(event.currentTarget.dataset.value);
updatePassword();
};
/**
* Handles clicks on the password container to copy the password to the clipboard.
* Changes the icon to indicate success and resets it after a delay.
*/
const handlePasswordContainerClick = () => {
const passwordText = document.querySelector("#password").textContent;
navigator.clipboard.writeText(passwordText).then(() => {
resetIcon("check"); // Change the icon to a checkmark
setTimeout(resetIcon, 5000); // Reset the icon after 5 seconds
});
};
// Attach event listeners to tab buttons
document
.querySelectorAll(".tab")
.forEach((btn) => btn.addEventListener("click", handleTabClick));
// Attach event listener to the generate button
document.querySelector("#generate").addEventListener("click", updatePassword);
// Attach event listener to the password container for copying functionality
document
.querySelector("#password-container")
.addEventListener("click", handlePasswordContainerClick);
// Generate a default password when the page loads
window.onload = updatePassword;