Need to handle when a new comment is added via Jira UI

Hi community,

In a JS script I need to catch when a new comment has been created in the UI (with Add comment button). Is there any event to to handle it?

I’ve tried JIRA.Events.NEW_CONTENT_ADDED but this is too generic, I just need when a new comment is created.

Thanks in advance.

Hi @AdolfoCasari,
You can use MutationObserver.

Hi @AdolfoCasari

You can try something like this :

// Observer callback to process mutations
const observerCallback = function(mutationsList, observer) {
    for (const mutation of mutationsList) {
        if (mutation.type === 'childList') {
            mutation.addedNodes.forEach(node => {
                // Check if the added node is a comment
                if (node.nodeType === Node.ELEMENT_NODE && node.matches('.comment-class')) { // Replace '.comment-class' with the actual class or criteria identifying comment nodes
                        // Place your specific action here
                }
            });
        }
    }
};

// Options for the observer (which mutations to observe)
const observerOptions = { childList: true, subtree: true };

// Select the target node, replace 'commentsContainerSelector' with the actual selector
const targetNode = document.querySelector('commentsContainerSelector'); 

// Create an instance of MutationObserver and start observing
if (targetNode) {
    const observer = new MutationObserver(observerCallback);
    observer.observe(targetNode, observerOptions);
} 

Fabien