-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (38 loc) · 1.71 KB
/
index.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
const characters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?",
"/"];
let generateEl = document.getElementById("generate-el");
let firstEl = document.getElementById("first-el");
let secondEl = document.getElementById("second-el");
let lenEl = document.getElementById("len");
let len = 0;
lenEl.addEventListener('input' ,function(){
len = parseFloat(lenEl.value);
});
generateEl.addEventListener("click", function(){
let firstPas = "";
let secondPas = "";
for(let i = 0; i<len; i++){
firstPas += characters[Math.floor(Math.random()* characters.length)];
secondPas += characters[Math.floor(Math.random()* characters.length)];
}
firstEl.textContent = firstPas;
secondEl.textContent = secondPas;
})
firstEl.addEventListener("click", function() {
if(firstEl.textContent !== "Generate" ){
copyToClipboard(firstEl.textContent);
firstEl.textContent = "Password copied to the clipboard";
}
});
secondEl.addEventListener("click", function() {
if(secondEl.textContent !== "Generate"){
copyToClipboard(secondEl.textContent);
secondEl.textContent = "Password copied to the clipboard";
}});
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(function() {
console.log('Text copied to clipboard');
}, function(err) {
console.error('Could not copy text: ', err);
});
}