1
0
Fork 0
VaultUI/src/pages/PwGen.js

57 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-05-03 09:25:42 +01:00
import { CopyableInputBox } from "../elements/CopyableInputBox.js";
2021-04-15 13:01:58 +01:00
import { Page } from "../types/Page.js";
import { makeElement } from "../htmlUtils.js";
2021-05-03 09:25:42 +01:00
import { setPageContent } from "../pageUtils.js";
import i18next from 'i18next';
2021-04-15 13:01:58 +01:00
function random() {
2021-04-15 15:37:52 +01:00
const {
crypto,
Uint32Array
} = window;
2021-04-15 13:01:58 +01:00
2021-04-15 15:37:52 +01:00
if (typeof (crypto === null || crypto === void 0 ? void 0 : crypto.getRandomValues) === 'function' && typeof Uint32Array === 'function') {
return window.crypto.getRandomValues(new Uint32Array(1))[0] / 4294967295;
}
2021-04-15 13:01:58 +01:00
2021-04-15 15:37:52 +01:00
return Math.random();
2021-04-15 13:01:58 +01:00
}
function genPw(len) {
2021-04-15 15:37:52 +01:00
let pw = "";
2021-04-18 10:42:57 +01:00
const pwArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'()*+,-./:;<=>?@[]^_{|}~".split('');
2021-04-15 15:37:52 +01:00
for (let i = 0; i < len; i++) {
pw = pw.concat(pwArray[Math.floor(random() * pwArray.length)]);
}
return pw;
2021-04-15 13:01:58 +01:00
}
export class PwGenPage extends Page {
2021-04-15 15:37:52 +01:00
constructor() {
super();
}
async render() {
let inputBox = CopyableInputBox(genPw(24));
setPageContent(makeElement({
tag: "div",
children: [
inputBox,
makeElement({
tag: "button",
text: i18next.t("gen_password_btn"),
2021-04-15 15:37:52 +01:00
class: ["uk-button", "uk-button-primary", "uk-margin-bottom"],
onclick: () => {
inputBox.setText(genPw(24));
}
})
]
}));
}
get name() {
return i18next.t("password_generator_title");
2021-04-15 15:37:52 +01:00
}
2021-04-15 13:01:58 +01:00
}