Skip to content
This repository was archived by the owner on Aug 10, 2025. It is now read-only.

Commit fcd26cb

Browse files
committed
Closes #5 and closes #6
1 parent 8bed281 commit fcd26cb

4 files changed

Lines changed: 15 additions & 15 deletions

File tree

module.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"id": "dnd5e",
2323
"type": "system",
2424
"compatibility": {
25-
"minimum": "3.1.0",
26-
"maximum": "3.1.9"
25+
"minimum": "3.2.0",
26+
"maximum": "3.2.9"
2727
}
2828
}
2929
]

scriptable-items

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scriptable-items

scripts/data/script-model.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ export class ScriptModel extends foundry.abstract.DataModel {
158158
}
159159

160160
/**
161+
* Returns the id of this script.
161162
* @type {string}
162163
*/
163164
get id() {
164165
return this._id;
165166
}
166167

167168
/**
169+
* Returns the triggers of this script as a formatted string.
168170
* @type {string}
169171
*/
170172
get triggersText() {
@@ -176,6 +178,7 @@ export class ScriptModel extends foundry.abstract.DataModel {
176178
}
177179

178180
/**
181+
* Returns the item this script belongs to.
179182
* @type {Item}
180183
*/
181184
get item() {

scripts/scriptable-items.mjs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,26 @@ Hooks.on("dnd5e.preUseItem", (item, config, options) => {
5656
return false;
5757
});
5858

59-
Hooks.on("dnd5e.preDisplayCard", (item, data) => {
59+
Hooks.on("dnd5e.renderChatMessage", (message, html) => {
60+
const item = message.getAssociatedItem();
61+
if (!item) return;
62+
6063
const scripts = ScriptModel.getAll(item).filter((x) =>
6164
x.triggers.has(TRIGGER.BUTTON),
6265
);
6366
if (!scripts.length) return;
6467

65-
// Convert cards HTML string to workable DOM element
66-
const el = document.createElement("DIV");
67-
el.innerHTML = data.content;
68-
6968
// Add button for each script with button trigger to the card
70-
let buttonContainer = el.querySelector(".card-buttons");
69+
let buttonContainer = html.querySelector(".card-buttons");
7170
if (buttonContainer == null) {
7271
// There are no buttons yet on the chat card, so no container, adding a container...
7372
buttonContainer = document.createElement("DIV");
7473
buttonContainer.classList.add("card-buttons");
75-
const parent = el.querySelector(".chat-card");
74+
const parent = html.querySelector(".chat-card");
7675
parent.insertBefore(buttonContainer, parent.children[1]);
7776
}
7877

78+
// Create a button for each script
7979
for (const script of scripts) {
8080
const newButton = document.createElement("BUTTON");
8181
newButton.setAttribute("data-action", `${MODULE_ID}-run`);
@@ -95,16 +95,12 @@ Hooks.on("dnd5e.preDisplayCard", (item, data) => {
9595
buttonContainer.append(newButton);
9696
}
9797

98-
data.content = el.innerHTML;
99-
});
100-
101-
Hooks.on("dnd5e.renderChatMessage", (message, html) => {
98+
// Add a listener to each button
10299
html.querySelectorAll(`[data-action="${MODULE_ID}-run"]`).forEach((x) => {
103100
x.addEventListener("click", async (event) => {
104101
event.preventDefault();
105102
const scriptId = event.currentTarget.dataset.scriptId;
106-
const itemUuid = message.getFlag("dnd5e", "use.itemUuid");
107-
const item = await fromUuid(itemUuid);
103+
const item = message.getAssociatedItem();
108104
const script = ScriptModel.getById(item, scriptId);
109105
await script.executeScript({
110106
trigger: TRIGGER.BUTTON,

0 commit comments

Comments
 (0)