aboutsummaryrefslogtreecommitdiff
path: root/www/index.js
blob: c8d55085052992f94e6c53aab848a6ac7ce80fbe (plain)
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
import * as wasm from "genpass";

const DEFAULT_LEN = 32;

let passwordInput = document.getElementById("password");
let generateButton = document.getElementById("generate");
let copyButton = document.getElementById("copy");
let sizeInput = document.getElementById("size");
let upperCaseInput = document.getElementById("upperCase");
let lowerCaseInput = document.getElementById("lowerCase");
let numberInput = document.getElementById("number");
let specialInput = document.getElementById("special");
let extendedInput = document.getElementById("extended");

function copyToClipboard() {
    let text = passwordInput.innerText
    navigator.clipboard.writeText(text).then(function () {
    }, function (err) {
        console.error('Async: Could not copy text: ', err);
    });

    copyButton.textContent = "copied"

    setTimeout(() => {
        copyButton.innerText = "copy"
    }, 3000);
}

function generate() {
    let size = sizeInput.value;
    let lower = lowerCaseInput.checked;
    let upper = upperCaseInput.checked;
    let number = numberInput.checked;
    let special = specialInput.checked;
    let extended = extendedInput.checked;
    passwordInput.innerHTML = wasm.generate(size, lower, upper, number, special, extended);
}

// Setting up defaults
sizeInput.value = DEFAULT_LEN;
upperCaseInput.checked = true;
lowerCaseInput.checked = true;
numberInput.checked = true;
specialInput.checked = true;
extendedInput.checked = false;
//this will trigger passwordInput to be filled
generate();

//Adding events hooks

sizeInput.onchange = generate;
upperCaseInput.onchange = generate;
lowerCaseInput.onchange = generate;
numberInput.onchange = generate;
specialInput.onchange = generate;
extendedInput.onchange = generate;
generateButton.onclick = generate;
copyButton.onclick = copyToClipboard;