Fix review comments

This commit is contained in:
Younes Aassila 2025-02-07 14:06:10 +01:00
parent e327d38899
commit d65b3bb8bf
No known key found for this signature in database
2 changed files with 25 additions and 19 deletions

View File

@ -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();
}
}
}
}

View File

@ -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,
});