2018-11-26 22:24:31 -05:00
|
|
|
import $ from 'jquery/dist/jquery'
|
|
|
|
import 'form-serializer/jquery.serialize-object'
|
|
|
|
|
2020-06-15 22:29:41 -04:00
|
|
|
import { generate, computeBits, defaultSymbol } from './generator'
|
2018-11-26 22:24:31 -05:00
|
|
|
|
|
|
|
$(() => {
|
|
|
|
const $options = $('#options-form')
|
|
|
|
const $output = $('#generated-password')
|
|
|
|
const $bits = $('#password-bits').find('div')
|
|
|
|
const classes = 'bg-danger bg-warning bg-info bg-success'
|
2018-11-29 01:36:07 -05:00
|
|
|
const defaults = {
|
2020-06-15 22:29:41 -04:00
|
|
|
list: 'large',
|
2018-11-29 01:36:07 -05:00
|
|
|
count: 4,
|
2020-06-15 22:29:41 -04:00
|
|
|
symbol: true,
|
|
|
|
separator: defaultSymbol,
|
2018-11-29 01:36:07 -05:00
|
|
|
}
|
2018-11-26 22:24:31 -05:00
|
|
|
|
|
|
|
function bitClass (bits) {
|
|
|
|
if (bits < 44) {
|
|
|
|
return 'bg-danger'
|
|
|
|
} else if (bits < 64) {
|
|
|
|
return 'bg-warning'
|
|
|
|
} else if (bits < 80) {
|
|
|
|
return 'bg-info'
|
|
|
|
} else {
|
|
|
|
return 'bg-success'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bitRound (value) {
|
|
|
|
const rounded = Math.round(value)
|
|
|
|
return rounded === value ? value : `≈ ${rounded}`
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateBitMeter () {
|
|
|
|
const options = $options.serializeObject()
|
|
|
|
const bits = computeBits(options)
|
|
|
|
const maxBits = 96
|
|
|
|
$bits
|
|
|
|
.removeClass(classes)
|
|
|
|
.addClass(bitClass(bits))
|
|
|
|
.text(`${bitRound(bits)} bits`)
|
|
|
|
.css('width', `${bits / maxBits * 100}%`)
|
2018-11-26 22:31:48 -05:00
|
|
|
.attr('aria-valuenow', bits)
|
|
|
|
.attr('aria-valuemax', maxBits)
|
2018-11-26 22:24:31 -05:00
|
|
|
}
|
|
|
|
|
2018-11-29 01:36:07 -05:00
|
|
|
function loadSettings (settings) {
|
|
|
|
$('#word-list').val(settings.list || 'small')
|
|
|
|
$('#word-count').val(settings.count || 4)
|
|
|
|
$options.find(':checkbox').each(function () {
|
|
|
|
$(this).prop('checked', !!settings[this.name])
|
|
|
|
})
|
2020-06-15 22:29:41 -04:00
|
|
|
$('#separator-symbol').val(settings.separator || defaultSymbol)
|
2018-11-29 01:36:07 -05:00
|
|
|
updateBitMeter()
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:31:48 -05:00
|
|
|
if (window.crypto && window.crypto.getRandomValues) {
|
|
|
|
$('#too-old').hide()
|
|
|
|
|
|
|
|
$('#run-generator').click(() => {
|
|
|
|
const options = $options.serializeObject()
|
2020-06-15 23:03:13 -04:00
|
|
|
$output.text(generate(options)).removeClass('placeholder')
|
|
|
|
$('#copy-password').prop('disabled', false)
|
2018-11-26 22:31:48 -05:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2018-11-29 01:36:07 -05:00
|
|
|
$('#save-settings').click(() => {
|
|
|
|
const options = $options.serializeObject()
|
|
|
|
window.localStorage.setItem('settings', JSON.stringify(options))
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
$('#clear-settings').click(() => {
|
|
|
|
window.localStorage.removeItem('settings')
|
|
|
|
loadSettings(defaults)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2018-11-26 22:31:48 -05:00
|
|
|
$options.find('select, input').change(updateBitMeter)
|
|
|
|
$options.find('input[type=nubmer]').on('input', updateBitMeter)
|
|
|
|
updateBitMeter()
|
|
|
|
}
|
2018-11-29 01:36:07 -05:00
|
|
|
|
|
|
|
const settings = window.localStorage.getItem('settings')
|
|
|
|
if (settings) {
|
|
|
|
try {
|
|
|
|
loadSettings(JSON.parse(settings))
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
}
|
2018-11-26 22:24:31 -05:00
|
|
|
})
|