/
index.js
103 lines (85 loc) · 2.27 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
// Libraries
import 'bootstrap'
// Styles
import './style.scss'
// FontAwesome 5
import {
library,
dom
} from '@fortawesome/fontawesome-svg-core'
import {
faCog
} from '@fortawesome/free-solid-svg-icons'
library.add(faCog)
dom.watch()
// Application code
import storage from './storage.js'
let now = () => {
let d = new Date()
return d.getTime()
}
let stopRefresh = () => {
clearInterval(window.ticker)
$('#focusedInput').select()
return false
}
let restartRefresh = () => {
window.lastLoad = now()
window.ticker = setInterval(tick, window.tickerInterval)
}
let setProgress = (perc) => {
$('.progress-bar').css('width', `${perc}%`)
}
let loadPassword = () => {
let options = loadOptions()
$.get(`/v1/getPassword?length=${options.passwordLength}&special=${options.useSpecial}&xkcd=${options.useXKCD}&separator=${options.xkcdSeparator}`, (data) => {
$('#focusedInput').val(data)
window.lastLoad = now()
})
}
let saveOptions = () => {
let options = {
passwordLength: $('#passwordLengthOption').val(),
useSpecial: $('#useSpecialOption')[0].checked,
useXKCD: $('#useXKCDOption')[0].checked,
xkcdSeparator: $('#xkcdSeparator').val(),
}
storage.set('SecurePasswordOptions', options)
$('#settingsModal').modal('hide')
loadPassword()
}
let loadOptions = () => {
let options = storage.get('SecurePasswordOptions')
if (!options) {
options = {
passwordLength: 20,
useSpecial: false,
useXKCD: false,
xkcdSeparator: '',
}
}
$('#passwordLengthOption').val(options.passwordLength || 20)
$('#useSpecialOption')[0].checked = options.useSpecial
$('#useXKCDOption')[0].checked = options.useXKCD
$('#xkcdSeparator').val(options.xkcdSeparator || '')
return options
}
let tick = () => {
let diff = now() - window.lastLoad
let perc = (window.refreshPassword - diff) / window.refreshPassword * 100
setProgress(perc)
if (diff >= window.refreshPassword) {
loadPassword()
}
}
$(() => {
window.lastLoad = now()
window.refreshPassword = 30000
window.tickerInterval = 200
window.ticker = setInterval(tick, window.tickerInterval)
$('#focusedInput').bind('click', stopRefresh)
$('#focusedInput').bind('blur', restartRefresh)
$('#optionSave').bind('click', saveOptions)
loadOptions()
loadPassword()
})