-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
147 lines (119 loc) · 4.07 KB
/
popup.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { load, save } from './settings';
document.addEventListener('DOMContentLoaded', async () => {
const options = await load();
document.documentElement.setAttribute('class', `theme-${options.theme}`);
const response = await fetch('wordlist/lipsum.json');
const dummyWords = await response.json();
const punctuation = ['!', '?', '.'];
const copyButton = document.getElementById('copy-button');
const lipsumTextArea = document.getElementById('lipsum-textarea');
const paragraphsInput = document.getElementById('paragraphs-input');
const wordsInput = document.getElementById('words-input');
const paragraphTagsCheckbox = document.getElementById('add-tags-checkbox');
let autoCopyTimeout;
/**
* @param {number} max
* @returns number
*/
const randomInt = (max) => Math.floor(Math.random() * max);
/**
* @param {string[]} words
* @returns string
*/
const randomWord = (words) => words[randomInt(words.length)];
/**
* @param {number} wordIndex
* @returns string
*/
const randomPunctuation = (wordIndex) => {
if (wordIndex > 1) {
if (wordIndex % randomInt(30) === 0) {
return `${randomWord(punctuation)} `;
}
if (wordIndex % randomInt(20) === 0) {
return ', ';
}
}
return ' ';
};
/**
* @param {string} text
* @returns string
*/
const capitalizeFirst = (text) => text.replace(/(?:[\n|>|?|!|.]|^)[\s]?([a-z])/g, (s) => s.toUpperCase());
const generateDummyText = () => {
clearTimeout(autoCopyTimeout);
let text = '<p>';
for (let i = 1; i <= options.paragraphCount; i += 1) {
for (let j = 1; j <= options.wordsPerParagraph; j += 1) {
text += randomWord(dummyWords);
if (j % options.wordsPerParagraph === 0) {
text += '.</p>\n\n<p>';
} else if (text.substring(text.length - 3, text.length) !== '<p>') {
text += randomPunctuation(j);
}
}
}
text += '</p>';
if (!options.includeParagraphTags) {
text = text.replace(/<\/?p>/g, '');
}
text = capitalizeFirst(text.substring(0, text.length - (options.includeParagraphTags ? 9 : 2)));
lipsumTextArea.value = text;
};
const copyText = async () => {
try {
await navigator.clipboard.writeText(lipsumTextArea.value);
copyButton.value = 'Copied!';
copyButton.disabled = 'disabled';
setTimeout(() => {
copyButton.value = 'Copy';
copyButton.disabled = false;
}, 1000);
} catch (err) {
console.error('Failed to copy text.', err.message);
}
};
const saveConfig = async () => {
if (/\d+/.test(paragraphsInput.value)) {
if (paragraphsInput.value > 999) {
paragraphsInput.value = 99;
} else if (paragraphsInput.value < 1) {
paragraphsInput.value = 1;
}
} else {
paragraphsInput.value = 1;
}
if (/\d+/.test(wordsInput.value)) {
if (wordsInput.value > 999) {
wordsInput.value = 999;
} else if (wordsInput.value < 1) {
wordsInput.value = 1;
}
} else {
wordsInput.value = 1;
}
if (paragraphsInput.value * wordsInput.value > 15000) {
wordsInput.value = 1;
paragraphsInput.value = 1;
}
options.paragraphCount = parseInt(paragraphsInput.value, 10);
options.wordsPerParagraph = parseInt(wordsInput.value, 10);
options.includeParagraphTags = paragraphTagsCheckbox.checked;
save(options);
generateDummyText();
if (options.autoCopy) {
autoCopyTimeout = setTimeout(copyText, options.autoCopyDelay);
}
};
copyButton.addEventListener('click', copyText);
paragraphsInput.value = options.paragraphCount;
paragraphsInput.addEventListener('input', saveConfig);
paragraphsInput.addEventListener('click', () => paragraphsInput.select());
wordsInput.value = options.wordsPerParagraph;
wordsInput.addEventListener('input', saveConfig);
wordsInput.addEventListener('click', () => wordsInput.select());
paragraphTagsCheckbox.checked = options.includeParagraphTags;
paragraphTagsCheckbox.addEventListener('click', saveConfig);
generateDummyText();
});