Skip to content

Commit

Permalink
Add possibility to merge consecutive messages from same user, so they…
Browse files Browse the repository at this point in the history
… are in one
  • Loading branch information
justlx committed Mar 7, 2024
1 parent 74c953c commit 59a8097
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
16 changes: 15 additions & 1 deletion CustomChat/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
});
Expand Down Expand Up @@ -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(`
<div data-sender="${uid}" data-msgid="${msgId}" class="message-row {animationIn} animated" id="msg-${totalMessages}">
<div class="user-box ${actionClass}">${badges}${username}</div>
Expand Down
10 changes: 10 additions & 0 deletions CustomChat/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 59a8097

Please sign in to comment.