-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.js
60 lines (54 loc) · 1.69 KB
/
password.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
import { combineLatest, concat, distinctUntilChanged, fromEvent, map, merge, of, switchMap, tap } from "https://esm.sh/rxjs@7.8.1";
const configForm = document.forms.config;
const passwordForm = document.forms.passwordForm;
const copyClipboard = document.getElementById('copyClipboard');
const regenerate = document.getElementById('regenerate');
merge(
fromEvent(passwordForm, 'submit')
).subscribe(e => e.preventDefault());
function generatePassword(length) {
return btoa(Array.from(crypto.getRandomValues(new Uint8Array(length + 3))).map(c => String.fromCharCode(c)).join('')).substr(0, length);
}
const config$ = concat(
of(undefined),
merge(
fromEvent(configForm, 'input'),
fromEvent(configForm, 'change')
)
).pipe(
map(() => ({
length: configForm.elements.passwordLength.value,
specials: configForm.elements.specials.value === 'true'
})),
distinctUntilChanged((a, b) => a === b, c => JSON.stringify(c)),
);
combineLatest([
config$,
concat(
of(undefined),
fromEvent(regenerate, 'click')
)
]).pipe(
map(([config]) => config),
map(({length, specials}) => {
let password;
do {
password = generatePassword(length > 0 ? length : 0);
} while (! (specials || /^[0-9a-zA-Z]*$/.test(password)));
return password;
}),
map(value => {
passwordForm.elements.password.value = value;
return passwordForm.elements.password
}),
switchMap(display => fromEvent(copyClipboard, 'click').pipe(
tap(() => {
if (navigator && navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(display.value);
} else {
display.select();
document.execCommand("copy");
}
})
))
).subscribe();