From d65b3bb8bf1ef3abd08c6329fd134f3a0a43b9d6 Mon Sep 17 00:00:00 2001 From: Younes Aassila <47226184+younesaassila@users.noreply.github.com> Date: Fri, 7 Feb 2025 14:06:10 +0100 Subject: [PATCH] Fix review comments --- src/content/content.ts | 38 +++++++++++++++++++++----------------- src/page/getFetch.ts | 6 ++++-- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/content/content.ts b/src/content/content.ts index 181f8e1..1180dba 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -145,43 +145,47 @@ function onPageMessage(event: MessageEvent) { } // --- else if (message.type === MessageType.ChannelSubStatusChange) { - const { channelName, wasSubscribed, isSubscribed } = message; - const isWhitelisted = isChannelWhitelisted(channelName); + const { channelNameLower, wasSubscribed, isSubscribed } = message; + const isWhitelisted = isChannelWhitelisted(channelNameLower); console.log("[TTV LOL PRO] ChannelSubStatusChange", { - channelName, + channelName: channelNameLower, wasSubscribed, isSubscribed, isWhitelisted, }); - const currentChannelName = findChannelFromTwitchTvUrl(location.href); - if (store.state.whitelistChannelSubscriptions && channelName != null) { + const currentChannelNameLower = findChannelFromTwitchTvUrl( + location.href + )?.toLowerCase(); + if (store.state.whitelistChannelSubscriptions && channelNameLower != null) { if (!wasSubscribed && isSubscribed) { - store.state.activeChannelSubscriptions.push(channelName); + store.state.activeChannelSubscriptions.push(channelNameLower); // Add to whitelist. if (!isWhitelisted) { - console.log(`[TTV LOL PRO] Adding '${channelName}' to whitelist.`); - store.state.whitelistedChannels.push(channelName); - } - if (channelName.toLowerCase() === currentChannelName?.toLowerCase()) { - location.reload(); + console.log( + `[TTV LOL PRO] Adding '${channelNameLower}' to whitelist.` + ); + store.state.whitelistedChannels.push(channelNameLower); + if (channelNameLower === currentChannelNameLower) { + location.reload(); + } } } else if (wasSubscribed && !isSubscribed) { store.state.activeChannelSubscriptions = store.state.activeChannelSubscriptions.filter( - channel => channel.toLowerCase() !== channelName.toLowerCase() + channel => channel.toLowerCase() !== channelNameLower ); // Remove from whitelist. if (isWhitelisted) { console.log( - `[TTV LOL PRO] Removing '${channelName}' from whitelist.` + `[TTV LOL PRO] Removing '${channelNameLower}' from whitelist.` ); store.state.whitelistedChannels = store.state.whitelistedChannels.filter( - channel => channel.toLowerCase() !== channelName.toLowerCase() + channel => channel.toLowerCase() !== channelNameLower ); - } - if (channelName.toLowerCase() === currentChannelName?.toLowerCase()) { - location.reload(); + if (channelNameLower === currentChannelNameLower) { + location.reload(); + } } } } diff --git a/src/page/getFetch.ts b/src/page/getFetch.ts index 337d60d..c44badb 100644 --- a/src/page/getFetch.ts +++ b/src/page/getFetch.ts @@ -408,6 +408,8 @@ export function getFetch(pageState: PageState): typeof fetch { twitchGqlHostRegex.test(host) && response.status < 400 ) { + await waitForStore(pageState); + if (!pageState.state?.whitelistChannelSubscriptions) break graphqlRes; responseBody ??= await readResponseBody(); // Preliminary check to avoid parsing the response body if possible. if ( @@ -444,16 +446,16 @@ export function getFetch(pageState: PageState): typeof fetch { channelName = body.data.user.login; isSubscribed = body.data.user.self.subscriptionBenefit != null; } + if (!channelName) break graphqlRes; const isLivestream = !/^\d+$/.test(channelName); // VODs have numeric IDs. if (!isLivestream) break graphqlRes; - await waitForStore(pageState); const wasSubscribed = wasChannelSubscriber(channelName, pageState); const hasSubStatusChanged = (wasSubscribed && !isSubscribed) || (!wasSubscribed && isSubscribed); if (hasSubStatusChanged) { pageState.sendMessageToContentScript({ type: MessageType.ChannelSubStatusChange, - channelName, + channelNameLower: channelName.toLowerCase(), wasSubscribed, isSubscribed, });