-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
90 lines (77 loc) · 2.25 KB
/
script.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
'use strict'
function $(id) {
return document.getElementById(id);
}
// js is a second-rate language and we all know it
function capitalize(word) {
return word.substring(0, 1).toUpperCase() + word.substring(1);
}
// function dictAvg(dictionary, domElement) {
// const reducer = (a, b) => {
// return a + b.length;
// };
// let number = (dictionary.reduce(reducer, 0) / dictionary.length).toPrecision(
// 3
// );
// domElement.textContent = number + ' characters';
// }
function newPhrase(dictionary, length = 4, separator = ' ', caps = 'false') {
if (length < 1) {
length = 4;
}
if (length > 99) {
length = 99;
}
let camel = false;
// don't judge me
if (caps === 'camel') {
caps = 'true';
camel = true;
}
const phrase = [];
let word;
for (let i = 0; i < length; i += 1) {
word = dictionary[Math.floor(Math.random() * dictionary.length)];
if (caps === 'true') {
word = capitalize(word);
}
phrase.push(word);
}
if (camel) {
phrase[0] = phrase[0].toLowerCase();
}
$('phraseDisplay').textContent = phrase.join(separator);
}
const multilingualURL = 'dictionaries/multilingual.json';
const diceURL = 'dictionaries/diceware.json';
const effURL = 'dictionaries/eff.json';
// const multilingualWebURL = 'https://raw.githubusercontent.com/ludant/jspass/master/dictionaries/multilingual.json';
// const diceWebURL = 'https://raw.githubusercontent.com/ludant/jspass/master/dictionaries/diceware.json';
// const effWebURL = 'https://raw.githubusercontent.com/ludant/jspass/master/dictionaries/eff.json';
const dictionaries = {};
$('instructionsLink').addEventListener('click', () => {
$('info').classList.toggle('hidden');
});
$('generateButton').addEventListener('click', () => {
newPhrase(
dictionaries[$('dictionarySelect').value],
$('phraseLength').value,
$('wordSeparator').value,
$('capitalsSelect').value,
);
});
async function loadDict(url, dictName, generate = false) {
const response = await fetch(url);
if (response.ok) {
dictionaries[dictName] = await response.json();
console.log(generate)
if (generate) {
newPhrase(dictionaries[dictName], 4, ' ', 'false');
};
} else {
console.log('fuck, ' + response.status);
}
}
loadDict(diceURL, 'diceware');
loadDict(effURL, 'eff');
loadDict(multilingualURL, 'multilingual', true);