Back

Version 20

5/29/2024
import cheerio from "https://esm.sh/cheerio@1.0.0-rc.10";
import { fetch } from "https://esm.town/v/std/fetch";

// Helper function to retrieve CSRF token if needed
async function getCsrfToken(url, cookies) {
try {
console.log("Fetching CSRF Token from URL:", url);
console.log("Using Cookies:", cookies);
const response = await fetch(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6422.113 Safari/537.36",
"Referer": "https://forums.spacebattles.com/",
"Accept-Language": "en-US,en;q=0.9",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Cookie": cookies,
},
});

console.log(`HTTP Response Status (CSRF Token Request): ${response.status}`);

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const html = await response.text();
console.log("Fetched HTML (CSRF Token Request):", html);

const $ = cheerio.load(html);
const csrfToken = $("meta[name=\"csrf-token\"]").attr("content");
console.log("Extracted CSRF Token:", csrfToken);
return csrfToken || "";
} catch (error) {
console.error("Error fetching CSRF token:", error);
return "";
}
}
Updated: May 29, 2024