update version to 1.2.0 and refactor button injection for Foundry V14 compatibility

This commit is contained in:
CPTN Cosmo 2026-04-21 19:21:14 +02:00
parent 281c3beb17
commit 1daa6f598e
2 changed files with 71 additions and 28 deletions

View file

@ -2,10 +2,10 @@
"id": "duality-roller", "id": "duality-roller",
"title": "Daggerheart Duality Dice Roller", "title": "Daggerheart Duality Dice Roller",
"description": "Adds a button next to the chat dice/controls that triggers the /dr command for the Foundryborne Daggerheart system.", "description": "Adds a button next to the chat dice/controls that triggers the /dr command for the Foundryborne Daggerheart system.",
"version": "1.1.6", "version": "1.2.0",
"compatibility": { "compatibility": {
"minimum": "13", "minimum": "13",
"verified": "13" "verified": "14"
}, },
"authors": [ "authors": [
{ {
@ -22,5 +22,5 @@
"languages": [], "languages": [],
"url": "https://git.geeks.gay/cosmo/DualityDiceRoller", "url": "https://git.geeks.gay/cosmo/DualityDiceRoller",
"manifest": "https://git.geeks.gay/cosmo/DualityDiceRoller/raw/branch/main/module.json", "manifest": "https://git.geeks.gay/cosmo/DualityDiceRoller/raw/branch/main/module.json",
"download": "https://git.geeks.gay/cosmo/DualityDiceRoller/releases/download/1.1.6/DualityDiceRoller.zip" "download": "https://git.geeks.gay/cosmo/DualityDiceRoller/releases/download/1.2.0/DualityDiceRoller.zip"
} }

View file

@ -8,46 +8,89 @@ Hooks.once("init", () => {
}); });
// Sidebar chat // Sidebar chat
Hooks.on("renderChatLog", (_app, _html) => addDRButton()); Hooks.on("renderChatLog", (app, html) => addDRButton(html));
// Mini/Popout chat // Mini/Popout chat
Hooks.on("renderChatPopout", (_app, _html) => addDRButton()); Hooks.on("renderChatPopout", (app, html) => addDRButton(html));
function addDRButton() { /**
* Adds the DR button to the chat controls
* @param {HTMLElement|jQuery} html - The application HTML
*/
function addDRButton(html) {
if (!html) return;
try { try {
// Find all roll-privacy divs and append one button per container, avoiding duplicates // Handle both jQuery (legacy) and HTMLElement (v14/AppV2)
const rollPrivacyDivs = document.querySelectorAll("#roll-privacy"); const root = html instanceof HTMLElement ? html : html[0];
rollPrivacyDivs.forEach(div => { if (!root) return;
// If this container already has our button, skip adding another
if (div.querySelector(".dr-quick-button-container")) return;
const container = document.createElement("div"); console.debug("DR Quick Button | addDRButton called. Root element:", root.tagName, root.className, root.getAttribute("data-application-part"));
container.className = "dr-quick-button-container";
// Build button // Common selectors for chat controls/roll privacy across versions
const btn = document.createElement("button"); const targetSelectors = [
btn.type = "button"; "#message-modes", // Foundry v14+ (Split button container)
btn.className = "ui-control icon fas-solid dr-quick-button"; "#chat-controls", // Generic chat controls
btn.title = "Duality Dice Roll"; "#roll-privacy", // Legacy ID
btn.setAttribute("aria-label", "Duality Dice Roll"); ".roll-privacy", // Legacy class
".roll-type-select", // System specific
".chat-controls" // Generic fallback
];
// Inline SVG (or use icon.src = "modules/dr-quick-button/icons/dr.svg"; if external) let found = false;
btn.innerHTML = `<img src="systems/daggerheart/assets/icons/dice/duality/Daggerheart Foundry_g489.png" alt="DR" style="width:12px; height:16px;">`; for (const selector of targetSelectors) {
btn.addEventListener("click", async () => { // Find targets within the root, or check if the root itself is the target
await runDRCommand(); let targets = Array.from(root.querySelectorAll(selector));
if (root.matches && root.matches(selector)) targets.push(root);
// Fallback: If not found in the provided root part, search the whole document
// (In v14, the hook might pass only one part of the ApplicationV2)
if (targets.length === 0) {
const globalTargets = document.querySelectorAll(selector);
// Only consider global targets that are actually visible/relevant
targets = Array.from(globalTargets).filter(t => root.contains(t) || t.closest(".window-app") === root.closest(".window-app") || t.closest("#sidebar") === root.closest("#sidebar"));
}
if (targets.length === 0) continue;
targets.forEach(div => {
// Avoid duplicates
if (div.querySelector(".dr-quick-button-container")) return;
console.log(`DR Quick Button | Adding button to target: ${selector}`);
const container = document.createElement("div");
container.className = "dr-quick-button-container";
const btn = document.createElement("button");
btn.type = "button";
btn.className = "ui-control icon fa-solid dr-quick-button";
btn.title = "Duality Dice Roll";
btn.setAttribute("aria-label", "Duality Dice Roll");
// Use the Daggerheart system icon
btn.innerHTML = `<img src="systems/daggerheart/assets/icons/dice/duality/Daggerheart Foundry_g489.png" alt="DR" style="width:12px; height:16px;">`;
btn.addEventListener("click", async (event) => {
event.preventDefault();
await runDRCommand();
});
container.appendChild(btn);
div.appendChild(container);
found = true;
}); });
container.appendChild(btn); if (found) break;
div.appendChild(container); }
});
if (!found) {
console.debug("DR Quick Button | No suitable target found in root or related containers.");
}
} catch (err) { } catch (err) {
console.error("DR Quick Button | addDRButton error:", err); console.error("DR Quick Button | addDRButton error:", err);
} }
} }
/** Run `/dr` as if typed into chat */ /** Run `/dr` as if typed into chat */
async function runDRCommand() { async function runDRCommand() {
try { try {