scratch-feed/js/app.js

171 lines
4.1 KiB
JavaScript
Raw Normal View History

2024-09-18 05:02:20 -04:00
// All URLs listed here are from a custom NGINX config.
2024-09-18 01:31:24 -04:00
let area = document.querySelector('#area-main');
let entries = [];
let users = {};
let fetchData;
let sum = 0;
let doThings = false;
let i = 0;
2024-09-18 05:02:20 -04:00
let studios = {};
2024-09-18 01:31:24 -04:00
async function genTree(treeId) {
let entry = treeId;
while (true) {
let entryDat = fetchData[entry];
entries.push(entry);
if (!entryDat) break;
let us = users[entryDat.username];
let s = 1 / ((new Date() - new Date(entryDat.datetime_created.$date)) + 1000 * 60 * 60);
sum += s;
users[entryDat.username] = (us ? us : 0) + s;
entry = entryDat.parent_id;
}
return entry;
}
2024-09-18 05:02:20 -04:00
//todo: rewrite
async function main(type) {
2024-09-18 01:31:24 -04:00
let users = document.querySelector('#users').value.split('\n');
2024-09-18 05:02:20 -04:00
for (let user of users) {
let typed = user[0];
user = user.slice(1);
if (typed == '#') {
studios[typed] = await (fetch(`https://hf.zenoverse.net/studio/${user}`)
.then(x => x.json()));
}
}
2024-09-18 01:31:24 -04:00
let url = new URL(window.location);
url.search = '?users=' + users.join('&users=');
history.pushState(null, '', url);
entries = [];
for (let user of users) {
2024-09-18 05:02:20 -04:00
let typed = user[0];
user = user.slice(1);
let extraFetch = [];
if (typed == '#' && type == '#user') {
extraFetch = await (fetch(`https://hf.zenoverse.net/studio_project/${user}`)
.then(x => x.json()));
extraFetch.forEach(x => {
x.studio = studios[typed].title;
});
} else if (typed == '#' && type == '#comment') {
extraFetch = await (fetch(`https://hf.zenoverse.net/studio_comment/${user}`)
.then(x => x.json()));
2024-09-18 05:08:14 -04:00
extraFetch.forEach(x => {
x.studio = studios[typed].title;
x.studioID = user
});
2024-09-18 05:02:20 -04:00
} else if (typed == '@' && type == '#user') {
extraFetch = await (fetch(`https://hf.zenoverse.net/user/${user}`)
.then(x => x.json()));
extraFetch.forEach(x => x.username = user);
}
2024-09-18 01:31:24 -04:00
entries = [...entries, ...extraFetch];
}
entries = entries.sort((a, b) => b.id - a.id);
let params = new URL(window.location).searchParams;
let start = params.get('start') * 1;
let end = params.get('end') * 1;
end = Math.min(end, entries.length - 2);
if (start && end) {
entries = entries.splice(entries.length - end - 2, end - start + 1);
}
doThings = true;
setInterval(function () {
2024-09-18 05:02:20 -04:00
if (!doThings || (window.innerHeight + window.scrollY) < document.body.scrollHeight - 25) return;
if (type == '#user') scroller();
if (type == '#comment') commenter();
2024-09-18 01:31:24 -04:00
}, 500);
}
function sanitize(content) {
const decoder = document.createElement('div');
decoder.textContent = content;
return decoder.innerHTML;
}
2024-09-18 05:02:20 -04:00
function commenter() {
let h = '';
area.className = 'nogrid';
for (let j = 0; j < 10; j++) {
let entry = entries[i];
if (!entry) continue;
// ugly tbf
h += `<div class='commenter'>
<img src='https://uploads.scratch.mit.edu/get_image/user/${entry.author.id}_999x999.png' class='user' float='left'>
<div>
2024-09-18 05:05:47 -04:00
<div><b><a href='https://scratch.mit.edu/users/${entry.author.username}'>${entry.author.username}</a></b></div>
<sub>In <a href='https://scratch.mit.edu/studios/${entry.studioID}'>${entry.studio}</a></sub>
2024-09-18 05:02:20 -04:00
<pre>${entry.content}</pre>
<sub>${entry.datetime_created}</sub>
</div>
</div>
`
i++;
}
area.innerHTML += h;
}
2024-09-18 01:31:24 -04:00
function scroller() {
let h = '';
for (let j = 0; j < 35; j++) {
let entry = entries[i];
if (!entry) continue;
2024-09-18 05:02:20 -04:00
// ugly tbf
h += `<div class='proj'>
<a href="https://scratch.mit.edu/projects/${entry.id}">
<img src='https://uploads.scratch.mit.edu/get_image/project/${entry.id}_1920x1080.png'>
${entry.title}
</a>
<sub>
by ${entry.username}
</sub>
<sub>
${entry.studio ? ' in ' + entry.studio : ''}
</sub>
</div>`;
2024-09-18 01:31:24 -04:00
i++;
}
area.innerHTML += h;
}
let sub = document.querySelector('#users');
2024-09-18 05:02:20 -04:00
let srch = new URLSearchParams(window.location.search)
let params = srch.getAll('users');
2024-09-18 01:31:24 -04:00
let val = params.join('\n') || sub.value;
sub.textContent = sub.value = val;
2024-09-18 05:02:20 -04:00
main(window.location.hash || '#user');
document.querySelector('#submit').onclick = () => main(window.location.hash || '#user');
2024-09-18 05:08:14 -04:00
document.querySelectorAll('.buttons button').forEach(x => x.onclick = function () {
window.setInterval(function () {
2024-09-18 05:02:20 -04:00
window.location.reload();
2024-09-18 05:08:14 -04:00
}, 100);
2024-09-18 05:02:20 -04:00
})