From faa84134da980cea44bd77975853dbb60d78d644 Mon Sep 17 00:00:00 2001
From: Koen <koen@pop-os.localdomain>
Date: Tue, 19 Sep 2023 14:40:55 +0200
Subject: [PATCH] Ported changes from Hotfix.

---
 TwitchConfig.json |  2 +-
 TwitchScript.js   | 50 ++++++++++++++++++++++++++++++-----------------
 2 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/TwitchConfig.json b/TwitchConfig.json
index c9bea1f..f610aed 100644
--- a/TwitchConfig.json
+++ b/TwitchConfig.json
@@ -7,7 +7,7 @@
 	"sourceUrl": "https://plugins.grayjay.app/Twitch/TwitchConfig.json",
 	"repositoryUrl": "https://futo.org",
 	"scriptUrl": "./TwitchScript.js",
-	"version": 8,
+	"version": 10,
 
 	"iconUrl": "./twitch.png",
 	"id": "c0f315f9-0992-4508-a061-f2738724c331",
diff --git a/TwitchScript.js b/TwitchScript.js
index d6e4802..45757d4 100644
--- a/TwitchScript.js
+++ b/TwitchScript.js
@@ -439,6 +439,13 @@ source.getComments = function (url) {
 source.getSubComments = function (comment) {
     return new CommentPager([], false, {}) //Not implemented
 }
+source.getLiveChatWindow = function(url) {
+    const login = url.split('/').pop()
+    return {
+        url: "https://www.twitch.tv/popout/" + login + "/chat",
+        removeElements: [ ".stream-chat-header", ".chat-room__content > div:first-child"]
+    };
+}
 source.getLiveEvents = function (url) {
     //TODO: Make this more robust, easy to break, expect query parameters.
     const login = url.split('/').pop()
@@ -458,31 +465,32 @@ source.getLiveEvents = function (url) {
             },
         },
         {
-            query: '#import "twilight/features/message/fragments/message-content-fragment.gql" query MessageBufferChatHistory($channelLogin: String! $channelID: ID) { channel(name: $channelLogin) { id recentChatMessages { ...historicalMessage } } } fragment chatHistoryParentMessage on Message { id content { text } deletedAt sender { id login displayName } } fragment historicalMessage on Message { id deletedAt sentAt content { ...messageContent } parentMessage { ...chatHistoryParentMessage } sender { id login chatColor displayName __typename } senderBadges(channelID: $channelID) { setID version id } }',
+            query: '#import "twilight/features/badges/models/badge-fragment.gql" #import "twilight/features/squad-stream/models/squad-stream-fragment.gql" query ChatList_Badges($channelLogin: String!) { badges { ...badge } user(login: $channelLogin) { id primaryColorHex broadcastBadges { ...badge } self { selectedBadge { ...badge } displayBadges { ...badge } } squadStream { ...squadStreamData } } }',
             extensions: {
                 persistedQuery: {
-                    sha256Hash: '432ef3ec504a750d797297630052ec7c775f571f6634fdbda255af9ad84325ae',
+                    sha256Hash: '86f43113c04606e6476e39dcd432dee47c994d77a83e54b732e11d4935f0cd08',
                     version: 1,
                 },
             },
-            operationName: 'MessageBufferChatHistory',
+            operationName: 'ChatList_Badges',
             variables: {
                 channelLogin: login,
             },
         },
+        /*
         {
-            query: '#import "twilight/features/badges/models/badge-fragment.gql" #import "twilight/features/squad-stream/models/squad-stream-fragment.gql" query ChatList_Badges($channelLogin: String!) { badges { ...badge } user(login: $channelLogin) { id primaryColorHex broadcastBadges { ...badge } self { selectedBadge { ...badge } displayBadges { ...badge } } squadStream { ...squadStreamData } } }',
+            query: '#import "twilight/features/message/fragments/message-content-fragment.gql" query MessageBufferChatHistory($channelLogin: String! $channelID: ID) { channel(name: $channelLogin) { id recentChatMessages { ...historicalMessage } } } fragment chatHistoryParentMessage on Message { id content { text } deletedAt sender { id login displayName } } fragment historicalMessage on Message { id deletedAt sentAt content { ...messageContent } parentMessage { ...chatHistoryParentMessage } sender { id login chatColor displayName __typename } senderBadges(channelID: $channelID) { setID version id } }',
             extensions: {
                 persistedQuery: {
-                    sha256Hash: '86f43113c04606e6476e39dcd432dee47c994d77a83e54b732e11d4935f0cd08',
+                    sha256Hash: '432ef3ec504a750d797297630052ec7c775f571f6634fdbda255af9ad84325ae',
                     version: 1,
                 },
             },
-            operationName: 'ChatList_Badges',
+            operationName: 'MessageBufferChatHistory',
             variables: {
                 channelLogin: login,
             },
-        },
+        }*/
     ]
 
     const json = callGQL(gql)
@@ -492,13 +500,13 @@ source.getLiveEvents = function (url) {
     const userOrError = ChannelShellResponse.data.userOrError
 
     /** @type {import("./types.d.ts").RecentChatsResponse} */
-    const RecentChatsResponse = json[1]
-    const chats = RecentChatsResponse.data.channel.recentChatMessages.map(
+    //const RecentChatsResponse = json[2]
+    const chats = []; /*RecentChatsResponse.data.channel.recentChatMessages.map(
         (chat) => new LiveEventComment(chat.sender.login, chat.content.text, '', chat.sender.chatColor)
-    )
+    )*/
 
     /** @type {import("./types.d.ts").BadgeListResponse} */
-    const BadgeListResponse = json[2]
+    const BadgeListResponse = json[1]
 
     let badge_url_map = {}
 
@@ -574,17 +582,23 @@ class TwitchLiveEventPager extends LiveEventPager {
                         }
                     }
 
-                    const name = msg.match(/;display-name=([^;]+);/)[1]
-                    const color = msg.match(/;color=([^;]+);/)[1]
-                    const badges = msg.match(/;badges=([^;]+);/)[1]
-                    const badge_array = badges.split(',')
+                    const nameMatch = msg.match(/;display-name=([^;]+);/);
+                    const name = (nameMatch && nameMatch.length >= 2) ? nameMatch[1] : null;
+                    const colorMatch = msg.match(/;color=([^;]+);/);
+                    const color = (colorMatch && colorMatch.length >= 2) ? colorMatch[1] : null;
+                    const badges = msg.match(/;badges=([^;]+);/)
+                    const badge_array = (badges && badges.length >= 2) ? badges[1].split(',') : [];
                     badge_array.forEach((badge) => {
                         newEmojis[badge] = badge_url_map[badge]
                     })
                     
-                    if (Object.keys(newEmojis).length > 0) me.events.push(new LiveEventEmojis(newEmojis))
-
-                    me.events.push(new LiveEventComment(name, parsedMessage.msg, '', color, badge_array))
+                    if (Object.keys(newEmojis).length > 0) 
+                        me.events.push(new LiveEventEmojis(newEmojis))
+                    
+                    if(name)
+                        me.events.push(new LiveEventComment(name, parsedMessage.msg, '', color, badge_array))
+                    else if(IS_TESTING)
+                        console.log("Failed name/color: " + msg);
                 },
             },
             false
-- 
GitLab