-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
163 lines (135 loc) · 3.84 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
let enWordList = [];
let output = document.getElementById('output');
let inputTextArea = document.getElementById('input-text');
let findBtn = document.getElementById('find');
import("./words.en.js").then(({words}) => {
enWordList = words()
output.innerHTML = '';
});
// UTILS
function escapeStringRegexp(string) {
return string
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d');
}
function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}
function formatDate(date) {
return (
[
date.getFullYear(),
padTo2Digits(date.getMonth() + 1),
padTo2Digits(date.getDate()),
].join('-') +
' ' +
[
padTo2Digits(date.getHours()),
padTo2Digits(date.getMinutes()),
padTo2Digits(date.getSeconds()),
].join('.')
);
}
function formatFileName(title) {
return formatDate(new Date()) + ' ' + title.substring(0, 50).replace(/\W+/g, ' ') + '.txt'
}
// DOWNLOAD TEXT
function downloadText(filename, text) {
if (isWindows()) {
text = text.replace(/\r?\n/g, "\r\n");
}
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
//OS
function isWindows() {
if (window && window.navigator && window.navigator.userAgent) {
return window.navigator.userAgent.toLowerCase().indexOf("windows") != -1;
}
return false;
}
// EVENTS
function addOnClick(id, fn) {
let el = document.getElementById(id);
el.onclick = fn;
}
function find() {
window.scrollTo(0,0)
output.innerHTML = 'Searching...';
setTimeout(function(){
let v = escapeStringRegexp(inputTextArea.value)
v = v.replaceAll('\\*','.*').replaceAll('\\?','.'); // wildcard
console.log(v);
let r = new RegExp('^' + v+ '$', 'i')
let words = enWordList.filter(a=> r.test(a))
output.innerHTML = '';
words.forEach(w=> addOutputLine(w));
}, 100)
}
inputTextArea.onkeyup = function(e) {
e.key === "Enter" && find();
};
let zoomValue = 1;
function addOutputLine(text, invert) {
if(!text || !text.trim()){
return;
}
let o = document.createElement("li");
o.classList.add("out-line");
o.innerText = text;
let bd = document.createElement("button");
bd.innerText = "Remove";
bd.classList.add("out-btn");
bd.onclick = function () {
o.remove();
};
o.appendChild(bd);
let bc = document.createElement("button");
bc.innerText = "Copy";
bc.classList.add("out-btn");
bc.onclick = function () {
navigator.clipboard.writeText(text)
};
o.appendChild(bc);
let bg = document.createElement("span");
bg.innerText = "🔍";
bg.title = "Google search";
bg.classList.add("out-btn-ico");
bg.onclick = function () {
window.open("https://www.google.com/search?q=" + encodeURIComponent(text) , '_blank')
};
o.appendChild(bg);
output.appendChild(o);
}
addOnClick('zoom-in', function () {
zoomValue *= 1.1
inputTextArea.style.setProperty('zoom', zoomValue);
findBtn.style.setProperty('zoom', zoomValue);
output.style.setProperty('zoom', zoomValue);
});
addOnClick('zoom-out', function () {
zoomValue *= 0.9
inputTextArea.style.setProperty('zoom', zoomValue);
findBtn.style.setProperty('zoom', zoomValue);
output.style.setProperty('zoom', zoomValue);
});
addOnClick('copy', function (e) {
navigator.clipboard.writeText(output.innerText)
});
addOnClick('find', find);
addOnClick('clean', function (e) {
output.innerHTML = '';
});
addOnClick('download', function () {
downloadText(formatFileName(inputTextArea.value.trim()), output.innerText);
});
function onHeaderResize() {
body.style.setProperty('margin-top', (header.offsetHeight) + 'px');
}
onHeaderResize()
new ResizeObserver(onHeaderResize).observe(header)