-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
230 lines (200 loc) · 7.51 KB
/
popup.js
File metadata and controls
230 lines (200 loc) · 7.51 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Références aux éléments de la popup
const passInput = document.getElementById('passphrase');
const setBtn = document.getElementById('setKey');
const clearBtn = document.getElementById('clearKey');
const statusDiv = document.getElementById('status');
const generateBtn = document.getElementById('generatePassword');
const site = document.getElementById('site');
const passwordResult = document.getElementById('passwordResult');
const passwordSecurity = document.getElementById('passwordSecurity');
const error = document.getElementById('error');
const siteContainer = document.getElementById('siteContainer');
const passwordResultContainer = document.getElementById('passwordResultContainer');
const passwordSecurityContainer = document.getElementById('passwordSecurityContainer');
const errorContainer = document.getElementById('errorContainer');
const lengthInput = document.getElementById('length');
const minInput = document.getElementById('lowercase');
const majInput = document.getElementById('uppercase');
const symInput = document.getElementById('numbers');
const chiInput = document.getElementById('symbols');
if (typeof browser === "undefined") {
var browser = chrome;
}
// Chargement des paramètres sauvegardés
window.addEventListener('DOMContentLoaded', () => {
hideResult();
hideError();
browser.runtime.sendMessage({ action: 'checkEncodingKey' }, (resp) => {
if (resp && resp.hasEncodingKey) {
statusDiv.style.color = 'green';
statusDiv.textContent = 'Clef définie.';
browser.runtime.sendMessage({ action: 'getEncodingKey' }, (resp) => {
passInput.value = resp.encodingKey;
})
}
});
browser.storage.local.get(
['length', 'minState', 'majState', 'chiState', 'symState'],
data => {
const defaultOptions = { length: 20, minState: true, majState: true, chiState: true, symState: true };
const options = { ...defaultOptions, ...data };
lengthInput.value = options.length;
minInput.checked = options.minState;
majInput.checked = options.majState;
symInput.checked = options.symState;
chiInput.checked = options.chiState;
updateParams(options)
}
);
});
function updateParams(options) {
browser.runtime.sendMessage({ action: 'setParams', data: {
lenghtNumber: options.length,
minState: options.minState,
majState: options.majState,
symState: options.symState,
chiState: options.chiState,
} }, (resp) => {
if (!resp | !resp.ok) {
statusDiv.style.color = 'red';
statusDiv.textContent = 'Erreur: ' + (resp && resp.error || 'n/a');
} else {
if (passInput.value) {
generatePassword();
}
}
});
}
function getParams() {
return {
length: lengthInput.value,
minState: minInput.checked,
majState: majInput.checked,
symState: symInput.checked,
chiState: chiInput.checked,
};
}
[lengthInput, minInput, majInput, symInput, chiInput].forEach(input => {
input.addEventListener('change', () => updateParams(getParams()));
});
// Définir la clef
setBtn.addEventListener('click', () => {
setPassword();
});
// Déclenche setPassword() si on appuie sur "Entrée" dans passInput
passInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
setPassword();
}
});
function setPassword() {
const pass = passInput.value.trim();
if (!pass) {
return;
}
browser.runtime.sendMessage({ action: 'getSharedValues' }, (resp) => {
if (pass === resp.encodingKey) {
passInput.value = resp.encodingKey;
return;
}
})
statusDiv.style.color = 'black';
statusDiv.textContent = 'Dérivation en cours...';
browser.runtime.sendMessage({ action: 'setEncodingKey', encodingKey: pass }, (resp) => {
if (resp && resp.ok) {
statusDiv.style.color = 'green';
statusDiv.textContent = 'Clef définie.';
generatePassword();
} else {
statusDiv.style.color = 'red';
statusDiv.textContent = 'Erreur: ' + (resp && resp.error || 'n/a');
}
});
}
// Effacer la clef
clearBtn.addEventListener('click', () => {
browser.runtime.sendMessage({ action: 'clearEncodingKey' }, (resp) => {
if (resp && resp.ok) {
statusDiv.style.color = 'black';
statusDiv.textContent = 'Clef effacée.';
passInput.value = '';
hideResult();
hideError();
}
});
});
// Copier le mot de passe
document.getElementById('copyPasswordBtn').addEventListener('click', () => {
const pwd = document.getElementById('passwordResult').textContent;
if (pwd) {
navigator.clipboard.writeText(pwd).then(() => {
});
}
});
// Générer un mot de passe pour l'onglet actif avec les paramètres avancés
generateBtn.addEventListener('click', () => {
generatePassword();
});
function generatePassword() {
const length = parseInt(document.getElementById('length').value, 10);
const minState = document.getElementById('lowercase').checked;
const majState = document.getElementById('uppercase').checked;
const chiState = document.getElementById('numbers').checked;
const symState = document.getElementById('symbols').checked;
// Sauvegarde automatique des options
browser.storage.local.set({ length, minState, majState, chiState, symState });
// Récupération de l'URL de l'onglet actif
browser.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (!tabs || !tabs.length) return;
const tab = tabs[0];
// Envoi du message au background pour générer le mot de passe
browser.runtime.sendMessage(
{
action: 'generatePassword',
url: tab.url,
options: { length, minState, majState, chiState, symState }
},
(response) => {
if (response.error) {
hideResult();
showError();
error.style.display = 'block';
error.textContent = response.error;
} else if (response.password) {
hideError();
showResult();
site.textContent = response.site;
passwordResult.textContent = response.password;
passwordSecurity.style.color = response.color;
passwordSecurity.textContent = `${response.security} (${response.bits} bits)`;
} else {
hideResult();
showError();
error.style.display = 'block';
error.textContent = 'Pas de réponse.';
}
}
);
});
}
function hideError() {
errorContainer.style.display = 'none';
error.textContent = '';
}
function showError() {
errorContainer.style.display = 'block';
}
function hideResult() {
siteContainer.style.display = 'none';
site.textContent = '';
passwordResultContainer.style.display = 'none';
passwordResult.textContent = '';
passwordSecurityContainer.style.display = 'none';
passwordSecurity.textContent = '';
}
function showResult() {
siteContainer.style.display = 'block';
passwordResultContainer.style.display = 'block';
passwordSecurityContainer.style.display = 'block';
}