Update main.js.download

This commit is contained in:
Adil Sadqi 2024-04-06 23:56:06 +00:00 committed by GitHub
parent 59e1ea966b
commit dff2b50b09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 15 deletions

View File

@ -198,32 +198,32 @@ function commander(cmd) {
}); });
break; break;
case "speedtest": case "speedtest":
// Function to perform speed test // Function to perform speed test using Fast.com API
function performSpeedTest(callback) { function performSpeedTest(callback) {
// Using a free service to perform speed test // Using Fast.com API to perform speed test
fetch('https://www.speedtest.net/api/js/speedtest-cdn-mini.php') fetch('https://fast.com/download')
.then(response => { .then(response => {
if (!response.ok) { if (!response.ok) {
throw new Error('Network response was not ok'); throw new Error('Network response was not ok');
} }
return response.json(); return response.text();
}) })
.then(data => { .then(html => {
// Extracting download and upload speeds from the response // Parsing HTML response to extract speed data
const downloadSpeed = data.download; const parser = new DOMParser();
const uploadSpeed = data.upload; const doc = parser.parseFromString(html, 'text/html');
callback({ download: downloadSpeed, upload: uploadSpeed }); const speed = doc.querySelector('.speed-results-container-speed').textContent.trim();
callback({ speed: speed });
}) })
.catch(error => callback({ error: "Unable to perform speed test. " + error.message })); .catch(error => callback({ error: "Unable to perform speed test. " + error.message }));
} }
// Displaying the download and upload speeds // Displaying the download speed
performSpeedTest(function(speeds) { performSpeedTest(function(speed) {
if (speeds.error) { if (speed.error) {
addLine("Error: " + speeds.error, "color2", 80); addLine("Error: " + speed.error, "color2", 80);
} else { } else {
addLine("Download Speed: " + speeds.download + " Mbps", "color2", 80); addLine("Download Speed: " + speed.speed + " Mbps", "color2", 80);
addLine("Upload Speed: " + speeds.upload + " Mbps", "color2", 80);
} }
}); });
break; break;