TheFabulousPika / steam-chatlog-to-text

A chrome extension that converts Steam webchat log to text format
2 stars 0 forks source link

Fix: Timestamp logic #56

Closed TheFabulousPika closed 3 years ago

TheFabulousPika commented 3 years ago

Time stamp logic is no longer working following the update that allows adding reactions to chat messages. Stay away from previous/next sibling and use classnames for futureproofing

function ChatMessageBlock(a){
    var thisBlockSpeaker = a.getElementsByClassName("speakerName")[0].innerText;
    var thisBlockMsg = a.getElementsByClassName("msgText");
    var firstMsg = thisBlockMsg[0];
    var thisBlockTime;
    if (checkIfMe(firstMsg)){
    thisBlockTime = cleanupTimeStamp(firstMsg.nextSibling.innerText);
    }
    else {
    thisBlockTime = cleanupTimeStamp(a.getElementsByClassName("speakerTimeStamp")[0].innerText);
    }
    var thisBlockMsgTime = new Array(thisBlockMsg.length);
    var thisBlockReturn = '';
    thisBlockMsgTime[0] = thisBlockTime;
        for (var j = 1; j < thisBlockMsg.length ; j++){
            var thisMsgTime;
//If the msg has an associated timestamp within the log, use that one
            if (thisBlockMsg[j].parentNode.childNodes.length > '2'){
                if (checkIfMe(thisBlockMsg[j])){
                thisMsgTime = thisBlockMsg[j].nextSibling.innerHTML;
                thisBlockMsgTime[j] = thisMsgTime;
                }
                else {
                thisMsgTime = thisBlockMsg[j].previousSibling.innerHTML;
                thisBlockMsgTime[j] = thisMsgTime;
                }
            }
            else {
//Subsequent msgs sent on the same timestamp from the same speaker will not have timestamps in the log, hence timestamp will be copied from the previous msg
                thisBlockMsgTime[j] = thisBlockMsgTime[j-1];
            }
        }
        for (var k = 0; k < thisBlockMsg.length ; k++){
        var thisMsg = cleanupMsg(thisBlockMsg[k]);
        thisBlockReturn = thisBlockReturn + thisBlockMsgTime[k] + ' - <span class="speaker">' + thisBlockSpeaker + ':</span> ' + thisMsg + '<br />';
        }
        return thisBlockReturn;
}
TheFabulousPika commented 3 years ago

New logic /me: use className=speakerTimeStamp innerText of the message block

not /me: default to className=speakerTimeStamp innerText of the message block, but use className=FriendChatTimeStamp innerText of msgText node's parent if available.

<div class="msg messages_MsgWithAddons_lFLbk isCurrentUser HasTimeStamp">
  <div class="FriendChatTimeStamp online " style="width: 108px;">9:59 PM</div>
  <div class="msgText " data-copytext=""><span data-copytext="" data-copystyle="merge-adjacent" bbcode-text="multi">multi</span></div>
  <div class="messages_Addons_1aOXJ"><svg width="30" height="25" viewBox="-2 0 30 25" fill="none" class="messages_Emoticon_ysEyj reactions_AddReactionIcon_2UK4J">
      <path d="M24.354 12.427A11.927 11.927 0 1115.571.922M18.431 14.148a6.246 6.246 0 01-11.917.292" stroke="#8492A4"></path>
      <ellipse cx="8.877" cy="9.019" rx="1.065" ry="1.775" fill="#8492A4"></ellipse>
      <ellipse cx="15.978" cy="9.018" rx="1.065" ry="1.775" fill="#8492A4"></ellipse>
      <path fill="#8492A4" d="M27.024 3.76v1h-8.522v-1z"></path>
      <path fill="#8492A4" d="M23.264 8.521h-1V-.001h1z"></path>
    </svg></div>
</div>
TheFabulousPika commented 3 years ago

New

function ChatMessageBlock(a){
    var thisBlock = a;
    var thisBlockSpeaker = thisBlock.getElementsByClassName("speakerName")[0].innerText;
    var thisBlockTime = cleanupTimeStamp(thisBlock.getElementsByClassName("speakerTimeStamp")[0].innerText);
    var thisBlockMsg = thisBlock.getElementsByClassName("msgText");
    var firstMsg = thisBlockMsg[0];
    var thisBlockMsgTime = new Array(thisBlockMsg.length);
    var thisBlockReturn = '';
    thisBlockMsgTime[0] = thisBlockTime;
        for (var j = 1; j < thisBlockMsg.length ; j++){
            var thisMsgTime;
//If the msg has an associated timestamp within the log, use that one
            if (checkIfMe(thisBlockMsg[j])){
                thisMsgTime = thisBlockMsg[j].parentNode.getElementsByClassName("speakerTimeStamp")[0].innerText;
                thisBlockMsgTime[j] = thisMsgTime;
            }
            else if (thisBlockMsg[j].parentNode.getElementsByClassName("FriendChatTimeStamp").length > 0) {
                thisMsgTime = thisBlockMsg[j].parentNode.getElementsByClassName("FriendChatTimeStamp")[0].innerText;
                thisBlockMsgTime[j] = thisMsgTime;
            }
            else {
//Subsequent msgs sent on the same timestamp from the same speaker will not have timestamps in the log, hence timestamp will be copied from the previous msg
                thisBlockMsgTime[j] = thisBlockMsgTime[j-1];
            }
        }
        for (var k = 0; k < thisBlockMsg.length ; k++){
        var thisMsg = cleanupMsg(thisBlockMsg[k]);
        thisBlockReturn = thisBlockReturn + thisBlockMsgTime[k] + ' - <span class="speaker">' + thisBlockSpeaker + ':</span> ' + thisMsg + '<br />';
        }
        return thisBlockReturn;
}
function ChatMessageBlockSingletonMsg(a){
    var thisChatBlock;
    if (checkFormatting(a,"voiceChannelInvite")){
    thisChatBlock = a.getElementsByClassName("msg voiceChannelInvite")[0].innerText + '<hr />';
    return thisChatBlock;
    }
    else {
    var msgText = a.getElementsByClassName("msgText")[0];
    var thisBlockSpeaker = a.getElementsByClassName("speakerName")[0].innerText;
    var msgTime;
        if (checkIfMe(msgText)){
        msgTime = cleanupTimeStamp(msgText.parentNode.getElementsByClassName("speakerTimeStamp")[0].innerText);
        }
        else {
        msgTime = cleanupTimeStamp(a.getElementsByClassName("speakerTimeStamp")[0].innerText);
        }
    var cleanedMsgText = cleanupMsg(msgText);
    thisChatBlock = msgTime + ' - <span class="speaker">' + thisBlockSpeaker + ':</span> ' + cleanedMsgText + '<br />';
    return thisChatBlock;
    }
}