-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword.js
More file actions
120 lines (97 loc) · 3.93 KB
/
Copy pathpassword.js
File metadata and controls
120 lines (97 loc) · 3.93 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
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function createPassword(passwordLength, charactersToInclude) {
// args: (
// passwordLength: integer
// charactersToInclude: list with possible items ["number", "lower", "upper", "special"]
// )
// uses random module to genereate a random password
// returns: string of length password_length
// possible characters
const alphabetString = 'abcdefghijklmnopqrstuvwxyz';
const lowerCase = alphabetString.split("");
const upperCase = alphabetString.toUpperCase().split('');
const numberRange = [...Array(10).keys()].map(String);
const specialCharacters = ['!', '@', '&', '$', '*'];
// create character list
characterMap = {
'number': numberRange,
'lower': lowerCase,
'upper': upperCase,
'special': specialCharacters
};
let characterList = [];
charactersToInclude.forEach(charList => {
characterList = characterList.concat(characterMap[charList]);
});
// generate password
let password = '';
for (i=0; i < passwordLength; i++) {
// create randomized index to reference characterList
const randomIndex = getRandomInt(characterList.length);
// add random char from characterList to password
const randomCharacter = characterList[randomIndex];
password += randomCharacter;
}
return password;
};
function generatePassword() {
// args: ( none )
// gets value inputs from html and outputs password to html
// returns: none
// see which items are checked
const listNumberChecked = document.getElementById("list-number").checked;
const listLowerChecked = document.getElementById("list-lower").checked;
const listUpperChecked = document.getElementById("list-upper").checked;
const listSpecialChecked = document.getElementById("list-special").checked;
// make array with lists to by included
let listsToInclude = []
if ( listNumberChecked ) listsToInclude.push('number');
if ( listLowerChecked ) listsToInclude.push('lower');
if ( listUpperChecked ) listsToInclude.push('upper');
if ( listSpecialChecked ) listsToInclude.push('special');
// record slider value for password length
const sliderValue = Number(sliderValueHtml.value);
// create and set password
password = createPassword(sliderValue, listsToInclude);
document.getElementById("password").innerHTML = password
};
function copyToClipboard(element) {
var range, selection, worked;
// select button text (password)
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
try {
// copy selection to clipboard
document.execCommand('copy');
// set password copied message
document.getElementById("copied-password").innerHTML = "Password copied to clipboard!";
// reset copied password message after 5 secs
setTimeout(function() {
document.getElementById("copied-password").innerHTML = ""
}, 5000 );
}
catch (err) {
console.log(err)
// let user know password was not copied to clipboard
document.getElementById("copied-password").innerHTML = "Unable to copy password.";
}
}
// get slider and slider output elements
let sliderValueHtml = document.getElementById("slider");
let sliderOutputHtml = document.getElementById("sliderOutput");
// set slider output to new slider value when the input changes
sliderOutputHtml.innerHTML = sliderValueHtml.value;
sliderValueHtml.oninput = () => {
sliderOutputHtml.innerHTML = sliderValueHtml.value;
}