-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopupScript.js
113 lines (100 loc) · 3.23 KB
/
popupScript.js
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
const generatePasswordButton = document.getElementById("generate-password");
const history = document.getElementById("generate-history");
const passwordLengthInput = document.getElementById("password-length");
const passwordTextarea = document.getElementById("password");
const lowerCaseEl = document.getElementById("lowercase");
const upperCaseEl = document.getElementById("uppercase");
const numberCaseEl = document.getElementById("numberCase");
const symbolEl = document.getElementById("symbol");
var i = 0;
generatePasswordButton.addEventListener("click", () => {
const passwordLength = parseInt(passwordLengthInput.value, 10);
const hasLower = lowerCaseEl.checked;
const hasUpper = upperCaseEl.checked;
const hasNumber = numberCaseEl.checked;
const hasSymbol = symbolEl.checked;
const lower = "abcdefghijklmnopqrstuvwxyz";
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const number = "0123456789";
const symbol = "!@#$%^&*()_-+=";
let characters = "";
if (hasLower) {
characters = characters + lower;
}
if (hasNumber) {
characters = characters + number;
}
if (hasUpper) {
characters = characters + upper;
}
if (hasSymbol) {
characters = characters + symbol;
}
if (isNaN(passwordLength)) {
passwordTextarea.value = "Please enter a valid password length.";
return;
}
if (!characters) {
passwordTextarea.value = "please check atleast one box";
return;
}
let password = "";
for (let i = 0; i < passwordLength; i++) {
const index = Math.floor(
(crypto.getRandomValues(new Uint32Array(1))[0] / (0xffffffff + 1)) *
characters.length
);
// console.log(index);
password += characters[index];
}
passwordTextarea.value = password;
function setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
let expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
setCookie(i, password, 5);
i = i + 1;
if (i > 5) {
i = 0;
}
});
history.addEventListener("click", () => {
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(";");
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == " ") {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
for (var j = 0; j < 5; j++) {
// history.value(getCookie(j));
console.log(getCookie(j));
}
});
// Get the copy button element
const copyButton = document.getElementById("copy-password");
// Add an event listener to the copy button
copyButton.addEventListener("click", function () {
// Create a temporary input element
const input = document.getElementById("password");
// Select and copy the password to the clipboard
input.select();
navigator.clipboard.writeText(input.value);
// document.execCommand("copy");
// Show a confirmation message
const confirmationMessage = document.getElementById("confirmation-message");
confirmationMessage.style.display = "block";
setTimeout(() => {
confirmationMessage.style.display = "none";
}, 2000);
});