working jsonp but with no invalid username warning

This commit is contained in:
spbeach46
2023-11-26 09:59:01 -07:00
parent 3462cd3101
commit b4df03c1f5
5 changed files with 294 additions and 92 deletions

90
gpt/MAIN.js Normal file
View File

@@ -0,0 +1,90 @@
// main.js
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",
];
function getRandomUserAgent() {
return userAgents[Math.floor(Math.random() * userAgents.length)];
}
function makeRequest() {
var username = document.getElementById('username').value;
var randomUserAgent = getRandomUserAgent();
var url = `https://api.chess.com/pub/player/${username}/games/archives?callback=jsonpCallback&user_agent=${encodeURIComponent(randomUserAgent)}`;
var script = document.createElement('script');
script.src = url;
window.jsonpCallback = function(data) {
// Process the URLs in the archives array
if (data && data.archives && Array.isArray(data.archives)) {
const promises = data.archives.map(function(archiveUrl) {
// Append '/.pgn' to each individual URL
const url = archiveUrl;
return makeAsyncRequest(url, getRandomUserAgent());
});
// Use Promise.all to wait for all requests to complete
Promise.all(promises)
.then(function(results) {
// Concatenate the 'pgn' values
const concatenatedPgn = results.flatMap(data => data.games.map(game => game.pgn)).join('\n');
// Use the concatenatedPgn variable as needed
console.log("Concatenated PGN:", concatenatedPgn);
})
.catch(function(error) {
console.error("Error in one or more requests:", error);
});
}
// Clean up the script element
document.head.removeChild(script);
};
document.head.appendChild(script);
}
function makeAsyncRequest(url, userAgent) {
return new Promise(function (resolve, reject) {
// Generate a unique callback function name
const callbackName = `jsonpCallback_${Date.now()}_${Math.floor(Math.random() * 1000)}`;
// Create a script element
const script = document.createElement('script');
// Define the callback function
window[callbackName] = function (data) {
// Clean up the script and the callback function
document.head.removeChild(script);
delete window[callbackName];
// Resolve the promise with the response data
resolve(data);
};
// Construct the URL with the callback parameter
const callbackUrl = `${url}?callback=${callbackName}&user_agent=${encodeURIComponent(userAgent)}`;
script.src = callbackUrl;
// Append the script element to the document
document.head.appendChild(script);
// Handle errors
script.onerror = function (error) {
// Clean up the script and the callback function
document.head.removeChild(script);
delete window[callbackName];
// Reject the promise with the error
reject(error);
};
});
}

100
gpt/fuck.js Normal file
View File

@@ -0,0 +1,100 @@
// main.js
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",
];
function getRandomUserAgent() {
return userAgents[Math.floor(Math.random() * userAgents.length)];
}
function makeRequest() {
var username = document.getElementById('username').value;
// Check if the username is blank
if (!username.trim()) {
alert("Please enter a username.");
return;
}
var randomUserAgent = getRandomUserAgent();
var url = `https://api.chess.com/pub/player/${username}/games/archives?callback=jsonpCallback&user_agent=${encodeURIComponent(randomUserAgent)}`;
var script = document.createElement('script');
script.src = url;
window.jsonpCallback = function(data) {
if (data && data.error && data.error.code === 0) {
alert(`Invalid username. Please check your username and try again.`);
} else if (data && data.archives && Array.isArray(data.archives)) {
if (data.archives.length === 0) {
alert(`No games found for the provided username.`);
} else {
const promises = data.archives.map(function(archiveUrl) {
// Append '/.pgn' to each individual URL
const url = archiveUrl;
return makeAsyncRequest(url, getRandomUserAgent());
});
// Use Promise.all to wait for all requests to complete
Promise.all(promises)
.then(function(results) {
// Concatenate the 'pgn' values
const concatenatedPgn = results.flatMap(data => data.games.map(game => game.pgn)).join('\n');
// Trigger download
downloadFile(username, concatenatedPgn);
})
.catch(function(error) {
console.error("Error in one or more requests:", error);
alert("An error occurred. Please try again.");
});
}
} else {
alert("Error retrieving games data. Please check your username and try again.");
}
// Clean up the script element
document.head.removeChild(script);
};
document.head.appendChild(script);
}
// Function to trigger download
function downloadFile(username, content) {
const blob = new Blob([content], { type: 'text/plain' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = `${username}.pgn`;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(a.href);
}
// Function to make asynchronous requests
function makeAsyncRequest(url, userAgent) {
return new Promise(function(resolve, reject) {
const callbackName = `jsonpCallback_${Date.now()}_${Math.floor(Math.random() * 1000)}`;
const script = document.createElement('script');
window[callbackName] = function(data) {
document.head.removeChild(script);
delete window[callbackName];
resolve(data);
};
const callbackUrl = `${url}?callback=${callbackName}&user_agent=${encodeURIComponent(userAgent)}`;
script.src = callbackUrl;
document.head.appendChild(script);
script.onerror = function(error) {
document.head.removeChild(script);
delete window[callbackName];
reject(error);
};
});
}

View File

@@ -1,16 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chess Game Downloader</title>
<title>Chess.com API Request</title>
</head>
<body>
<h1>Chess Game Downloader</h1>
<input type="text" id="username" placeholder="Enter username">
<button onclick="downloadGames()">Download Games</button>
<script src="main.js"></script>
<label for="username">Enter Chess.com Username:</label>
<input type="text" id="username" placeholder="Enter username">
<button onclick="makeRequest()">Fetch Games</button>
<script src="./main.js"></script>
</body>
</html>

View File

@@ -1,94 +1,103 @@
// main.js
function jsonp(url, callbackName) {
return new Promise((resolve) => {
const script = document.createElement('script');
script.src = `${url}&callback=${callbackName}`;
document.body.appendChild(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",
];
window[callbackName] = (data) => {
function getRandomUserAgent() {
return userAgents[Math.floor(Math.random() * userAgents.length)];
}
function makeRequest() {
var username = document.getElementById('username').value;
// Check if the username is blank
if (!username.trim()) {
// If the username is blank, do nothing
return;
}
var randomUserAgent = getRandomUserAgent();
var url = `https://api.chess.com/pub/player/${username}/games/archives?callback=jsonpCallback&user_agent=${encodeURIComponent(randomUserAgent)}`;
var script = document.createElement('script');
script.src = url;
window.jsonpCallback = function(data) {
// Check if the response has archives
if (data && data.archives && Array.isArray(data.archives)) {
if (data.archives.length === 0) {
alert("No games found for the provided username.");
// Optionally, you can provide a message or take other actions
} else {
const promises = data.archives.map(function(archiveUrl) {
// Append '/.pgn' to each individual URL
const url = archiveUrl;
return makeAsyncRequest(url, getRandomUserAgent());
});
// Use Promise.all to wait for all requests to complete
Promise.all(promises)
.then(function(results) {
// Concatenate the 'pgn' values
const concatenatedPgn = results.flatMap(data => data.games.map(game => game.pgn)).join('\n');
// Trigger download
downloadFile(username, concatenatedPgn);
})
.catch(function(error) {
console.error("Error in one or more requests:", error);
// Optionally, you can log errors, but no popup is shown
});
}
} else {
// Handle the case where there are no archives or an invalid response
alert("Invalid response from the Chess.com API. Please try again.");
// Optionally, you can provide a message or take other actions
}
// Clean up the script element
document.head.removeChild(script);
};
document.head.appendChild(script);
}
// Function to trigger download
function downloadFile(username, content) {
const blob = new Blob([content], { type: 'text/plain' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = `${username}.pgn`;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(a.href);
}
// Function to make asynchronous requests
function makeAsyncRequest(url, userAgent) {
return new Promise(function(resolve, reject) {
const callbackName = `jsonpCallback_${Date.now()}_${Math.floor(Math.random() * 1000)}`;
const script = document.createElement('script');
window[callbackName] = function(data) {
document.head.removeChild(script);
delete window[callbackName];
document.body.removeChild(script);
resolve(data);
};
const callbackUrl = `${url}?callback=${callbackName}&user_agent=${encodeURIComponent(userAgent)}`;
script.src = callbackUrl;
document.head.appendChild(script);
script.onerror = function(error) {
document.head.removeChild(script);
delete window[callbackName];
reject(error);
};
});
}
async function getGames(username, userAgents) {
const gamesUrl = `https://api.chess.com/pub/player/${username}/games/archives`;
try {
const response = await jsonp(gamesUrl);
// Extracting URLs
const gameUrls = response.archives.map(url => `${url}/.pgn`);
return gameUrls;
} catch (error) {
console.error('Error fetching game URLs:', error);
throw error;
}
}
async function fetchGames(url, userAgent) {
const callbackName = `jsonp_callback_${Math.round(100000 * Math.random())}`;
try {
const response = await jsonp(url, callbackName);
// Process the JSONP response as needed
return response;
} catch (error) {
console.error(`Error fetching game from ${url}:`, error);
throw error;
}
}
function concatenateGames(gameResults) {
// Customize how you want to concatenate the games
return gameResults.join('\n\n');
}
async function downloadGames() {
const username = document.getElementById('username').value;
// Function 1: Get game URLs
const allUserAgents = await loadUserAgents();
const urls = await getGames(username, allUserAgents);
// Function 2: Asynchronously fetch games
const gamePromises = urls.map(async (url, index) => {
const userAgent = allUserAgents[index % allUserAgents.length];
return await fetchGames(url, userAgent);
});
try {
// Function 3: Concatenate game results
const gameResults = await Promise.all(gamePromises);
const concatenatedGames = concatenateGames(gameResults);
// Function 4: Trigger download
download(concatenatedGames, 'chess_games.pgn', 'application/pgn');
} catch (error) {
console.error('Error fetching games:', error);
}
}
function download(content, fileName, contentType) {
const a = document.createElement('a');
const file = new Blob([content], { type: contentType });
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
async function loadUserAgents() {
try {
const userAgentsModule = await import('./userAgents.js');
return userAgentsModule.default;
} catch (error) {
console.error('Error loading user agents:', error);
throw error;
}
}
document.addEventListener('DOMContentLoaded', function () {
// Your initialization logic here...
});