Twitch single-message deletion basic support

This commit is contained in:
Luxferre
2024-09-20 16:17:20 +03:00
parent 3d65d68d8a
commit 707b9e611a
7 changed files with 103 additions and 45 deletions
+42 -29
View File
@@ -39,40 +39,52 @@ function overrideStyles(text) {
}
/* individual message appender */
function gooseAppendMessage(id, type, timestamp, username, text, icon) {
function gooseAppendMessage(id, native_id, author_id, type, timestamp, username, text, icon) {
var html = ""
if(parseInt(id) > 0 && type.length > 0) {
var classes = type.split('^'), cl = classes.length, j,
colorpref = null, badges = [], platform, bname
for(j=0;j<cl;j++) { /* detect color preference */
if(classes[j].startsWith('color'))
colorpref = classes[j].slice(5) /* remove color word */
if(classes[j].startsWith('badge')) { /* remove the class but cache it */
badges.push(classes[j].slice(5)) /* remove badge word */
classes[j] = "" /* remove the class from the list */
if(type == "delmsg") { /* message deletion event */
var el = document.querySelector('[data-native-id="' + esc(native_id) + '"]')
if(el) el.parentNode.removeChild(el)
} else if(type == "deluser") { /* bulk deletion event by username */
var els = document.querySelector('[data-author-id="' + esc(author_id) + '"]')
if(els && els.length > 0) {
var i, l = els.length
for(i=0;i<l;i++) els[i].parentNode.removeChild(els[i])
}
} else { /* message appending event */
var classes = type.split('^'), cl = classes.length, j,
colorpref = null, badges = [], platform, bname
for(j=0;j<cl;j++) { /* detect color preference */
if(classes[j].startsWith('color'))
colorpref = classes[j].slice(5) /* remove color word */
if(classes[j].startsWith('badge')) { /* remove the class but cache it */
badges.push(classes[j].slice(5)) /* remove badge word */
classes[j] = "" /* remove the class from the list */
}
}
html += '<div data-msg-id="' + id + '" data-native-id="' + esc(native_id) + '" data-author-id="'
+ esc(author_id) + '" class="' + classes.join(' ') +'">'
/* handle badges and icons */
html += '<span class=icon>'
for(j=0,cl=badges.length;j<cl;j++) { /* iterate over badges */
platform = badges[j].slice(0,2) /* first 2 characters denote the platform */
bname = badges[j].slice(2) /* the rest is the badge name itself */
html += '<img class=badge src="/badge/{platform}/{badgename}">'
.replace('{platform}', platform).replace('{badgename}', bname)
}
if(icon.length > 0) /* user icon, if present */
html += '<img src="' + icon + '">'
html += '</span>'
html += '<span class=timestamp>' + timestamp + '</span>'
html += '<span class=username'
if(colorpref) html += ' style="color:' + colorpref + '"'
html += '>' + username + '</span>'
html += '<span class=msg' + overrideStyles(text) +'>' + handleEmotes(text) + '</span></div>'
document.getElementById("msglist").innerHTML += html
}
html += '<div data-msg-id="' + id + '" class="' + classes.join(' ') +'">'
/* handle badges and icons */
html += '<span class=icon>'
for(j=0,cl=badges.length;j<cl;j++) { /* iterate over badges */
platform = badges[j].slice(0,2) /* first 2 characters denote the platform */
bname = badges[j].slice(2) /* the rest is the badge name itself */
html += '<img class=badge src="/badge/{platform}/{badgename}">'
.replace('{platform}', platform).replace('{badgename}', bname)
}
if(icon.length > 0) /* user icon, if present */
html += '<img src="' + icon + '">'
html += '</span>'
html += '<span class=timestamp>' + timestamp + '</span>'
html += '<span class=username'
if(colorpref) html += ' style="color:' + colorpref + '"'
html += '>' + username + '</span>'
html += '<span class=msg' + overrideStyles(text) +'>' + handleEmotes(text) + '</span></div>'
}
document.getElementById("msglist").innerHTML += html
/* autoscroll to the end */
window.setTimeout(function(){window.scrollTo(0, document.body.scrollHeight)}, 100)
window.setTimeout(function(){window.scrollTo(0, document.body.scrollHeight)}, 150)
}
/* entry point, set up an event source */
@@ -80,6 +92,7 @@ window.addEventListener('DOMContentLoaded', function() {
var msgSource = new EventSource('/sse')
msgSource.onmessage = e => {
var msg = JSON.parse(e.data)
gooseAppendMessage(msg.id, msg.type, msg.timestamp, msg.username, msg.text, msg.icon)
console.log("Message", msg)
gooseAppendMessage(msg.id, msg.native_id, msg.author_id, msg.type, msg.timestamp, msg.username, msg.text, msg.icon)
}
}, false)