2021-04-15 13:01:58 +01:00
|
|
|
import { Page } from "../types/Page.js";
|
|
|
|
import { setPageContent } from "../pageUtils.js";
|
|
|
|
import { makeElement } from "../htmlUtils.js";
|
|
|
|
|
|
|
|
import { CopyableInputBox } from "../elements/CopyableInputBox.js";
|
|
|
|
|
|
|
|
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: "Gen New Password",
|
|
|
|
class: ["uk-button", "uk-button-primary", "uk-margin-bottom"],
|
|
|
|
onclick: () => {
|
|
|
|
inputBox.setText(genPw(24));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return "Password Generator";
|
|
|
|
}
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|