frictionless-browser/js/app.js

62 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-01-02 17:21:44 -05:00
let area = document.querySelector('#area-main');
let entries = [];
let users = {};
let fetchData;
async function genTree(treeId) {
2024-08-02 03:42:44 -04:00
fetchData = fetchData || await fetch(`https://scratch.mit.edu/projects/${treeId}/remixtree/bare/`)
.then(x => x.json());
2024-01-02 17:21:44 -05:00
entries.push(treeId)
let entry = fetchData[treeId];
while (entry) {
entry = entry.parent_id;
entries.push(entry);
2024-08-02 03:42:44 -04:00
let entryDat = fetchData[entry];
if (!entryDat) continue;
let us = users[entryDat.username];
users[entryDat.username] = (us ? us : 0) + 1 / entries.length;
entry = entryDat;
2024-01-02 17:21:44 -05:00
}
return entry;
}
2024-01-03 09:43:38 -05:00
let doThings = false;
2024-01-02 17:21:44 -05:00
async function main() {
let latestData = await fetch("https://corsproxy.io/?https://api.scratch.mit.edu/studios/34493018/projects").then(x => x.json());
2024-07-25 14:28:47 -04:00
await genTree(latestData[0].id); // latest project
2024-01-02 17:21:44 -05:00
await genTree("654605857"); // tree bug here, unavoidable without jank
2024-01-03 09:43:38 -05:00
doThings = true;
2024-08-02 03:42:44 -04:00
document.querySelector('.contributors').innerHTML = Object.keys(users)
.sort((x, y) => users[y] - users[x])
.map(x => `<a href='https://scratch.mit.edu/users/${x}'>${x}</a> <sub>${Math.trunc(users[x] / Math.log(entries.length) * 100000) / 1000}%</sub>`)
.join(', '); // Doesn't need to be sanitized (hopefully)
2024-01-02 17:21:44 -05:00
}
main();
let i = 0;
function sanitize(content) {
const decoder = document.createElement('div');
decoder.textContent = content;
return decoder.innerHTML;
}
2024-08-02 03:42:44 -04:00
setInterval(function () {
2024-01-03 09:43:38 -05:00
if (doThings && (window.innerHeight + window.scrollY) >= document.body.scrollHeight - 25) {
2024-08-02 03:42:44 -04:00
let h = '';
2024-01-02 17:21:44 -05:00
for (let j = 0; j < 35; j++) {
let entry = entries[i];
2024-01-03 09:43:38 -05:00
if (!entry || !(entry in fetchData)) break;
2024-08-02 03:42:44 -04:00
h += `<div class='proj'><a href="https://scratch.mit.edu/projects/${entry}"><img src='https://uploads.scratch.mit.edu/get_image/project/${entry}_1920x1080.png'>${sanitize(fetchData[entry].title)}</a> <sub>#${entries.length - i} by ${fetchData[entry].username}</sub></div>`;
2024-01-02 17:21:44 -05:00
i++;
}
area.innerHTML += h;
}
2024-08-02 03:42:44 -04:00
}, 500);