@@ -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