-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
429 lines (374 loc) · 12 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
(function() {
const TIPS = [
'ティーダの……やっぱなんでもない…ww',
'授業中に出したら終わるのは学生生活どころではないよな…🏫💩🩲',
'らぎ虐のギャクって、虐待のギャクだったんだ!',
'6時に起こされたと思ったのに9時だった。',
'ゆるせね~~~!!!絶対今のチクチク言葉じゃん!!',
'令和最新版!らぎのページへようこそ!',
'…ウケるね。',
'…ウケるね。(笑)',
'…そういうこともある(無責任)',
'ありえね~~~!!!',
'異臭のするissueってこと?',
];
const REPLACES = [
{
from: /^\-+$/gm,
to: '<hr style="width: 12.5%;">'
},
{
from: /^([ ◆]+)$/gm,
to: '<div style="text-align: center; margin-top: 10px; margin-bottom: 10px;">$1</div>'
},
{
from: /^(.+)$/gm,
to: '<p>$1</p>'
},
{
from: /\n\n\n+/g,
to: '<div class=many-br-like-spacing></div>'
},
{
from: /(?<!\<[^>]+)\n\n+/g,
to: '<div class=thin-br-like-spacing></div>'
},
{
from: /(?<!\<[^>]+)((.+?)|\(.+?\))/g,
to: '<span class="image-like softblink do-not-markup" title="$1.gif">$1</span>'
},
{
from: /(?<!\<[^>]+)\{\{Warning\|(.+)\}\}/gi,
to: '</p><div class=warning>$1</div><p>'
},
{
from: /(?<!\<[^>]+)\[\[twitter:(.+)\|(.+)\]\]/gi,
to: '<a href="https://twitter.com/$1" class="link-like do-not-markup" target=_blank>$2</a>'
},
{
from: /(?<!\<[^>]+)\[\[tweet:(.+)\|(.+)\]\]/gi,
to: '<a href="https://twitter.com/_/status/$1" class=link-like target=_blank>$2</a>'
},
{
from: /(?<!\<[^>]+)\{\{twitter hashtag\|(.+)\}\}/gi,
to: '<a href="https://twitter.com/hashtag/$1" class=link-like target=_blank>#$1</a>'
},
{
from: /(?<!\<[^>]+)(@)([0-9_a-z]+)/gi,
to: '<a href="https://twitter.com/$2" class="link-like do-not-markup" target=_blank>$1$2</a>'
},
{
from: /(?<!\<[^>]+)⬛/g,
to: '■',
},
{
from: /(?<!\<[^>]+)([!■✕□])/g,
to: '<span class="black-out image-like" title="$1.gif">$1</span>',
},
{
from: /(?<!\<[^>]+)♡/g,
to: "❤️",
},
{
from: /(?<!\<[^>]+)(?<paren>(”|"))(.+?)\k<paren>/g,
to: "<b>$1$3$1</b>",
},
{
from: /(?<!\<[^>]+)(?<paren>(\*))(.+?)\k<paren>/g,
to: "<b>$3</b>",
},
{
from: /(?<!\<[^>]+)(「.+?」|[.+?]|〔.+?〕)/g,
to: "<b>$1</b>",
},
{
from: /(?<!\<[^>]+)\[(.+):(.+)\]:?/g,
to: '</p><div class=warning><span class="blink image-like">$1</span>$2</div><p>'
},
{
from: /(?<!\<[^>]+)(VRChat|evil|break|learned|T1|Windows(\d+)?|i\d(\-\d+)?|Q\d+|[\??!!]+?|Twitter2?|Discord|インターネット|SAN値?(ピンチ)?|女装)/gi,
to: '<span class="image-like colorful-rotate green fast do-not-markup" title="$1.gif">$1</span>'
},
{
from: /(?<!\<[^>]+)(?<=\d)(:)(?=\d)/g,
to: '<span class="image-like colorful-rotate blue fast" title="$1.gif">$1</span>'
},
{
from: /(?<!\<[^>]+)(?<=\d)(\-)(?=\d)/g,
to: '<span class="image-like colorful-rotate blue fast" title="$1.gif">$1</span>'
},
{
from: /(?<!\<[^>]+)(?<=\d)(\.)(?=\d)/g,
to: '<span class="image-like colorful-rotate blue fast" title="$1.gif">$1</span>'
},
{
from: /(?<!\<[^>]+)(?<=\d)(,)(?=\d)/g,
to: '<span class="image-like colorful-rotate blue fast" title="$1.gif">$1</span>'
},
{
from: /(?<!\<[^>]+)(\d)/g,
to: '<span class="image-like colorful-rotate blue fast" title="$1.gif">$1</span>'
},
{
from: /^/g,
to: '<div>'
},
{
from: /\n/g,
to: '</div><div>'
},
{
from: /$/g,
to: '</div>'
},
];
const sanitizeHTML = text => {
const e = document.createElement('div');
e.textContent = text;
return e.innerHTML;
};
const dateTimeToString = dt => {
if (dt == null) return 'null';
let s = '';
s += dt.getFullYear();
s += '-'
s += (dt.getMonth()+1).toString().padStart(2, '0');
s += '-'
s += dt.getDate().toString().padStart(2, '0');
s += ' '
s += dt.getHours().toString().padStart(2, '0');
s += ':'
s += dt.getMinutes().toString().padStart(2, '0');
return s;
};
const applyArticle = (a, specified) => {
if (a.created_at)
document.getElementById('datetime').textContent =
`作成日時: ${dateTimeToString(a.created_at)}`;
if (a.created_at && a.updated_at)
if (a.created_at.getTime() != a.updated_at.getTime()) {
document.getElementById('datetime').textContent +=
`\n更新日時: ${dateTimeToString(a.updated_at)}`;
}
document.getElementById('title').textContent = a.title;
if (specified)
document.title = `${a.title} ${document.title}`;
else
document.title = `${document.title}`;
let bodyHTML = sanitizeHTML(a.body);
REPLACES.forEach(v => {
bodyHTML = bodyHTML.replace(v.from, v.to);
});
document.getElementById('body').innerHTML = bodyHTML;
};
const applyPager = links => {
const prev = document.getElementById('prev');
if (links.prev != null) {
prev.href = links.prev;
prev.classList.remove('not-allowed');
}
const next = document.getElementById('next');
if (links.next != null) {
next.href = links.next;
next.classList.remove('not-allowed');
}
};
const loadTips = () => {
document.getElementById('tips').value =
TIPS[Math.floor(Math.random() * TIPS.length)];
};
const documentReady = new Promise(resolve => {
if (document.readyState != 'loading')
resolve();
document.addEventListener('DOMContentLoaded', () => {
resolve();
});
});
const fetchArticles = new Promise(resolve => {
/** @type {string | null} */
const articleName = new URLSearchParams(location.search).get('article');
fetch('https://blog.ksrgte.ch/api/list/article')
.then(response => response.json())
.then(data => {
const articles = data.map(a => ({
created_at: new Date(a.created_at),
updated_at: new Date(a.updated_at),
title: a.id,
body: a.content,
})).sort((a, b) => b.created_at - a.created_at);
const articleTree = new Map();
for (const a of articles) {
const year = a.created_at.getFullYear().toString() + '年';
const month = (a.created_at.getMonth()+1).toString()
.padStart(2, '0') + '月';
if (!articleTree.has(year))
articleTree.set(year, new Map());
if (!articleTree.get(year).has(month))
articleTree.get(year).set(month, []);
articleTree.get(year).get(month).push(a.title);
}
if (!articles.length) {
resolve({
article: { title: "204 No Content", body: "No Content" },
links: { next: null, prev: null },
since: new Date(new Date().getTime() - (1000*60*60*24*3)),
articleTree: articleTree,
});
return;
}
let a, articleIndex, articleSpecified;
const since = articles[articles.length-1].created_at;
if (articleName == undefined) {
a = articles[0];
articleIndex = 0;
} else {
articleIndex = articles.findIndex(a => a.title == articleName);
articleSpecified = true;
if (articleIndex == -1) {
resolve({
article: { title: "404 Not Found", body: "Not Found" },
links: { next: null, prev: null },
since: since,
articleTree: articleTree,
});
return;
}
a = articles[articleIndex];
}
fetch(`https://blog.ksrgte.ch/api/article/${a.title}`)
.then(response => response.text())
.then(data => {
resolve({
article: { title: a.title, body: data },
links: {
next: 0 < articleIndex ?
`/?article=${articles[articleIndex-1].title}` : null,
prev: articleIndex+1 < articles.length ?
`/?article=${articles[articleIndex+1].title}` : null,
},
since: since,
articleTree: articleTree,
articleSpecified: articleSpecified,
});
})
.catch((v) => {
resolve({
article: { title: v.message, body: "らぎサーバーから情報を取得できません。\n\nらぎがわるいかも。" },
links: {
next: 0 < articleIndex ?
`/?article=${articles[articleIndex-1].title}` : null,
prev: articleIndex+1 < articles.length ?
`/?article=${articles[articleIndex+1].title}` : null,
},
since: since,
articleTree: articleTree,
articleSpecified: articleSpecified,
});
});
}).catch((v) => {
resolve({
article: { title: v.message, body: "らぎサーバーから情報を取得できません。\n\nらぎがわるいかも。" },
links: { next: null, prev: null },
since: new Date(new Date().getTime() - (1000*60*60*24*3)),
articleTree: new Map(),
});
});
});
const applyArticleTree = tree => {
const te = document.getElementById('article-tree');
te.innerHTML = '';
for (const [year, monthTree] of tree.entries()) {
const yearD = document.createElement('div');
const yearI = document.createElement('input');
yearI.type = 'checkbox';
yearI.id = year;
const yearS = document.createElement('label');
yearS.textContent = year;
yearS.htmlFor = year;
yearD.appendChild(yearI);
yearD.appendChild(yearS);
let sum = 0;
for (const [month, titleArray] of monthTree.entries()) {
const monthD = document.createElement('div');
const monthI = document.createElement('input');
monthI.type = 'checkbox';
monthI.id = `${year}${month}`;
const monthS = document.createElement('label');
monthS.textContent = `${month} (${titleArray.length}件)`;
monthS.htmlFor = `${year}${month}`;
sum += titleArray.length;
monthD.appendChild(monthI);
monthD.appendChild(monthS);
const monthDD = document.createElement('div');
const monthU = document.createElement('ul');
for (const title of titleArray) {
const titleL = document.createElement('li');
const titleA = document.createElement('a');
titleA.href = `/?article=${title}`
titleA.textContent = title;
titleA.classList.add('link-like');
titleL.appendChild(titleA);
monthU.appendChild(titleL);
}
monthDD.appendChild(monthU);
monthD.appendChild(monthDD);
yearD.appendChild(monthD);
}
yearS.textContent += ` (${sum}件)`;
te.appendChild(yearD);
}
const firstYearI = te.querySelector('#article-tree > div > input');
if (firstYearI) firstYearI.checked = true;
const firstMonthI = te.querySelector('#article-tree > div > div > input');
if (firstMonthI) firstMonthI.checked = true;
};
// distribution in range [0, 1] skewed towards the average=0.5 with higher input
const nTimeRandom = n => new Array(n)
.fill(0)
.map(_ => Math.random())
.reduce((sum, a) => sum + a, 0) / n;
const applyAccessCount = since => {
const ACCESS_PER_DAY = 20;
const DAY = 1000 * 24 * 60 * 60;
const localOffset = parseInt(localStorage.getItem('localOffset') ?? '0');
const accessCount = Math.floor(
(new Date().getTime() - since.getTime()) / DAY * ACCESS_PER_DAY
) + localOffset;
localStorage.setItem('localOffset', localOffset + 1);
const e = document.getElementById('counter');
e.innerHTML = '';
// how much each number should be delayed, in ms
const delayMsTable = new Array(10)
.fill(0)
.map(_ => 750 * nTimeRandom(2));
for (const c of accessCount.toString().padStart(8, '0')) {
const span = document.createElement('span');
span.classList.add('blink', 'image-like', 'big', 'hidden-number');
setTimeout(() => span.classList.remove('hidden-number'), delayMsTable[c | 0])
span.textContent = c;
span.title = `${c}.gif`;
e.appendChild(span);
}
};
Promise.allSettled([fetchArticles, documentReady]).then(v => {
applyArticle(v[0].value.article, v[0].value.articleSpecified);
applyPager(v[0].value.links);
applyAccessCount(v[0].value.since);
applyArticleTree(v[0].value.articleTree);
});
document.addEventListener('contextmenu', () => {
alert('右クリック禁止!');
event.preventDefault();
});
document.addEventListener('copy', () => {
alert('コピー禁止!');
event.preventDefault();
});
const dummyLoading = new Promise(
f => setTimeout(f, 1000 * nTimeRandom(5))
);
Promise.allSettled([fetchArticles, documentReady, dummyLoading]).then(() => {
loadTips();
document.getElementsByTagName('body')[0].classList.remove('loading');
});
})();