jsonp attempts
This commit is contained in:
80
non_jsonp/index.html
Normal file
80
non_jsonp/index.html
Normal file
@@ -0,0 +1,80 @@
|
||||
|
||||
<!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",
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35",
|
||||
"Mozilla/5.0 (Windows NT 6.1; rv:109.0) Gecko/20100101 Firefox/113.0",
|
||||
"Mozilla/5.0 (Android 12; Mobile; rv:109.0) Gecko/113.0 Firefox/113.0",
|
||||
"mozilla/5.0 (macintosh; intel mac os x 10.15; rv:109.0) gecko/20100101 firefox/113.0",
|
||||
];
|
||||
|
||||
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 url = archiveUrl + '/.pgn';
|
||||
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>
|
Reference in New Issue
Block a user