1
Fork 0
mirror of https://github.com/Steffo99/tidebringer-firefox.git synced 2025-01-06 22:19:46 +00:00
tidebringer-firefox/extension/add_items_to_collection.js

65 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2020-01-17 00:42:13 +00:00
async function addItemToCollection(itemElement) {
const collectionId = document.querySelector(`#AddChildItemForm [name="id"]`).value;
const sessionId = document.querySelector(`#AddChildItemForm [name="sessionid"]`).value;
const itemId = /choice_\w+_([0-9]+)/.exec(itemElement.id)[1];
2022-07-22 04:09:53 +00:00
2020-01-17 00:42:13 +00:00
const formdata = new FormData();
formdata.append("id", collectionId);
formdata.append("sessionid", sessionId);
formdata.append("childid", itemId);
//This endpoint has no rate limits?!
let response = await fetch("https://steamcommunity.com/sharedfiles/addchild", {
"method": "POST",
"body": formdata
})
2022-07-22 04:09:53 +00:00
if (response.status === 200) {
2020-01-17 00:42:13 +00:00
itemElement.className = itemElement.className + " inCollection";
}
}
async function addAllSubscribedToCollection() {
const buttonElement = document.querySelector("#tidebringer-btn-add-all-subscribed");
buttonElement.removeEventListener("click", addAllSubscribedToCollection)
2020-01-17 00:42:13 +00:00
buttonElement.className += " tidebringer-running";
buttonElement.setAttribute("disabled", "true")
2020-01-17 00:42:13 +00:00
const itemElements = document.querySelectorAll(".itemChoice");
2022-07-22 04:09:53 +00:00
for (let i = 0; i < itemElements.length; i++) {
2020-01-17 00:42:13 +00:00
let element = itemElements[i];
const match_sub = /MySubscribedItems/.exec(element.id);
2022-07-22 04:09:53 +00:00
if (match_sub === null) {
2020-01-17 00:42:13 +00:00
continue;
}
const match_class = /inCollection/.exec(element.className);
2022-07-22 04:09:53 +00:00
if (match_class !== null) {
2020-01-17 00:42:13 +00:00
continue;
}
await addItemToCollection(element);
await new Promise(r => setTimeout(r, 100));
}
}
function tidebringer() {
const whereTheContainerShouldBePlaced = document.querySelector(".collectionAddItemsSection");
if(!whereTheContainerShouldBePlaced) return
console.log("%c Tidebringer %chttps://github.com/Steffo99/tidebringer-firefox", "background-color: black; color: #a0f3f6;");
const container = document.createElement("div")
container.id = "tidebringer-container"
whereTheContainerShouldBePlaced.appendChild(container)
2020-01-17 00:42:13 +00:00
const addAllSubscribedButton = document.createElement("button");
addAllSubscribedButton.className = "btn_darkblue_white_innerfade btn_medium"
addAllSubscribedButton.addEventListener("click", addAllSubscribedToCollection);
addAllSubscribedButton.id = "tidebringer-btn-add-all-subscribed";
2020-01-17 00:42:13 +00:00
const addAllSubscribedSpan = document.createElement("span");
const addAllSubscribedText = document.createTextNode("Add all subscribed elements");
addAllSubscribedSpan.appendChild(addAllSubscribedText);
addAllSubscribedButton.appendChild(addAllSubscribedSpan);
container.appendChild(addAllSubscribedButton);
2020-01-17 00:42:13 +00:00
}
tidebringer();