elia-li-website/assets/api.js

104 lines
3.9 KiB
JavaScript
Raw Normal View History

2024-06-09 03:44:03 +02:00
document.addEventListener('contextmenu', function(event) {
event.preventDefault();
});
async function fetchTemperature() {
const response = await fetch('https://language.apis.zhr1.infra.elia.network/v2/infra/temperature');
2024-06-09 03:44:03 +02:00
return response.text();
}
async function fetchAndDisplay() {
const responseDiv = document.querySelector('.response');
const statsDiv = document.querySelector('.stats');
let buffer = "";
let charCount = 0;
let startTime = performance.now();
let initialText = responseDiv.textContent;
2024-06-09 13:16:46 +02:00
const eraseDelay = 100;
const printDelay = 50;
const statsEraseDelay = 15;
const statsPrintDelay = 30;
2024-06-09 03:44:03 +02:00
let lastTemperature = "...";
let lastTemperatureUpdateTime = 0;
const response = await fetch('https://language.apis.zhr1.infra.elia.network/v2/text/generate');
2024-06-09 03:44:03 +02:00
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
while (initialText.length > 0) {
initialText = initialText.slice(0, -1);
responseDiv.textContent = initialText;
2024-06-09 13:16:46 +02:00
await new Promise(resolve => setTimeout(resolve, eraseDelay));
2024-06-09 03:44:03 +02:00
}
async function updateStatsAndTemperature() {
statsDiv.style.display = 'block';
2024-06-09 03:44:03 +02:00
let currentTime = (performance.now() - startTime) / 1000;
let charsPerSecond = charCount / currentTime;
2024-06-09 13:16:46 +02:00
if (currentTime - lastTemperatureUpdateTime >= 3) {
2024-06-09 03:44:03 +02:00
try {
lastTemperature = await fetchTemperature();
lastTemperatureUpdateTime = currentTime;
} catch (error) {
lastTemperature = "...";
2024-06-09 03:44:03 +02:00
}
}
statsDiv.textContent = `${charsPerSecond.toFixed(1)} chars/s, ${charCount} chars, ${currentTime.toFixed(1)}s, Temp: ${lastTemperature}°C`;
}
async function appendTextSmoothly(textPart) {
buffer += textPart;
while (buffer.length > 0) {
let pos = buffer.indexOf('**');
if (pos === 0) {
let endPos = buffer.indexOf('**', 2);
if (endPos !== -1) {
responseDiv.innerHTML += '<b>' + buffer.substring(2, endPos) + '</b>';
buffer = buffer.slice(endPos + 2);
} else {
break;
}
} else {
let nextPos = pos !== -1 ? pos : buffer.length;
for (let i = 0; i < nextPos; i++) {
responseDiv.innerHTML += buffer[i];
2024-06-09 13:16:46 +02:00
await new Promise(resolve => setTimeout(resolve, printDelay));
2024-06-09 03:44:03 +02:00
charCount++;
2024-06-09 13:16:46 +02:00
await updateStatsAndTemperature();
2024-06-09 03:44:03 +02:00
}
buffer = buffer.slice(nextPos);
}
}
}
while (true) {
const { value, done } = await reader.read();
if (done) {
if (buffer) {
responseDiv.innerHTML += buffer;
}
break;
}
const text = decoder.decode(value, { stream: true });
await appendTextSmoothly(text);
}
let statsText = statsDiv.textContent;
while (statsText.length > 0) {
statsText = statsText.slice(0, -1);
statsDiv.textContent = statsText;
2024-06-09 13:16:46 +02:00
await new Promise(resolve => setTimeout(resolve, statsEraseDelay));
2024-06-09 03:44:03 +02:00
}
await new Promise(resolve => setTimeout(resolve, 200));
const message = '// credit to hartnett media \\\\';
2024-06-09 03:44:03 +02:00
let messageIndex = 0;
while (messageIndex < message.length) {
statsDiv.textContent += message[messageIndex];
messageIndex++;
2024-06-09 13:16:46 +02:00
await new Promise(resolve => setTimeout(resolve, statsPrintDelay));
2024-06-09 03:44:03 +02:00
}
}
document.addEventListener('DOMContentLoaded', fetchAndDisplay);
console.log(`      ___
    
    | _ _|
    / __x
  /     |
   /    
  | | |    
|  | | |  
| (_ヽ_)_) 
二つ`);
console.log("hello");