Skip to content

Instantly share code, notes, and snippets.

@a-gu
Created December 22, 2023 19:26
Show Gist options
  • Save a-gu/c51428c640b531bfea3e9ecac22bd1e7 to your computer and use it in GitHub Desktop.
Save a-gu/c51428c640b531bfea3e9ecac22bd1e7 to your computer and use it in GitHub Desktop.
Claim all items in an itch.io bundle
(async function () {
if (!/\/bundle\/download\/.+/.test(location.pathname))
return alert("Not on a supported page")
const baseURL = `${location.origin}${location.pathname}`,
baseDOM = new DOMParser().parseFromString(
await fetch(baseURL).then((res) => res.text()),
"text/html"
),
maxPage = Math.max(
...Array.from(baseDOM.querySelectorAll(".pager a[href]")).map((x) =>
Number.parseInt(
new URL(x?.href ?? "a://")?.searchParams?.get("page") ?? ""
)
)
)
for (let pageNum = 1; pageNum <= maxPage; pageNum++) {
let pageDOM = new DOMParser().parseFromString(
await fetch(`${baseURL}?page=${pageNum}`).then((res) => res.text()),
"text/html"
),
games = pageDOM.querySelectorAll(".game_row")
for (let game of games) {
let form = game.querySelector('form:has(button[value="claim"])'),
formData = new FormData(),
gameTitle = game?.querySelector(".game_title")?.innerText?.trim() ?? ""
if (!form) {
console.debug(`Skipping item (in library or unclaimable): ${gameTitle}`)
continue
}
form
.querySelectorAll(":scope>*[name][value]")
.forEach((e) =>
formData.append(
e?.getAttribute("name") ?? "",
e?.getAttribute("value") ?? ""
)
)
console.debug(`Adding item to library: ${gameTitle}`)
fetch(baseURL, {
method: form?.getAttribute("method") ?? "post",
body: formData,
})
}
}
})()
!async function(){if(!/\/bundle\/download\/.+/.test(location.pathname))return alert("Not on a supported page");const e=`${location.origin}${location.pathname}`,t=(new DOMParser).parseFromString(await fetch(e).then((e=>e.text())),"text/html"),r=Math.max(...Array.from(t.querySelectorAll(".pager a[href]")).map((e=>Number.parseInt(new URL(e?.href??"a://")?.searchParams?.get("page")??""))));for(let t=1;t<=r;t++){let r=(new DOMParser).parseFromString(await fetch(`${e}?page=${t}`).then((e=>e.text())),"text/html").querySelectorAll(".game_row");for(let t of r){let r=t.querySelector('form:has(button[value="claim"])'),a=new FormData,o=t?.querySelector(".game_title")?.innerText?.trim()??"";r?(r.querySelectorAll(":scope>*[name][value]").forEach((e=>a.append(e?.getAttribute("name")??"",e?.getAttribute("value")??""))),console.debug(`Adding item to library: ${o}`),fetch(e,{method:r?.getAttribute("method")??"post",body:a})):console.debug(`Skipping item (in library or unclaimable): ${o}`)}}}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment