Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cameronmoreau/aae8603ce1bbb4cb7c22b336afe888d1 to your computer and use it in GitHub Desktop.
Save cameronmoreau/aae8603ce1bbb4cb7c22b336afe888d1 to your computer and use it in GitHub Desktop.
Break khanhs website
// Save the original fetch function
const originalFetch = window.fetch;
class TextQueue {
constructor() {
this.queue = [];
this.isProcessing = false;
}
// Add items to the queue
add(text) {
this.queue.push(text);
// Start processing if not already running
if (!this.isProcessing) {
this.startProcessing();
}
}
// Add multiple items at once
addBatch(textArray) {
textArray.forEach(text => this.queue.push(text));
// Start processing if not already running
if (!this.isProcessing) {
this.startProcessing();
}
}
// Process the queue at 2-second intervals
startProcessing() {
this.isProcessing = true;
const processNext = () => {
if (this.queue.length > 0) {
const text = this.queue.shift();
console.log(`Processing: ${text}`);
// Call the clickButtonWithText function
this.clickButtonWithText(text);
// Schedule the next item after 2 seconds
setTimeout(processNext, 500);
} else {
// Queue is empty, stop processing
this.isProcessing = false;
}
};
// Start the processing cycle
processNext();
}
// The method that will be called for each queued item
clickButtonWithText(buttonText) {
// Find all button elements
const buttons = document.querySelectorAll('button');
// Loop through buttons to find one with matching text
for (const button of buttons) {
// Check if the button's text content includes the search text
if (button.textContent.trim() === buttonText) {
console.log(`Found button with text "${buttonText}". Clicking it...`);
// Click the button
button.click();
return true;
}
}
// If we reach here, no button was found
console.warn(`No button found with text "${buttonText}"`);
return false;
}
}
// Usage example
const queue = new TextQueue();
// Override the fetch function
window.fetch = async function(...args) {
// Check if this is a request to the target endpoint
if (args[0] && (args[0] === "/api/game" || (typeof args[0] === "object" && args[0].url === "/api/game"))) {
console.log("Detected request to /api/game", args);
try {
// Call the original fetch and get the response
const response = await originalFetch.apply(this, args);
// Clone the response to avoid consuming it
const clonedResponse = response.clone();
// Process the JSON response
clonedResponse.json().then(data => {
console.log("Response from /api/game:", data);
// foo(data.correctCourse.courseName);
queue.add("");
queue.add(data.correctCourse.courseName);
queue.add("Next Question");
}).catch(err => {
console.log("Could not parse JSON response:", err);
// Try to get text if JSON parsing fails
clonedResponse.text().then(text => {
console.log("Response as text:", text);
});
});
// Return the original response so the application works normally
return response;
} catch (error) {
console.error("Error intercepting /api/game request:", error);
throw error;
}
}
// For all other requests, just use the original fetch
return originalFetch.apply(this, args);
};
console.log("Fetch monitoring for /api/game is now active!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment