From 59a80978ebb61bd92622cfb4ca8508eeb8e5638e Mon Sep 17 00:00:00 2001 From: lx Date: Thu, 7 Mar 2024 17:44:31 +0100 Subject: [PATCH] Add possibility to merge consecutive messages from same user, so they are in one --- CustomChat/widget.js | 16 +++++++++++++++- CustomChat/widget.json | 10 ++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CustomChat/widget.js b/CustomChat/widget.js index c76afcb..49b7399 100644 --- a/CustomChat/widget.js +++ b/CustomChat/widget.js @@ -5,6 +5,8 @@ let animationOut = 'bounceOut'; let hideAfter = 60; let hideCommands = "no"; let ignoredUsers = []; +let previousSender = ""; +let mergeMessages = false; window.addEventListener('onEventReceived', function (obj) { if (obj.detail.event.listener === 'widget-button') { @@ -74,7 +76,7 @@ window.addEventListener('onEventReceived', function (obj) { } if (obj.detail.listener === "delete-message") { const msgId = obj.detail.event.msgId; - $(`.message-row[data-msgid=${msgId}]`).remove(); + $(`[data-msgid=${msgId}]`).remove(); return; } else if (obj.detail.listener === "delete-messages") { const sender = obj.detail.event.userId; @@ -108,6 +110,7 @@ window.addEventListener('onEventReceived', function (obj) { username = ''; } addMessage(username, badges, message, data.isAction, data.userId, data.msgId); + previousSender = data.userId; }); window.addEventListener('onWidgetLoad', function (obj) { @@ -120,6 +123,7 @@ window.addEventListener('onWidgetLoad', function (obj) { customNickColor = fieldData.customNickColor; hideCommands = fieldData.hideCommands; channelName = obj.detail.channel.username; + mergeMessages = fieldData.mergeMessages === "yes"; fetch('https://api.streamelements.com/kappa/v2/channels/' + obj.detail.channel.id + '/').then(response => response.json()).then((profile) => { provider = profile.provider; }); @@ -195,6 +199,16 @@ function addMessage(username, badges, message, isAction, uid, msgId) { if (isAction) { actionClass = "action"; } + if (mergeMessages && previousSender === uid) { + const lastMessage = document.querySelector('.main-container').lastElementChild; + const messageElement = document.createElement('span'); + messageElement.innerHTML = ` ${message}`; // Use `messageText` or your actual message content variable here + messageElement.dataset.sender = uid; + messageElement.dataset.msgid = msgId; + lastMessage.querySelector('.user-message').appendChild(messageElement); + return; + } + const element = $.parseHTML(`
${badges}${username}
diff --git a/CustomChat/widget.json b/CustomChat/widget.json index 7ca7065..ee0cf9b 100644 --- a/CustomChat/widget.json +++ b/CustomChat/widget.json @@ -109,6 +109,16 @@ }, "group": "Settings" }, + "mergeMessages": { + "label": "Merge consecutive messages from the same user", + "type": "dropdown", + "value": "no", + "options": { + "yes": "Yes", + "no": "No" + }, + "group": "Settings" + }, "ignoredUsers": { "label": "Ignored users (comma separated)", "type": "text",