1
0
Fork 0
Armyfroge_Cards_Userscript/main.user.js

98 lines
3 KiB
JavaScript
Raw Normal View History

2024-07-28 08:38:31 +00:00
// ==UserScript==
// @name Armyfroge Cards
// @namespace https://git.euph.dev/Snoweuph
// @match https://army-forge.onepagerules.com/view*
// @grant none
2024-07-28 10:22:17 +00:00
// @version 1.0.8
2024-07-28 08:38:31 +00:00
// @author Snoweuph
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @require https://html2canvas.hertzen.com/dist/html2canvas.min.js
// ==/UserScript==
// Bind to Ctrl + S
document.addEventListener("keydown", (event) => {
if (event.ctrlKey && event.key === "s") {
event.preventDefault();
2024-07-28 09:24:47 +00:00
generateCards();
generateRules();
2024-07-28 08:38:31 +00:00
}
});
2024-07-28 09:24:47 +00:00
function generateCards() {
2024-07-28 08:38:31 +00:00
const cards_root =
document.getElementsByClassName("MuiContainer-root")[0].firstElementChild;
Array.from(cards_root.children).forEach((child) => {
const card = child.firstElementChild;
const header = card.firstElementChild;
2024-07-28 09:49:18 +00:00
const title = getHeaderTitle(header);
const unit_count = getUnitCount(title);
2024-07-28 08:38:31 +00:00
const marker_section = document.createElement("div");
2024-07-28 09:47:06 +00:00
marker_section.style.display = "grid";
marker_section.style.gridTemplateColumns = "repeat(10, 1fr)";
2024-07-28 08:38:31 +00:00
marker_section.style.gap = "0.5rem";
marker_section.style.padding = "0.5rem";
marker_section.style.marginBottom = "0.5rem";
2024-07-28 09:47:06 +00:00
for (let i = 0; i < 10 * unit_count; i++) {
2024-07-28 08:38:31 +00:00
const maker_space = document.createElement("div");
maker_space.style.width = "3rem";
maker_space.style.aspectRatio = "1";
maker_space.style.borderRadius = "100%";
maker_space.style.border = "dashed 1px lightgray";
marker_section.appendChild(maker_space);
}
card.insertBefore(marker_section, header.nextSibling);
html2canvas(card, { scale: 15, backgroundColor: null }).then((canvas) => {
const link = document.createElement("a");
2024-07-28 09:48:27 +00:00
link.download = title + ".png";
2024-07-28 08:38:31 +00:00
link.href = canvas.toDataURL();
document.body.appendChild(link);
link.click();
});
});
}
2024-07-28 09:24:47 +00:00
function generateRules() {
2024-07-28 09:49:18 +00:00
const rules_root =
2024-07-28 10:22:17 +00:00
document.getElementsByClassName("MuiContainer-root")[0].lastElementChild
.firstElementChild.firstElementChild.firstElementChild;
2024-07-28 09:31:33 +00:00
2024-07-28 09:53:28 +00:00
const rules_list =
2024-07-28 10:22:17 +00:00
rules_root.firstElementChild.lastElementChild.firstElementChild
.firstElementChild.firstElementChild.firstElementChild.firstElementChild;
2024-07-28 09:53:06 +00:00
rules_list.style.display = "flex";
rules_list.style.flexDirection = "column";
rules_list.style.gap = "1rem";
2024-07-28 09:24:47 +00:00
2024-07-28 11:19:12 +00:00
html2canvas(rules_root, { scale: 8, backgroundColor: null }).then(
2024-07-28 09:49:18 +00:00
(canvas) => {
const link = document.createElement("a");
link.download = "Rules.png";
link.href = canvas.toDataURL();
document.body.appendChild(link);
link.click();
},
);
2024-07-28 09:24:47 +00:00
}
2024-07-28 09:47:06 +00:00
function getUnitCount(str) {
2024-07-28 09:49:18 +00:00
const match = str.match(/^\d+/);
return match ? parseInt(match[0], 10) : 1;
2024-07-28 09:47:06 +00:00
}
2024-07-28 09:24:47 +00:00
2024-07-28 08:38:31 +00:00
function getHeaderTitle(pElement) {
let textContent = "";
pElement.firstElementChild.firstElementChild.firstElementChild.childNodes.forEach(
(node) => {
if (node.nodeType === Node.TEXT_NODE) {
textContent += node.textContent;
}
2024-07-28 08:40:50 +00:00
},
2024-07-28 08:38:31 +00:00
);
return textContent;
}