Please help (dev console)
Archived 10 months ago
L
Nyx ♱
Copy Paster!
need some help with javascript since im really bad with it, and gpt can't help me either, i'm trying to download all pdf files of example.com/ebooks/x.pdf (where x is a random file name), and send it to a discord webhook. i have full access to the website, and so would any other person, its free to view by anyone, i just want to automize it so i don't have to do it myself, only thing is i don't really know javascript that well, can someone help?
gpt gave this:
```js
// Your Discord webhook URL
const webhookURL = 'https://discord.com/api/webhooks/your-webhook-url';
// Base URL where PDFs are stored
const baseURL = 'https://example.com/ebooks/';
// Function to download and send PDFs
async function downloadAndSendEbooks() {
console.log(`Starting to fetch PDF filenames from ${baseURL}`);
try {
// Fetch the directory listing page
const response = await fetch(baseURL);
const body = await response.text();
// Parse the HTML to find PDF links
const parser = new DOMParser();
const doc = parser.parseFromString(body, 'text/html');
const links = Array.from(doc.querySelectorAll('a'));
const pdfLinks = links
.map(link => link.href)
.filter(href => href.endsWith('.pdf'));
console.log(`Found ${pdfLinks.length} PDF files.`);
// Iterate over the found PDF links and send them to Discord
for (const pdfUrl of pdfLinks) {
console.log(`Attempting to fetch PDF: ${pdfUrl}`);
// Fetch the PDF file
const pdfResponse = await fetch(pdfUrl);
// If the PDF doesn't exist (404), skip it
if (pdfResponse.status === 404) {
console.log(`PDF ${pdfUrl} not found, skipping.`);
continue;
}
console.log(`Successfully fetched ${pdfUrl}`);
const blob = await pdfResponse.blob();
// Create a FormData object for sending the file to Discord
const formData = new FormData();
formData.append('file', blob, pdfUrl.substring(pdfUrl.lastIndexOf('/') + 1)); // Get just the file name
// Send the PDF to the Discord webhook
const discordResponse = await fetch(webhookURL, {
method: 'POST',
body: formData
});
if (!discordResponse.ok) {
const errorText = await discordResponse.text();
throw new Error(`Failed to send to Discord: ${pdfUrl}, Status: ${discordResponse.status}, Response: ${errorText}`);
}
console.log(`PDF ${pdfUrl} sent to Discord`);
// Adding a delay to avoid sending too quickly
await new Promise(resolve => setTimeout(resolve, 3000)); // 3-second delay
}
} catch (error) {
console.error(`Error during the process: `, error);
}
}
// Run the function in the console
downloadAndSendEbooks();```
i want this to be runable in the chrome console
