-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
367 lines (317 loc) · 11 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
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
let isMuted = localStorage?.getItem('mute');
if (!isMuted) isMuted = 'true';
function playSound(path) {
if (isMuted === 'true') return;
const sound = new Audio(path);
sound.play();
}
function popup(message) {
const overlay = document.createElement('div');
overlay.className = 'overlay';
const popup = document.createElement('div');
popup.className = 'popup';
popup.innerHTML = `<p>${message}</p>`;
overlay.appendChild(popup);
document.body.appendChild(overlay);
overlay.addEventListener('click', () => {
overlay.remove();
});
setTimeout(() => {
overlay.remove();
}, 1000);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function fetchJson(path) {
console.log(`Try to fetch ${path}.`);
const response = await fetch(path);
if (!response.ok) return console.error(`Failed to fetch ${path}.`);
const rawData = await response.text();
const data = JSON.parse(rawData);
console.log(`Successfully fetched ${path}.`, data);
return data;
};
async function fetchText(path) {
console.log(`Try to fetch ${path}.`);
const response = await fetch(path);
if (!response.ok) return console.error(`Failed to fetch ${path}.`);
const rawData = await response.text();
let data = rawData;
if (data.endsWith('\n')) data = data.slice(0, -1);
data = data.replace(/\n/g, '<br>');
console.log(`Successfully fetched ${path}.`, data);
return data;
};
function showLoadingBar(input) {
const loading = document.createElement('div');
loading.id = 'loading';
loading.innerHTML = `「${input}」を検索しています...`;
const progressBar = document.createElement('div');
progressBar.id = 'progress-bar';
loading.appendChild(progressBar);
document.body.appendChild(loading);
const during = 1500;
setTimeout(() => {
progressBar.style.transition = `width ${during}ms linear`;
progressBar.style.width = '0%';
}, 10);
setTimeout(() => {
loading.style.opacity = '0';
setTimeout(() => loading.remove(), 500);
}, during);
}
function search() {
const searchInput = document.getElementById('search');
const searchWord = searchInput.value;
const searchButton = document.getElementById('search-button');
const searchIcon = document.getElementById('search-icon');
searchButton.disabled = true;
searchIcon.src = '/img/svg/loading.svg';
searchIcon.title = 'Loading...';
searchIcon.alt = 'Loading...';
searchIcon.classList.add('spin');
const sitemapChilds = document.querySelectorAll('li[id="sitemap-child"]');
if (searchWord) showLoadingBar(searchWord);
setTimeout(() => {
searchIcon.classList.remove('spin');
searchButton.disabled = false;
searchIcon.src = '/img/svg/search.svg';
searchIcon.title = 'Search';
searchIcon.alt = 'Search';
if (searchWord) {
for (const child of sitemapChilds) {
const childLinks = child.getElementsByTagName('a');
let matchFound = false;
for (const link of childLinks) {
if (
link.textContent.includes(searchWord) ||
link.href
.replace(`${window.location.protocol}//${window.location.host}`, '')
.replace('/main/', '')
.replace(/\//g, '')
.includes(searchWord)
) {
matchFound = true;
break;
}
}
if (matchFound) {
child.style.display = 'block';
} else {
child.style.display = 'none';
}
}
} else {
for (const child of sitemapChilds) {
child.style.display = 'block';
}
}
}, 1000)
}
document.addEventListener('DOMContentLoaded', () => {
fetch('/pages.txt')
.then(response => response.text())
.then(data => {
const sitemapElement = document.getElementById('sitemap');
if (!sitemapElement) return;
const searchBar = document.getElementById('search-bar');
const searchInputWrapper = document.createElement('div');
searchInputWrapper.id = 'search-wrapper';
const searchInput = document.createElement('input');
searchInput.id = 'search';
searchInput.placeholder = 'サイトマップを検索';
searchInputWrapper.appendChild(searchInput);
searchBar.appendChild(searchInputWrapper);
searchInput.addEventListener('input', () => {
const clearButton = searchInputWrapper.querySelector('::after');
if (searchInput.value) {
searchInputWrapper.style.setProperty('--clear-button-display', 'block');
} else {
searchInputWrapper.style.setProperty('--clear-button-display', 'none');
}
});
searchInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
search();
}
});
searchInputWrapper.addEventListener('click', (event) => {
const rect = searchInputWrapper.getBoundingClientRect();
const isWithinClearButton = event.clientX > rect.right - 30 && event.clientX < rect.right;
if (isWithinClearButton) {
searchInput.value = '';
searchInput.dispatchEvent(new Event('input'));
}
});
const searchButton = document.createElement('button');
searchButton.id = 'search-button';
searchButton.onclick = search;
const searchIcon = document.createElement('img');
searchIcon.id = 'search-icon';
searchIcon.src = '/img/svg/search.svg';
searchIcon.title = 'Search';
searchIcon.alt = 'Search';
searchIcon.style.width = '20px';
searchIcon.style.height = '20px';
searchButton.appendChild(searchIcon);
searchBar.appendChild(searchButton);
const lines = data.split('\n');
const is404 = document?.getElementById('notfound');
const pageName = document.getElementById('name');
const descriptionDiv = document.getElementById('description');
const newParagraph = document.createElement('p');
pageName.textContent = '404';
newParagraph.textContent = 'Not found.';
lines.forEach(line => {
if (line.trim()) {
const args = line.split(' ');
const path = args[0];
const name = args[1];
const description = args.slice(2).join(' ');
const now = `/${window.location.href.split('/').slice(3).join('/')}`;
console.log(now);
if (
!is404 &&
(
now.includes(path) ||
(
/^\/+$/.test(now) &&
path === '/index.html'
)
)
) {
pageName.textContent = name;
newParagraph.textContent = description;
}
const listItem = document.createElement('li');
listItem.id = 'sitemap-child';
const link = document.createElement('a');
link.href = path;
link.textContent = name;
link.description = description;
listItem.appendChild(link);
sitemapElement.appendChild(listItem);
}
});
descriptionDiv.appendChild(newParagraph);
})
.catch(error => {
console.error('Error loading pages.txt:', error);
});
});
document.addEventListener('DOMContentLoaded', function () {
const functionSection = document.getElementById('function');
if (functionSection) {
const button = document.createElement('button');
button.id = 'scroll-button';
button.textContent = '⏬️';
document.body.appendChild(button);
button.addEventListener('click', function () {
functionSection.scrollIntoView({ behavior: 'smooth' });
setTimeout(() => {
button.style.display = 'none';
}, 300)
});
}
});
document.addEventListener('DOMContentLoaded', function () {
const button = document.createElement('button');
button.id = 'sound-button';
button.textContent = isMuted === 'true' ? '📣' : '🔇';
document.body.appendChild(button);
button.addEventListener('click', function () {
if (button.textContent === '📣') {
button.textContent = '🔇';
localStorage.setItem('mute', 'false');
isMuted = 'false';
} else {
button.textContent = '📣';
localStorage.setItem('mute', 'true');
isMuted = 'true';
}
popup(`ミュートを${isMuted === 'true' ? '有効化' : '無効化'}しました!`);
console.log("Mute: " + isMuted);
});
});
const tpp = document.getElementById('t-pp');
if (tpp) {
(async () => {
const link = (await fetchJson('/config.json'))['t-pp'];
const a = document.createElement('a');
a.href = link;
a.textContent = '利用規約・プライバシーポリシー';
a.target = '_blank';
tpp.appendChild(a);
})();
}
const footer = document.querySelector('footer');
if (footer) {
(async () => {
const links = await fetchJson('/links.json');
for (const link of links) {
const element = document.createElement('a');
element.href = link.url;
element.textContent = link.textContent;
element.target = '_blank';
footer.appendChild(element);
}
const copyButton = document.createElement('button');
copyButton.className = 'copy-button';
const linkIcon = '/img/svg/link_b.svg';
const checkIcon = '/img/svg/check_b.svg';
const iconImage = document.createElement('img');
iconImage.src = linkIcon;
iconImage.alt = 'Copy Link';
iconImage.style.width = '24px';
iconImage.style.height = '24px';
copyButton.appendChild(iconImage);
copyButton.title = 'Copy';
copyButton.style.position = 'absolute';
copyButton.style.right = '20px';
copyButton.style.bottom = '20px';
copyButton.style.border = 'none';
copyButton.style.background = 'transparent';
copyButton.style.cursor = 'pointer';
copyButton.style.marginLeft = '10px';
copyButton.addEventListener('click', () => {
const url = window.location.href;
navigator.clipboard.writeText(url).then(() => {
iconImage.src = checkIcon;
iconImage.alt = 'Copied';
copyButton.title = 'Success!';
popup('ページのリンクをコピーしました!');
setTimeout(() => {
iconImage.src = linkIcon;
iconImage.alt = 'Copy Link';
copyButton.title = 'Copy';
}, 1000);
}).catch(err => {
console.error(err);
});
});
footer.appendChild(copyButton);
const c = document.createElement('p');
c.innerHTML = `© 2024 - ${new Date().getFullYear()} otoneko. All rights reserved. | Project Archive`;
c.style.display = 'block';
footer.appendChild(c);
})();
}
function showBar() {
const notFound = document.getElementById('notfound');
if (!notFound) return;
notFound.innerHTML = 'ページが見つかりませんでした';
const progressBar = document.createElement('div');
progressBar.id = 'progress-bar';
notFound.appendChild(progressBar);
const during = 5000;
setTimeout(() => {
progressBar.style.transition = `width ${during}ms linear`;
progressBar.style.width = '0%';
}, 10);
setTimeout(() => {
notFound.style.opacity = '0';
setTimeout(() => notFound.remove(), 500);
}, during);
}
window.onload = showBar;