Fix review comments

This commit is contained in:
Younes Aassila 2025-02-04 16:09:34 +01:00
parent 9f45b77b83
commit 6fcae2eea1
No known key found for this signature in database
3 changed files with 31 additions and 23 deletions

View File

@ -6,6 +6,6 @@ export default function isChannelWhitelisted(
if (!channelName) return false; if (!channelName) return false;
const channelNameLower = channelName.toLowerCase(); const channelNameLower = channelName.toLowerCase();
return store.state.whitelistedChannels.some( return store.state.whitelistedChannels.some(
c => c.toLowerCase() === channelNameLower channel => channel.toLowerCase() === channelNameLower
); );
} }

View File

@ -145,47 +145,51 @@ function onPageMessage(event: MessageEvent) {
} }
// --- // ---
else if (message.type === MessageType.ChannelSubStatusChange) { else if (message.type === MessageType.ChannelSubStatusChange) {
const { channelName, wasSubscribed, isSubscribed } = message; const { channelNameLower, wasSubscribed, isSubscribed } = message;
const isWhitelisted = isChannelWhitelisted(channelName); const isWhitelisted = isChannelWhitelisted(channelNameLower);
console.log("[TTV LOL PRO] ChannelSubStatusChange", { console.log("[TTV LOL PRO] ChannelSubStatusChange", {
channelName, channelNameLower,
wasSubscribed, wasSubscribed,
isSubscribed, isSubscribed,
isWhitelisted, isWhitelisted,
}); });
const currentChannelName = findChannelFromTwitchTvUrl(location.href); const currentChannelNameLower = findChannelFromTwitchTvUrl(
if (store.state.whitelistChannelSubscriptions && channelName != null) { location.href
)?.toLowerCase();
if (store.state.whitelistChannelSubscriptions && channelNameLower != null) {
if (!wasSubscribed && isSubscribed) { if (!wasSubscribed && isSubscribed) {
store.state.activeChannelSubscriptions.push(channelName); store.state.activeChannelSubscriptions.push(channelNameLower);
// Add to whitelist. // Add to whitelist.
if (!isWhitelisted) { if (!isWhitelisted) {
console.log(`[TTV LOL PRO] Adding '${channelName}' to whitelist.`); console.log(
store.state.whitelistedChannels.push(channelName); `[TTV LOL PRO] Adding '${channelNameLower}' to whitelist.`
} );
if (channelName === currentChannelName) { store.state.whitelistedChannels.push(channelNameLower);
if (channelNameLower === currentChannelNameLower) {
location.reload(); location.reload();
} }
}
} else if (wasSubscribed && !isSubscribed) { } else if (wasSubscribed && !isSubscribed) {
store.state.activeChannelSubscriptions = store.state.activeChannelSubscriptions =
store.state.activeChannelSubscriptions.filter( store.state.activeChannelSubscriptions.filter(
c => c.toLowerCase() !== channelName.toLowerCase() channel => channel.toLowerCase() !== channelNameLower
); );
// Remove from whitelist. // Remove from whitelist.
if (isWhitelisted) { if (isWhitelisted) {
console.log( console.log(
`[TTV LOL PRO] Removing '${channelName}' from whitelist.` `[TTV LOL PRO] Removing '${channelNameLower}' from whitelist.`
); );
store.state.whitelistedChannels = store.state.whitelistedChannels =
store.state.whitelistedChannels.filter( store.state.whitelistedChannels.filter(
c => c.toLowerCase() !== channelName.toLowerCase() channel => channel.toLowerCase() !== channelNameLower
); );
} if (channelNameLower === currentChannelNameLower) {
if (channelName === currentChannelName) {
location.reload(); location.reload();
} }
} }
} }
} }
}
// --- // ---
else if (message.type === MessageType.UsherResponse) { else if (message.type === MessageType.UsherResponse) {
try { try {

View File

@ -72,7 +72,9 @@ export function getFetch(pageState: PageState): typeof fetch {
if (!message.channelName) break; if (!message.channelName) break;
const channelNameLower = message.channelName.toLowerCase(); const channelNameLower = message.channelName.toLowerCase();
for (let i = 0; i < usherManifests.length; i++) { for (let i = 0; i < usherManifests.length; i++) {
if (usherManifests[i].channelName === channelNameLower) { if (
usherManifests[i].channelName?.toLowerCase() === channelNameLower
) {
usherManifests[i].deleted = true; usherManifests[i].deleted = true;
} }
} }
@ -406,6 +408,8 @@ export function getFetch(pageState: PageState): typeof fetch {
twitchGqlHostRegex.test(host) && twitchGqlHostRegex.test(host) &&
response.status < 400 response.status < 400
) { ) {
await waitForStore(pageState);
if (!pageState.state?.whitelistChannelSubscriptions) break graphqlRes;
responseBody ??= await readResponseBody(); responseBody ??= await readResponseBody();
// Preliminary check to avoid parsing the response body if possible. // Preliminary check to avoid parsing the response body if possible.
if ( if (
@ -442,16 +446,16 @@ export function getFetch(pageState: PageState): typeof fetch {
channelName = body.data.user.login; channelName = body.data.user.login;
isSubscribed = body.data.user.self.subscriptionBenefit != null; isSubscribed = body.data.user.self.subscriptionBenefit != null;
} }
if (!channelName) break graphqlRes;
const isLivestream = !/^\d+$/.test(channelName); // VODs have numeric IDs. const isLivestream = !/^\d+$/.test(channelName); // VODs have numeric IDs.
if (!isLivestream) break graphqlRes; if (!isLivestream) break graphqlRes;
await waitForStore(pageState);
const wasSubscribed = wasChannelSubscriber(channelName, pageState); const wasSubscribed = wasChannelSubscriber(channelName, pageState);
const hasSubStatusChanged = const hasSubStatusChanged =
(wasSubscribed && !isSubscribed) || (!wasSubscribed && isSubscribed); (wasSubscribed && !isSubscribed) || (!wasSubscribed && isSubscribed);
if (hasSubStatusChanged) { if (hasSubStatusChanged) {
pageState.sendMessageToContentScript({ pageState.sendMessageToContentScript({
type: MessageType.ChannelSubStatusChange, type: MessageType.ChannelSubStatusChange,
channelName, channelNameLower: channelName.toLowerCase(),
wasSubscribed, wasSubscribed,
isSubscribed, isSubscribed,
}); });
@ -682,7 +686,7 @@ function isChannelWhitelisted(
const channelNameLower = channelName.toLowerCase(); const channelNameLower = channelName.toLowerCase();
return ( return (
pageState.state?.whitelistedChannels.some( pageState.state?.whitelistedChannels.some(
c => c.toLowerCase() === channelNameLower channel => channel.toLowerCase() === channelNameLower
) ?? false ) ?? false
); );
} }
@ -695,7 +699,7 @@ function wasChannelSubscriber(
const channelNameLower = channelName.toLowerCase(); const channelNameLower = channelName.toLowerCase();
return ( return (
pageState.state?.activeChannelSubscriptions.some( pageState.state?.activeChannelSubscriptions.some(
c => c.toLowerCase() === channelNameLower channel => channel.toLowerCase() === channelNameLower
) ?? false ) ?? false
); );
} }