-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.js
157 lines (144 loc) · 4.1 KB
/
root.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
import {init, render} from "https://tatomyr.github.io/purity/purity.js"
import {ls} from "https://tatomyr.github.io/purity/ls.js"
import {getRecurrPw, inputAdapter} from "./app.js"
const {put, get} = ls("lordofpasswords")
const {mount, getState, setState} = init({
...get({passwordlength: 12}),
showNotification: false,
})
// Visual effects
const onSuccessfullCopy = () => {
setState(() => ({showNotification: true}))
setTimeout(() => {
setState(() => ({showNotification: false}))
}, 10000)
}
// Handlers
const copyPassword = async password => {
try {
navigator.clipboard.writeText(password)
onSuccessfullCopy()
} catch (err) {
window.prompt(
`Could not copy the password. Please do it manually:`,
password
)
}
}
const handleSubmit = e => {
e.preventDefault()
const passwordlength = +e.target.passwordlength.value
put({passwordlength})
setState(() => ({passwordlength}))
getRecurrPw(inputAdapter(e.target), password => {
e.target.reset()
if (e.submitter.name === "showpassword") {
setTimeout(() => window.alert(password), 300)
} else {
copyPassword(password)
}
e.target.service.focus()
})
}
const root = () => render`
<main id="root">
<form
::submit=${handleSubmit}
::input=${() => {
setState(() => ({showNotification: false}))
}}
>
<section class="form-group">
<label for="service">
<div class="field-name">Public service (site) name <span>🌎</span></div>
<input
class="field-value"
name="service"
id="service"
placeholder="public service name"
required
autofocus
/>
</label>
</section>
<section class="form-group">
<label for="masterpassword">
<div class="field-name">Masterpassword <span>🔑</span></div>
<input
class="field-value"
name="masterpassword"
id="masterpassword"
type="password"
placeholder="your secure key"
required
/>
</label>
</section>
<section class="form-group">
<label for="passwordlength" class="inline">
<div>Password length <span>#️⃣</span></div>
<input
name="passwordlength"
id="passwordlength"
type="number"
min="6"
max="256"
value="${getState().passwordlength}"
required
/>
</label>
</section>
<section class="form-group wrap">
<label for="special" class="inline">
<section class="checkbox-wrapper">
<input name="special" id="special" type="checkbox" />
</section>
Use special characters
</label>
<label for="casesensitive" class="inline">
<section class="checkbox-wrapper">
<input name="casesensitive" id="casesensitive" type="checkbox" />
</section>
Case sensitive
</label>
</section>
<section id="buttons" class="form-group buttons wrap">
${
getState().showNotification
? render`
<div class="notification button-like">
Copied to clipboard
</div>
`
: render`
<button name="copypassword">
Copy password to clipboard
</button>
<button name="showpassword">
Show password
</button>
`
}
</section>
</form>
</main>
`
mount(root)
if ("serviceWorker" in navigator && window.location.protocol !== "http:") {
navigator.serviceWorker
.register("./lordofpasswords.sw.js")
.then(registration => {
// eslint-disable-next-line no-console
console.info(
"[lordofpasswords.sw] Registration successful, scope is:",
registration.scope
)
})
.catch(error => {
// eslint-disable-next-line no-console
console.info(
"[lordofpasswords.sw] Service worker registration failed, error:",
error
)
})
}