From 87cc7d04e82a5762a10838908355736798506bde Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Tue, 30 Jan 2024 22:36:44 +0100 Subject: [PATCH 01/12] Testing --- src/background/handlers/onContentScriptMessage.ts | 2 +- src/content/content.ts | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/background/handlers/onContentScriptMessage.ts b/src/background/handlers/onContentScriptMessage.ts index 23b595d2..f4de706d 100644 --- a/src/background/handlers/onContentScriptMessage.ts +++ b/src/background/handlers/onContentScriptMessage.ts @@ -23,7 +23,7 @@ export default function onContentScriptMessage( } // Set new timeout for request type. - const fetchTimeoutMs = 3000; // Time for fetch to be called. + const fetchTimeoutMs = 5000; // Time for fetch to be called. const replyTimeoutMs = Date.now() - message.timestamp; // Time for reply to be received. timeoutMap.set( requestType, diff --git a/src/content/content.ts b/src/content/content.ts index 872c9124..82f8441c 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -52,10 +52,7 @@ function clearStats() { if (!channelName) return; if (store.state.streamStatuses.hasOwnProperty(channelName)) { - setStreamStatus(channelName, { - proxied: false, - reason: "", - }); + delete store.state.streamStatuses[channelName]; } console.log( `[TTV LOL PRO] Cleared stats for channel '${channelName}' (content script).` From cc1353213787dc4b4790f5fd715ec1a082cc979d Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Wed, 31 Jan 2024 15:18:31 +0100 Subject: [PATCH 02/12] Improve clear stats process --- .../handlers/onContentScriptMessage.ts | 2 +- src/content/content.ts | 15 +++++------ src/page/getFetch.ts | 15 ++++++++--- src/page/page.ts | 26 ++++++++++++++----- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/background/handlers/onContentScriptMessage.ts b/src/background/handlers/onContentScriptMessage.ts index f4de706d..d8310480 100644 --- a/src/background/handlers/onContentScriptMessage.ts +++ b/src/background/handlers/onContentScriptMessage.ts @@ -23,7 +23,7 @@ export default function onContentScriptMessage( } // Set new timeout for request type. - const fetchTimeoutMs = 5000; // Time for fetch to be called. + const fetchTimeoutMs = 4000; // Time for fetch to be called. const replyTimeoutMs = Date.now() - message.timestamp; // Time for reply to be received. timeoutMap.set( requestType, diff --git a/src/content/content.ts b/src/content/content.ts index 82f8441c..fc913b6a 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -40,22 +40,21 @@ function injectPageScript() { function onStoreLoad() { // Clear stats for stream on page load/reload. - clearStats(); + clearStats(findChannelFromTwitchTvUrl(location.href)); } /** * Clear stats for stream on page load/reload. * @returns */ -function clearStats() { - const channelName = findChannelFromTwitchTvUrl(location.href); +function clearStats(channelName: string | null) { if (!channelName) return; - - if (store.state.streamStatuses.hasOwnProperty(channelName)) { - delete store.state.streamStatuses[channelName]; + const channelNameLower = channelName.toLowerCase(); + if (store.state.streamStatuses.hasOwnProperty(channelNameLower)) { + delete store.state.streamStatuses[channelNameLower]; } console.log( - `[TTV LOL PRO] Cleared stats for channel '${channelName}' (content script).` + `[TTV LOL PRO] Cleared stats for channel '${channelNameLower}' (content script).` ); } @@ -151,7 +150,7 @@ function onPageMessage(event: MessageEvent) { }); break; case MessageType.ClearStats: - clearStats(); + clearStats(message.channelName); break; } } diff --git a/src/page/getFetch.ts b/src/page/getFetch.ts index ec0daee0..b4041482 100644 --- a/src/page/getFetch.ts +++ b/src/page/getFetch.ts @@ -69,10 +69,17 @@ export function getFetch(pageState: PageState): typeof fetch { switch (message.type) { case MessageType.ClearStats: console.log("[TTV LOL PRO] Cleared stats (getFetch)."); - usherManifests = []; - cachedPlaybackTokenRequestHeaders = null; - cachedPlaybackTokenRequestBody = null; - cachedUsherRequestUrl = null; + const channelNameLower = message.channelName.toLowerCase(); + usherManifests = usherManifests.filter( + manifest => manifest.channelName !== channelNameLower + ); + if (cachedPlaybackTokenRequestBody?.includes(channelNameLower)) { + cachedPlaybackTokenRequestHeaders = null; + cachedPlaybackTokenRequestBody = null; + } + if (cachedUsherRequestUrl?.includes(channelNameLower)) { + cachedUsherRequestUrl = null; + } break; } }); diff --git a/src/page/page.ts b/src/page/page.ts index bdab078c..3aa533a5 100644 --- a/src/page/page.ts +++ b/src/page/page.ts @@ -148,7 +148,9 @@ window.addEventListener("message", event => { } }); -function onChannelChange(callback: (channelName: string) => void) { +function onChannelChange( + callback: (channelName: string, oldChannelName: string | null) => void +) { let channelName: string | null = findChannelFromTwitchTvUrl(location.href); const NATIVE_PUSH_STATE = window.history.pushState; @@ -161,8 +163,9 @@ function onChannelChange(callback: (channelName: string) => void) { const fullUrl = toAbsoluteUrl(url.toString()); const newChannelName = findChannelFromTwitchTvUrl(fullUrl); if (newChannelName != null && newChannelName !== channelName) { + const oldChannelName = channelName; channelName = newChannelName; - callback(channelName); + callback(channelName, oldChannelName); } return NATIVE_PUSH_STATE.call(window.history, data, unused, url); } @@ -178,8 +181,9 @@ function onChannelChange(callback: (channelName: string) => void) { const fullUrl = toAbsoluteUrl(url.toString()); const newChannelName = findChannelFromTwitchTvUrl(fullUrl); if (newChannelName != null && newChannelName !== channelName) { + const oldChannelName = channelName; channelName = newChannelName; - callback(channelName); + callback(channelName, oldChannelName); } return NATIVE_REPLACE_STATE.call(window.history, data, unused, url); } @@ -188,17 +192,25 @@ function onChannelChange(callback: (channelName: string) => void) { window.addEventListener("popstate", () => { const newChannelName = findChannelFromTwitchTvUrl(location.href); if (newChannelName != null && newChannelName !== channelName) { + const oldChannelName = channelName; channelName = newChannelName; - callback(channelName); + callback(channelName, oldChannelName); } }); } -onChannelChange(() => { - sendMessageToContentScript({ type: MessageType.ClearStats }); - sendMessageToPageScript({ type: MessageType.ClearStats }); +onChannelChange((channelName, oldChannelName) => { + sendMessageToContentScript({ + type: MessageType.ClearStats, + channelName: oldChannelName, + }); + sendMessageToPageScript({ + type: MessageType.ClearStats, + channelName: oldChannelName, + }); sendMessageToWorkerScript(pageState.twitchWorker, { type: MessageType.ClearStats, + channelName: oldChannelName, }); }); From 4079415f9a77bf937221e5b56cb28cdae3c7adb9 Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Wed, 31 Jan 2024 15:35:06 +0100 Subject: [PATCH 03/12] Fix #296 --- src/common/ts/updateDnsResponses.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/common/ts/updateDnsResponses.ts b/src/common/ts/updateDnsResponses.ts index 6a228428..4508eb8b 100644 --- a/src/common/ts/updateDnsResponses.ts +++ b/src/common/ts/updateDnsResponses.ts @@ -41,7 +41,11 @@ export default async function updateDnsResponses() { } try { - const response = await fetch(`https://dns.google/resolve?name=${host}`); + const response = await fetch(`https://1.1.1.1/dns-query?name=${host}`, { + headers: { + Accept: "application/dns-json", + }, + }); const json = await response.json(); const { Answer } = json; if (!Array.isArray(Answer)) { From 5eeeb251473286679d600f83f182991bd0176c19 Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Wed, 31 Jan 2024 15:37:32 +0100 Subject: [PATCH 04/12] Use domain name for Cloudflare DNS --- src/common/ts/updateDnsResponses.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/common/ts/updateDnsResponses.ts b/src/common/ts/updateDnsResponses.ts index 4508eb8b..7bdd5d78 100644 --- a/src/common/ts/updateDnsResponses.ts +++ b/src/common/ts/updateDnsResponses.ts @@ -41,11 +41,14 @@ export default async function updateDnsResponses() { } try { - const response = await fetch(`https://1.1.1.1/dns-query?name=${host}`, { - headers: { - Accept: "application/dns-json", - }, - }); + const response = await fetch( + `https://cloudflare-dns.com/dns-query?name=${host}`, + { + headers: { + Accept: "application/dns-json", + }, + } + ); const json = await response.json(); const { Answer } = json; if (!Array.isArray(Answer)) { From 562600170d2338b6354c51834a5b59d7d3b9e7e1 Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Wed, 31 Jan 2024 15:44:34 +0100 Subject: [PATCH 05/12] Bump version number --- package-lock.json | 4 ++-- package.json | 2 +- src/manifest.chromium.json | 2 +- src/manifest.firefox.json | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index ae3bd823..1ab85a68 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ttv-lol-pro", - "version": "2.3.3", + "version": "2.3.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ttv-lol-pro", - "version": "2.3.3", + "version": "2.3.4", "license": "GPL-3.0", "dependencies": { "bowser": "^2.11.0", diff --git a/package.json b/package.json index 55f9b0dd..c16706c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ttv-lol-pro", - "version": "2.3.3", + "version": "2.3.4", "description": "TTV LOL PRO removes most livestream ads from Twitch.", "@parcel/bundler-default": { "minBundles": 10000000, diff --git a/src/manifest.chromium.json b/src/manifest.chromium.json index 061d13b3..d1215b70 100644 --- a/src/manifest.chromium.json +++ b/src/manifest.chromium.json @@ -3,7 +3,7 @@ "name": "TTV LOL PRO", "description": "TTV LOL PRO removes most livestream ads from Twitch.", "homepage_url": "https://github.com/younesaassila/ttv-lol-pro", - "version": "2.3.3", + "version": "2.3.4", "background": { "service_worker": "background/background.ts", "type": "module" diff --git a/src/manifest.firefox.json b/src/manifest.firefox.json index e23cd64c..1471bcb6 100644 --- a/src/manifest.firefox.json +++ b/src/manifest.firefox.json @@ -3,7 +3,7 @@ "name": "TTV LOL PRO", "description": "TTV LOL PRO removes most livestream ads from Twitch.", "homepage_url": "https://github.com/younesaassila/ttv-lol-pro", - "version": "2.3.3", + "version": "2.3.4", "background": { "scripts": ["background/background.ts"], "persistent": false From 9ea14024e663013049ca4e6f997faaeebdfc248c Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Wed, 31 Jan 2024 18:45:07 +0100 Subject: [PATCH 06/12] Add level of control --- src/popup/popup.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/popup/popup.ts b/src/popup/popup.ts index 36c3b706..3e29c981 100644 --- a/src/popup/popup.ts +++ b/src/popup/popup.ts @@ -8,6 +8,7 @@ import { import { alpha2 } from "../common/ts/countryCodes"; import findChannelFromTwitchTvUrl from "../common/ts/findChannelFromTwitchTvUrl"; import isChannelWhitelisted from "../common/ts/isChannelWhitelisted"; +import isChromium from "../common/ts/isChromium"; import store from "../store"; import type { StreamStatus } from "../types"; @@ -183,6 +184,7 @@ copyDebugInfoButtonElement.addEventListener("click", async e => { channelNameLower != null ? store.state.streamStatuses[channelNameLower] : null; + const proxySettings = await browser.proxy.settings.get({}); const debugInfo = [ `**Debug Info**\n`, @@ -221,6 +223,9 @@ copyDebugInfoButtonElement.addEventListener("click", async e => { `- Country: ${status.proxyCountry ?? "N/A"}\n`, ].join("") : "", + isChromium + ? `Proxy level of control: ${proxySettings.levelOfControl}\n` + : "", ].join("") : "", store.state.adLog.length > 0 From 6a40669b48ebd5778a63f25f5d221efcd61929fc Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Wed, 31 Jan 2024 20:49:53 +0100 Subject: [PATCH 07/12] Better! --- src/common/ts/findChannelFromTwitchTvUrl.ts | 4 ++-- src/content/content.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/common/ts/findChannelFromTwitchTvUrl.ts b/src/common/ts/findChannelFromTwitchTvUrl.ts index dba40715..23956ed9 100644 --- a/src/common/ts/findChannelFromTwitchTvUrl.ts +++ b/src/common/ts/findChannelFromTwitchTvUrl.ts @@ -1,7 +1,7 @@ import { twitchChannelNameRegex } from "./regexes"; /** - * Returns the channel name from a Twitch.tv URL. + * Returns the channel name from a Twitch.tv URL in lowercase. * Returns `null` if the URL is not a valid Twitch.tv URL. * @param twitchTvUrl * @returns @@ -13,5 +13,5 @@ export default function findChannelFromTwitchTvUrl( const match = twitchChannelNameRegex.exec(twitchTvUrl); if (!match) return null; const [, channelName] = match; - return channelName; + return channelName.toLowerCase(); } diff --git a/src/content/content.ts b/src/content/content.ts index fc913b6a..2440ebb0 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -40,7 +40,8 @@ function injectPageScript() { function onStoreLoad() { // Clear stats for stream on page load/reload. - clearStats(findChannelFromTwitchTvUrl(location.href)); + const channelName = findChannelFromTwitchTvUrl(location.href); + clearStats(channelName); } /** From ac7c46054d854b83735f58a8f8832fab2b4254ac Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Thu, 1 Feb 2024 11:02:07 +0100 Subject: [PATCH 08/12] Add Passport option description --- src/options/page.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/options/page.html b/src/options/page.html index 65699831..abfbc7c8 100644 --- a/src/options/page.html +++ b/src/options/page.html @@ -31,6 +31,11 @@

Options

Passport

+ + Controls which types of requests are proxied. Increase the slider + only if you are seeing ads with the current setting. + +
Date: Fri, 2 Feb 2024 14:50:28 +0100 Subject: [PATCH 09/12] Fix #297 --- .../handlers/onContentScriptMessage.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/background/handlers/onContentScriptMessage.ts b/src/background/handlers/onContentScriptMessage.ts index d8310480..c58f8d67 100644 --- a/src/background/handlers/onContentScriptMessage.ts +++ b/src/background/handlers/onContentScriptMessage.ts @@ -6,6 +6,9 @@ import { MessageType, ProxyRequestType } from "../../types"; type Timeout = string | number | NodeJS.Timeout | undefined; const timeoutMap: Map = new Map(); +const fetchTimeoutMsOverride: Map = new Map([ + [ProxyRequestType.Usher, 7000], +]); export default function onContentScriptMessage( message: any, @@ -23,13 +26,15 @@ export default function onContentScriptMessage( } // Set new timeout for request type. - const fetchTimeoutMs = 4000; // Time for fetch to be called. + const fetchTimeoutMs = fetchTimeoutMsOverride.has(requestType) + ? fetchTimeoutMsOverride.get(requestType)! + : 3000; // Time for fetch to be called. const replyTimeoutMs = Date.now() - message.timestamp; // Time for reply to be received. timeoutMap.set( requestType, setTimeout(() => { console.log( - `[TTV LOL PRO] Disabling full mode (request type: ${requestType}, timeout)` + `Disabling full mode (request type: ${requestType}, timeout)` ); timeoutMap.delete(requestType); if (store.state.chromiumProxyActive) { @@ -42,7 +47,7 @@ export default function onContentScriptMessage( } console.log( - `[TTV LOL PRO] Enabled full mode for ${ + `Enabled full mode for ${ fetchTimeoutMs + replyTimeoutMs }ms (request type: ${requestType})` ); @@ -51,10 +56,7 @@ export default function onContentScriptMessage( type: MessageType.EnableFullModeResponse, }); } catch (error) { - console.error( - "[TTV LOL PRO] Failed to send EnableFullModeResponse message", - error - ); + console.error("Failed to send EnableFullModeResponse message", error); } } @@ -68,8 +70,6 @@ export default function onContentScriptMessage( if (store.state.chromiumProxyActive) { updateProxySettings([...timeoutMap.keys()]); } - console.log( - `[TTV LOL PRO] Disabled full mode (request type: ${requestType})` - ); + console.log(`Disabled full mode (request type: ${requestType})`); } } From 4fcc6bb37ac3ad6737be46781ce59a6a7140975c Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Fri, 2 Feb 2024 15:06:28 +0100 Subject: [PATCH 10/12] Add emojis --- src/background/handlers/onContentScriptMessage.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/background/handlers/onContentScriptMessage.ts b/src/background/handlers/onContentScriptMessage.ts index c58f8d67..e456e96a 100644 --- a/src/background/handlers/onContentScriptMessage.ts +++ b/src/background/handlers/onContentScriptMessage.ts @@ -34,7 +34,7 @@ export default function onContentScriptMessage( requestType, setTimeout(() => { console.log( - `Disabling full mode (request type: ${requestType}, timeout)` + `🔴 Disabled full mode (request type: ${requestType}, timeout)` ); timeoutMap.delete(requestType); if (store.state.chromiumProxyActive) { @@ -47,7 +47,7 @@ export default function onContentScriptMessage( } console.log( - `Enabled full mode for ${ + `🟢 Enabled full mode for ${ fetchTimeoutMs + replyTimeoutMs }ms (request type: ${requestType})` ); @@ -70,6 +70,6 @@ export default function onContentScriptMessage( if (store.state.chromiumProxyActive) { updateProxySettings([...timeoutMap.keys()]); } - console.log(`Disabled full mode (request type: ${requestType})`); + console.log(`🔴 Disabled full mode (request type: ${requestType})`); } } From ec25d9b5ce8846cbc0f0a422ff20e7192595120f Mon Sep 17 00:00:00 2001 From: younesaassila <47226184+younesaassila@users.noreply.github.com> Date: Fri, 2 Feb 2024 23:49:38 +0100 Subject: [PATCH 11/12] Improve description --- src/options/page.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/options/page.html b/src/options/page.html index abfbc7c8..9a068ccf 100644 --- a/src/options/page.html +++ b/src/options/page.html @@ -36,6 +36,11 @@

Passport

only if you are seeing ads with the current setting.
+ + If you are seeing "Commercial break in progress" ads, increasing the + slider will not help. + +
Date: Sat, 3 Feb 2024 11:58:17 +0100 Subject: [PATCH 12/12] Improve handler --- src/background/handlers/onContentScriptMessage.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/background/handlers/onContentScriptMessage.ts b/src/background/handlers/onContentScriptMessage.ts index e456e96a..3440bfba 100644 --- a/src/background/handlers/onContentScriptMessage.ts +++ b/src/background/handlers/onContentScriptMessage.ts @@ -7,7 +7,7 @@ type Timeout = string | number | NodeJS.Timeout | undefined; const timeoutMap: Map = new Map(); const fetchTimeoutMsOverride: Map = new Map([ - [ProxyRequestType.Usher, 7000], + [ProxyRequestType.Usher, 7000], // Account for slow page load. ]); export default function onContentScriptMessage( @@ -56,7 +56,7 @@ export default function onContentScriptMessage( type: MessageType.EnableFullModeResponse, }); } catch (error) { - console.error("Failed to send EnableFullModeResponse message", error); + console.error("❌ Failed to send EnableFullModeResponse message", error); } }