chesstok-lookup/non_jsonp/=
2023-11-25 11:44:31 -07:00

77 lines
2.9 KiB
Plaintext

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chess.com API Request</title>
</head>
<body>
<label for="username">Enter Chess.com Username:</label>
<input type="text" id="username" placeholder="Enter username">
<button onclick="makeRequest()">Fetch Games</button>
<script>
const userAgents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35",
// Add more user agents as needed
];
let jsonData;
function getRandomUserAgent() {
return userAgents[Math.floor(Math.random() * userAgents.length)];
}
async function makeRequest() {
try {
const username = document.getElementById('username').value;
const randomUserAgent = getRandomUserAgent();
const url = `https://api.chess.com/pub/player/${username}/games/archives`;
const response = await fetch(url, {
headers: {
'User-Agent': randomUserAgent,
},
});
jsonData = await response.json();
// Process the URLs in the archives array
if (jsonData && jsonData.archives && Array.isArray(jsonData.archives)) {
const promises = jsonData.archives.map(function(url) {
return makeAsyncRequest(url, randomUserAgent);
});
// Use Promise.all to wait for all requests to complete
const results = await Promise.all(promises);
console.log("All requests completed:", results);
// Perform further processing with the results
const concatenatedContent = results.map(data => data.pgn).join('\n');
console.log("Concatenated PGN:", concatenatedContent);
}
} catch (error) {
console.error("Error in one or more requests:", error);
}
}
async function makeAsyncRequest(url, userAgent) {
try {
const asyncUrl = `${url}?user_agent=${encodeURIComponent(userAgent)}`;
const response = await fetch(asyncUrl);
const data = await response.json();
console.log("Async Response:", data);
return data;
} catch (error) {
console.error("Error in async request:", error);
throw error; // Rethrow the error to propagate it to the Promise.all catch block
}
}
</script>
</body>
</html>