-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (88 loc) · 3.13 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
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 startLength = 16;
let lastLength = 0;
const slider = $('.ui.slider').slider({
min: 1,
max: 64,
start: startLength,
step: 1,
onMove: (e) => {
$('#length').val(e)
if(lastLength != e)
newPassword();
lastLength = e;
}
})
$('#length').val(startLength)
$('#generatePassword').click(() => newPassword());
$('#copyPassword').click(function(){
clearTimeout(popupTimer);
$('#password').select();
document.execCommand('copy');
$('#password').blur();
$(this).popup({
title: 'Successfully copied to the clipboard!',
on: 'manual',
exclusive: true
}).popup('show');
delayPopup(this);
});
var popupTimer;
function delayPopup(popup){
popupTimer = setTimeout(function(){ $(popup).popup('hide')}, 1000);
}
$('#length').on('input', function(e) {
let value = parseInt(e.target.value);
if(value > 0)
slider.slider('set value', value);
})
$('.ui.checkbox').checkbox({
onChange: () => newPassword()
})
Object.defineProperty(Array.prototype, 'randomIndex', {
value: function() { return Math.floor(Math.random()*this.length);}
})
Object.defineProperty(Array.prototype, 'random', {
value: function() { return this[this.randomIndex()];}
})
const newPassword = () => {
let length = $('.ui.slider').slider('get value');
let includeLowercase = $('#includeLowercase').checkbox('is checked');
let includeUppercase = $('#includeUppercase').checkbox('is checked');
let includeNumbers = $('#includeNumbers').checkbox('is checked');
let includeSymbols = $('#includeSymbols').checkbox('is checked');
let password = generatePassword(length, includeLowercase, includeUppercase, includeNumbers, includeSymbols);
$('#password').val(password);
}
newPassword();
function generatePassword(length, includeLowercase, includeUppercase, includeNumbers, includeSymbols)
{
if(!includeLowercase && !includeUppercase && !includeNumbers && !includeSymbols)
return "";
const lowercaseLetters = [...'abcdefghijklmnopqrstuvwxyz'];
const uppercaseLetters = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'];
const numbers = [...'0123456789'];
const symbols = [...'!"#$%&\'()*+,-./'];
var chars = [
...includeLowercase ? lowercaseLetters : [],
...includeUppercase ? uppercaseLetters : [],
...includeNumbers ? numbers : [],
...includeSymbols ? symbols : [],
]
let mustIncludeChars = [];
if(includeLowercase)
mustIncludeChars.push(lowercaseLetters.random());
if(includeUppercase)
mustIncludeChars.push(uppercaseLetters.random());
if(includeNumbers)
mustIncludeChars.push(numbers.random());
if(includeSymbols)
mustIncludeChars.push(symbols.random());
let password = [];
for(let i = 0; i< length-mustIncludeChars.length; i++)
password.push(chars.random());
mustIncludeChars.map(c => password.splice(password.randomIndex(), 0, c));
return password.join("");
}
function characters(firstChar, lastChar) {
return Array.from(Array(lastChar.charCodeAt(0)-firstChar.charCodeAt(0)+1).keys()).map(c => String.fromCharCode(c+firstChar.charCodeAt(0)));
}