-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
202 lines (176 loc) · 7.09 KB
/
popup.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"use strict";
/**
* Password generator popup script
*
* @author Michal Tichý - ajda2 [https://github.com/ajda2]
* @package Password generator
* @copyright Michal Tichý - ajda2 [https://github.com/ajda2]
*/
var PasswordGenerator = PasswordGenerator || {};
/**
* Base app configuration
* @type {{passwordInputId: string, lengthInputId: string, submitButtonId: string}}
*/
PasswordGenerator.config = {
passwordInputId: 'password',
lengthInputId: 'length',
submitButtonId: 'submit',
settingsButtonId: 'settingsButton',
settingsHolderId: 'settingsHolder',
lengthValueOutId: 'lengthValue',
settingsButtonActiveClass: 'active',
settingButtons: {
involveNumbersInputId: 'settingsInvolveNumbers',
involveCzechCharsInputId: 'settingsInvolveCzechChars',
involveSpecialCharsInputId: 'settingsInvolveSpecialChars'
}
};
/**
* Settings for output password
* @type {{involveNumbers: boolean, involveCzechChars: boolean, involveSpecialChars: boolean, passwordLength: number}}
*/
PasswordGenerator.passwordSettings = {
involveNumbers: true,
involveCzechChars: true,
involveSpecialChars: true,
passwordLength: 18
};
/**
* Sources from password will be generated
* @type {{chars: string, numbers: string, czechChars: string, specialChars: string}}
*/
PasswordGenerator.passwordSources = {
chars: 'ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz',
numbers: '0123456789',
czechChars: 'ěščřžýáíéů',
specialChars: '+@#$%^&*()-_=[{]};:|,<.>/?~'
};
/**
* Load stored configuration
*/
PasswordGenerator.loadSettingsFromStorage = function () {
// TODO: Implement
};
/**
* Generates random password from selected sources
* @param length
* @param useNumbers
* @param useCzechChars
* @param useSpecialChars
* @returns {string}
*/
PasswordGenerator.generatePassword = function (length, useNumbers, useCzechChars, useSpecialChars) {
var passwordInputString = PasswordGenerator.passwordSources.chars;
var outputPassword = '';
if (useNumbers) {
passwordInputString += PasswordGenerator.passwordSources.numbers;
}
if (useCzechChars) {
passwordInputString += PasswordGenerator.passwordSources.czechChars;
}
if (useSpecialChars) {
passwordInputString += PasswordGenerator.passwordSources.specialChars;
}
for (var i = 0; i < length; i++) {
outputPassword += passwordInputString[Math.round(Math.random() * (passwordInputString.length - 1))]
}
return outputPassword;
};
/**
* Submit button click listener
*/
PasswordGenerator.submitButtonClickListener = function () {
var passwordInput = document.getElementById(PasswordGenerator.config.passwordInputId);
var lengthInput = document.getElementById(PasswordGenerator.config.lengthInputId);
var involveNumbersInput = document.getElementById(PasswordGenerator.config.settingButtons.involveNumbersInputId);
var involveCzechInput = document.getElementById(PasswordGenerator.config.settingButtons.involveCzechCharsInputId);
var involveSpecialInput = document.getElementById(PasswordGenerator.config.settingButtons.involveSpecialCharsInputId);
PasswordGenerator.passwordSettings.passwordLength = lengthInput.value;
PasswordGenerator.passwordSettings.involveNumbers = involveNumbersInput.checked;
PasswordGenerator.passwordSettings.involveCzechChars = involveCzechInput.checked;
PasswordGenerator.passwordSettings.involveSpecialChars = involveSpecialInput.checked;
passwordInput.value = PasswordGenerator.generatePassword(
PasswordGenerator.passwordSettings.passwordLength,
PasswordGenerator.passwordSettings.involveNumbers,
PasswordGenerator.passwordSettings.involveCzechChars,
PasswordGenerator.passwordSettings.involveSpecialChars
);
};
/**
* Password input click listener
*/
PasswordGenerator.passwordInputFocus = function () {
this.setSelectionRange(0, this.value.length);
};
/**
* Settings button click listener
*/
PasswordGenerator.settingsButtonListener = function () {
var settingsHolder = document.getElementById(PasswordGenerator.config.settingsHolderId);
if (PasswordGenerator.hasClass(this, PasswordGenerator.config.settingsButtonActiveClass)) {
PasswordGenerator.removeClass(settingsHolder, PasswordGenerator.config.settingsButtonActiveClass);
PasswordGenerator.removeClass(this, PasswordGenerator.config.settingsButtonActiveClass);
}
else {
PasswordGenerator.addClass(settingsHolder, PasswordGenerator.config.settingsButtonActiveClass);
PasswordGenerator.addClass(this, PasswordGenerator.config.settingsButtonActiveClass);
}
};
/**
* Pure JavaScript Element has class check
* @param el
* @param className
* @return {Boolean}
*/
PasswordGenerator.hasClass = function (el, className) {
var className = " " + className + " ";
var elemClassName = el.getAttribute("class");
if ((" " + elemClassName + " ").replace(/[\n\t]/g, " ").indexOf(className) > -1) {
return true;
}
return false;
}
/**
* Add class to element
* @param el
* @param className
*/
PasswordGenerator.addClass = function (el, className) {
el.className = el.className.replace(className, ""); // first remove the class name if that already exists
el.className = el.className + className;
};
/**
* Remove class to element
* @param el
* @param className
*/
PasswordGenerator.removeClass = function (el, className) {
el.className = el.className.replace(className, ""); // first remove the class name if that already exists
};
/**
* DOM loaded
*/
document.addEventListener('DOMContentLoaded', function () {
// Load configuration from Storage
PasswordGenerator.loadSettingsFromStorage();
var passwordInput = document.getElementById(PasswordGenerator.config.passwordInputId);
var lengthInput = document.getElementById(PasswordGenerator.config.lengthInputId);
var submitButton = document.getElementById(PasswordGenerator.config.submitButtonId);
var lengthOutput = document.getElementById(PasswordGenerator.config.lengthValueOutId);
// Set values from config into GUI
var involveNumbers = document.getElementById(PasswordGenerator.config.settingButtons.involveNumbersInputId);
lengthOutput.innerHTML = PasswordGenerator.passwordSettings.passwordLength;
// generate password directly
passwordInput.value = PasswordGenerator.generatePassword(
PasswordGenerator.passwordSettings.passwordLength,
PasswordGenerator.passwordSettings.involveNumbers,
PasswordGenerator.passwordSettings.involveCzechChars,
PasswordGenerator.passwordSettings.involveSpecialChars
);
submitButton.addEventListener('click', PasswordGenerator.submitButtonClickListener);
passwordInput.addEventListener('click', PasswordGenerator.passwordInputFocus);
lengthInput.addEventListener('input', function () {
lengthOutput.innerHTML = this.value;
});
document.getElementById(PasswordGenerator.config.settingsButtonId).addEventListener('click', PasswordGenerator.settingsButtonListener);
});