-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
293 lines (250 loc) · 9.21 KB
/
app.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
(function () {
let repl = [
[
[" _ _ _ "],
[" __| |_ __ ___ (_) __| |_ __ __ _ __ _ ___ "],
[" / _` | '__/ _ \\| |/ _` | '__/ _` |/ _` |/ _ \\"],
[" | (_| | | | (_) | | (_| | | | (_| | (_| | __/"],
[" \\__,_|_| \\___/|_|\\__,_|_| \\__,_|\\__, |\\___|"],
[" |___/ "],
[""],
[["h1", "Welcome to droidrage", {}]],
[""],
["High priest of the mainframe since ´99"],
[""],
],
[
[["h1", "About me", {}]],
[""],
[
"My name is Pär. I’m a self-employed developer, born on a cold autumn day in the northern part of Sweden in ’80.",
],
["I have always had a passion for learning things."],
[
"When I got my first computer at 15, I started learning how to code almost immediately.",
],
["Since ’99, I have worked professionally as a developer."],
[""],
],
[
[["h1", "Contact", {}]],
[""],
[
"You can find me at ",
["a", "github", { href: "https://github.com/pariz", target: "_blank" }],
", ",
[
"a",
"stack overflow",
{
href: "https://stackoverflow.com/users/1503261/p%c3%a4r-karlsson",
target: "_blank",
},
],
", ",
[
"a",
"linkedin",
{ href: "https://www.linkedin.com/in/parkarlsson", target: "_blank" },
],
],
["or contact me by dropping an email at hi@{replace-with-my-domain}"],
[""],
],
[
[["h1", "Tech stuff", {}]],
[""],
[
"For the past few years, I have mostly worked with Golang, Elixir, JavaScript, SQL/NoSQL DBs, Redis, and RabbitMQ.",
],
[
"The stuff I have worked on usually runs in AWS or GCP, either on k8s or nomad.",
],
[""],
[
"This site was created in the evening of a rainy august, leveraging pure vanilla JavaScript.",
],
[""],
[
"If you're interested in learning more about me, my full resumé is available upon request.",
],
[""],
],
[
[["h1", "What is droidrage?", {}]],
[""],
[
"Droidrage is the name of my company, it's sort of a wordplay between roid rage and droids.",
],
["... unfortunately few get it :/"],
[
"I decided to start my company in spring 2021 after a long time working as an employed developer.",
],
[""],
],
];
// remove the 1337 droidrage print for all crawlers
if (isCrawler()) {
repl[0].splice(0, 7);
}
let commands = [
"available commands: ",
[
["home", 0],
["whoami", 1],
["tech", 3],
["contact", 2],
["droidrage", 4],
],
];
const container = document.getElementsByClassName("terminal-output")[0];
let inputReady = false;
async function writeReplLines(lines) {
for (const line of lines) {
let [elem, elemContent, removeCursorFunction] = genElements();
container.appendChild(elem);
for (const col of line) {
switch (typeof col) {
case "object":
const link = document.createElement(col[0]);
for (const [key, value] of Object.entries(col[2])) {
link.setAttribute(key, value);
}
elemContent.appendChild(link);
await writeReplChars(col[1], link);
break;
default:
const charElem = document.createElement("span");
elemContent.appendChild(charElem);
await writeReplChars(col, charElem);
}
}
removeCursorFunction();
}
}
async function writeReplCommands() {
const [h, cmds] = commands;
let [elem, elemContent, removeCursorFunction] = genElements("nav");
container.appendChild(elem);
await writeReplChars(h, elemContent);
for (const cmd of cmds) {
const commandLink = document.createElement("a");
commandLink.href = "#/" + cmd[0];
elemContent.appendChild(commandLink);
function handleInteraction(e) {
e.preventDefault();
pushState(cmd[0], e.type);
writeRepl(repl[cmd[1]]);
}
commandLink.addEventListener("touchend", handleInteraction);
commandLink.addEventListener("click", handleInteraction);
await writeReplChars(cmd[0], commandLink);
const spaceElement = document.createElement("span");
spaceElement.innerHTML = " ";
elemContent.append(" ");
}
removeCursorFunction();
}
async function writeReplChars(chars, element) {
// Skip slow rendering if crawler
if (isCrawler()) {
element.innerHTML = chars;
return;
}
for (let i = 0; i < chars.length; i++) {
let char = chars[i];
element.innerHTML += char;
await sleep();
}
}
async function writeReplInput() {
const [inputFeedElem, inputFeedElemContent] = genElements();
//inputFeedElemContent.innerHTML += "> ";
container.appendChild(inputFeedElem);
}
async function writeRepl(lines) {
inputReady = false;
while (container.firstChild) {
container.removeChild(container.firstChild);
}
await writeReplLines(lines);
await writeReplCommands();
await writeReplInput();
inputReady = true;
}
async function sleep() {
return new Promise((resolve) => setTimeout(resolve, 20));
}
function pushState(path, type) {
window.location.hash = path;
window.goatcounter.count({
path: location.pathname + location.search + location.hash,
title: type + " to " + window.location.hash,
event: true,
});
}
function genElements(contentTag = "span") {
let cursor = document.createElement("span");
cursor.innerHTML = "_";
cursor.className = "cursor";
let elem = document.createElement("div");
elem.className = "repl-line";
let elemContent = document.createElement(contentTag);
elem.appendChild(elemContent);
elem.appendChild(cursor);
return [
elem,
elemContent,
function () {
elem.removeChild(cursor);
},
];
}
function isCrawler() {
var botPattern =
"(googlebot/|bot|Googlebot-Mobile|Googlebot-Image|Google favicon|Mediapartners-Google|bingbot|slurp|java|wget|curl|Commons-HttpClient|Python-urllib|libwww|httpunit|nutch|phpcrawl|msnbot|jyxobot|FAST-WebCrawler|FAST Enterprise Crawler|biglotron|teoma|convera|seekbot|gigablast|exabot|ngbot|ia_archiver|GingerCrawler|webmon |httrack|webcrawler|grub.org|UsineNouvelleCrawler|antibot|netresearchserver|speedy|fluffy|bibnum.bnf|findlink|msrbot|panscient|yacybot|AISearchBot|IOI|ips-agent|tagoobot|MJ12bot|dotbot|woriobot|yanga|buzzbot|mlbot|yandexbot|purebot|Linguee Bot|Voyager|CyberPatrol|voilabot|baiduspider|citeseerxbot|spbot|twengabot|postrank|turnitinbot|scribdbot|page2rss|sitebot|linkdex|Adidxbot|blekkobot|ezooms|dotbot|Mail.RU_Bot|discobot|heritrix|findthatfile|europarchive.org|NerdByNature.Bot|sistrix crawler|ahrefsbot|Aboundex|domaincrawler|wbsearchbot|summify|ccbot|edisterbot|seznambot|ec2linkfinder|gslfbot|aihitbot|intelium_bot|facebookexternalhit|yeti|RetrevoPageAnalyzer|lb-spider|sogou|lssbot|careerbot|wotbox|wocbot|ichiro|DuckDuckBot|lssrocketcrawler|drupact|webcompanycrawler|acoonbot|openindexspider|gnam gnam spider|web-archive-net.com.bot|backlinkcrawler|coccoc|integromedb|content crawler spider|toplistbot|seokicks-robot|it2media-domain-crawler|ip-web-crawler.com|siteexplorer.info|elisabot|proximic|changedetection|blexbot|arabot|WeSEE:Search|niki-bot|CrystalSemanticsBot|rogerbot|360Spider|psbot|InterfaxScanBot|Lipperhey SEO Service|CC Metadata Scaper|g00g1e.net|GrapeshotCrawler|urlappendbot|brainobot|fr-crawler|binlar|SimpleCrawler|Livelapbot|Twitterbot|cXensebot|smtbot|bnf.fr_bot|A6-Indexer|ADmantX|Facebot|Twitterbot|OrangeBot|memorybot|AdvBot|MegaIndex|SemanticScholarBot|ltx71|nerdybot|xovibot|BUbiNG|Qwantify|archive.org_bot|Applebot|TweetmemeBot|crawler4j|findxbot|SemrushBot|yoozBot|lipperhey|y!j-asr|Domain Re-Animator Bot|AddThis)";
var re = new RegExp(botPattern, "i");
var userAgent = navigator.userAgent;
return re.test(userAgent);
}
// Repl read loop
(function replReadLoop() {
let inputBuffer = "";
window.addEventListener("keydown", function (evt) {
if (!inputReady) {
return;
}
evt.preventDefault();
const replElem = container.lastChild.firstChild;
switch (evt.key) {
case "Enter":
const cmd = commands[1].find((c) => c[0] === inputBuffer);
if (cmd !== undefined) {
pushState(cmd[0], "keyboard_input");
writeRepl(repl[cmd[1]]);
}
inputBuffer = "";
break;
case "Backspace":
inputBuffer = inputBuffer.slice(0, -1);
break;
default: // append to buffer
if (/^[a-zA-Z]{1}$/.test(evt.key)) {
inputBuffer += evt.key;
}
}
replElem.innerHTML = inputBuffer;
});
})();
(function () {
let page = window.location.hash.replace("#", "");
let replIdx = 0;
if (page !== "") {
const cmd = commands[1].find((c) => c[0] === page);
if (cmd !== undefined) {
replIdx = cmd[1];
}
}
writeRepl(repl[replIdx]);
})();
})();