From c65b07724e96d17684dc00cc5afeadd40880647e Mon Sep 17 00:00:00 2001 From: Sean Patrick Wallace Date: Tue, 2 Jul 2024 19:07:55 +0200 Subject: [PATCH 1/5] Resolve merge conflict --- src/lib/is-enabled.ts | 16 +++++++++++----- src/sites/index.ts | 3 ++- src/sites/youtube.ts | 27 ++++++++++++++++++--------- src/store/index.ts | 1 - 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/lib/is-enabled.ts b/src/lib/is-enabled.ts index de42d48..8137d81 100644 --- a/src/lib/is-enabled.ts +++ b/src/lib/is-enabled.ts @@ -6,6 +6,14 @@ import { SiteStatus, } from '../background/store/sites/selectors'; +function pathMatches(path: string, patterns: string[]): boolean { + return patterns.some(pattern => { + // Convert wildcard patterns to regex to accomodate for wildcards in YouTube Shorts + const regexPattern = new RegExp(`^${pattern.replace(/\*/g, '.*')}$`); + return regexPattern.test(path); + }); +} + export type EnabledStatus = | { type: 'enabled' } | { type: 'disabled' } @@ -20,9 +28,9 @@ export function enabledStatus(state: SettingsState): EnabledStatus { for (let siteId of Object.keys(Sites)) { let site: Site = Sites[siteId]; const siteStatus: SiteStatus = siteStatuses[siteId]; - if (site.domain.find(domain => window.location.host.includes(domain)) != null) { - // Always disabled if the path doesn't match - if (site.paths.indexOf(window.location.pathname) === -1) { + if (window.location.host.includes(site.domain)) { + // Check if the current path is in the list of paths for this site + if (!pathMatches(window.location.pathname, site.paths)) { return { type: 'disabled' }; } @@ -31,10 +39,8 @@ export function enabledStatus(state: SettingsState): EnabledStatus { } else if (siteStatus.type === SiteStatusTag.DISABLED_TEMPORARILY) { return { type: 'disabled-temporarily', until: siteStatus.until }; } - return { type: 'enabled' }; } } - return { type: 'disabled' }; } diff --git a/src/sites/index.ts b/src/sites/index.ts index 872e5b2..d3e7057 100644 --- a/src/sites/index.ts +++ b/src/sites/index.ts @@ -12,6 +12,7 @@ export type SiteId = | 'instagram' | 'github'; + export const Sites: Record = { facebook: { label: 'Facebook', @@ -53,7 +54,7 @@ export const Sites: Record = { youtube: { label: 'YouTube', domain: ['youtube.com'], - paths: ['/', '/feed/trending'], + paths: ['/', '/feed/trending', '/shorts', '/shorts/*'], origins: ['https://www.youtube.com/*'], }, linkedin: { diff --git a/src/sites/youtube.ts b/src/sites/youtube.ts index fb7c05d..f35cd07 100644 --- a/src/sites/youtube.ts +++ b/src/sites/youtube.ts @@ -1,4 +1,6 @@ -import injectUI, { isAlreadyInjected } from '../lib/inject-ui'; +import injectUI, { + isAlreadyInjected, +} from '../lib/inject-ui'; import { isEnabled } from '../lib/is-enabled'; import { Store } from '../store'; @@ -8,26 +10,33 @@ export function checkSite(): boolean { export function eradicate(store: Store) { function eradicateRetry() { - const settings = store.getState().settings; + const state = store.getState(); + const settings = state.settings; if (settings == null || !isEnabled(settings)) { return; } - // Don't do anything if the UI hasn't loaded yet const feed = document.querySelector('#primary'); + const shorts = document.querySelector('#shorts-container'); - if (feed == null) { + // Don't do anything if the UI hasn't loaded yet + if (feed == null && shorts == null) { return; } - const container = feed; - - // Add News Feed Eradicator quote/info panel - if (container && !isAlreadyInjected()) { + if (feed || shorts) { // Hack so that injectUI can handle dark theme document.body.style.background = 'var(--yt-spec-general-background-a)'; - injectUI(container, store); + // Redirect the user to the main page and quote if they are on the shorts page + if (shorts) { + window.location.href = '/'; + } + + // Add News Feed Eradicator quote/info panel + if (feed && !isAlreadyInjected()) { + injectUI(feed, store); + } } } diff --git a/src/store/index.ts b/src/store/index.ts index 8177890..04b2be6 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -17,6 +17,5 @@ export function createStore(): Store { undefined, applyMiddleware(effectsMiddleware(rootEffect)) ); - return store; } From 2a48b7ab45c3f848b337c25cac77626c4a25f11e Mon Sep 17 00:00:00 2001 From: Sean Patrick Wallace Date: Tue, 2 Jul 2024 18:58:41 +0200 Subject: [PATCH 2/5] Resolve merge conflict --- src/background/background.ts | 69 ++++++++++++++++++++++++++++++++++++ src/lib/is-enabled.ts | 6 ++-- 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 src/background/background.ts diff --git a/src/background/background.ts b/src/background/background.ts new file mode 100644 index 0000000..a245040 --- /dev/null +++ b/src/background/background.ts @@ -0,0 +1,69 @@ +import { createBackgroundStore } from './store/store'; +import { getBrowser, TabId } from '../webextension'; +import {Site, Sites} from '../sites'; + +createBackgroundStore(); + +const browser = getBrowser(); +browser.browserAction.onClicked.addListener(() => { + browser.runtime.openOptionsPage(); +}); + +const tabMutex = new Set(); +const onTabChange = async (tabId: TabId) => { + // Ensures we're only running this listener once per tab simultaneously + if (tabMutex.has(tabId)) return; + tabMutex.add(tabId); + + try { + // Check first if the script has already been injected + const injectInfo = await getBrowser().tabs.executeScript<{ + host: string; + loaded: boolean; + }>(tabId, { + code: '({ host: window.location.host, loaded: document.nfeScriptsInjected || false });', + runAt: 'document_start', + }); + + if ( + injectInfo != null && + injectInfo[0] != null && + injectInfo[0].loaded !== true + ) { + // Inject them scripts (and CSS) + browser.tabs.insertCSS(tabId, { + file: 'eradicate.css', + runAt: 'document_start', + }); + + // Site specific CSS + for (let siteKey in Sites) { + const site: Site = Sites[siteKey]; + if (site.domain.find(domain => injectInfo[0]?.host.endsWith(domain)) != null) { + const css = site.css; + if (css != null) { + browser.tabs.insertCSS(tabId, { + code: css, + runAt: 'document_start', + }); + } + } + } + + browser.tabs.executeScript(tabId, { + file: 'intercept.js', + runAt: 'document_start', + }); + await browser.tabs.executeScript(tabId, { + code: 'document.nfeScriptsInjected = true;', + runAt: 'document_start', + }); + } else { + // already injected or unavailable + } + } finally { + tabMutex.delete(tabId); + } +}; + +browser.tabs.onUpdated.addListener(onTabChange); diff --git a/src/lib/is-enabled.ts b/src/lib/is-enabled.ts index 8137d81..579f231 100644 --- a/src/lib/is-enabled.ts +++ b/src/lib/is-enabled.ts @@ -28,9 +28,9 @@ export function enabledStatus(state: SettingsState): EnabledStatus { for (let siteId of Object.keys(Sites)) { let site: Site = Sites[siteId]; const siteStatus: SiteStatus = siteStatuses[siteId]; - if (window.location.host.includes(site.domain)) { - // Check if the current path is in the list of paths for this site - if (!pathMatches(window.location.pathname, site.paths)) { + if (site.domain.find(domain => window.location.host.includes(domain)) != null) { + // Always disabled if the path doesn't match + if (site.paths.indexOf(window.location.pathname) === -1) { return { type: 'disabled' }; } From 8c318d6c9a9cafe3548a61f3552df34f7851f451 Mon Sep 17 00:00:00 2001 From: Sean Patrick Wallace Date: Sun, 30 Jun 2024 16:21:35 +0200 Subject: [PATCH 3/5] Rebase --- src/lib/is-enabled.ts | 58 +++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/lib/is-enabled.ts b/src/lib/is-enabled.ts index 579f231..6a46629 100644 --- a/src/lib/is-enabled.ts +++ b/src/lib/is-enabled.ts @@ -1,46 +1,46 @@ import { Sites, Site } from '../sites'; import { SettingsState } from '../background/store/reducer'; import { - getSiteStatus, - SiteStatusTag, - SiteStatus, + getSiteStatus, + SiteStatusTag, + SiteStatus, } from '../background/store/sites/selectors'; function pathMatches(path: string, patterns: string[]): boolean { return patterns.some(pattern => { - // Convert wildcard patterns to regex to accomodate for wildcards in YouTube Shorts - const regexPattern = new RegExp(`^${pattern.replace(/\*/g, '.*')}$`); - return regexPattern.test(path); + // Convert wildcard patterns to regex to accommodate for wildcards in YouTube Shorts + const regexPattern = new RegExp(`^${pattern.replace(/\*/g, '.*')}$`); + return regexPattern.test(path); }); } export type EnabledStatus = - | { type: 'enabled' } - | { type: 'disabled' } - | { type: 'disabled-temporarily'; until: number }; + | { type: 'enabled' } + | { type: 'disabled' } + | { type: 'disabled-temporarily'; until: number }; export function isEnabled(state: SettingsState): boolean { - return enabledStatus(state).type === 'enabled'; + return enabledStatus(state).type === 'enabled'; } export function enabledStatus(state: SettingsState): EnabledStatus { - const siteStatuses = getSiteStatus(state); - for (let siteId of Object.keys(Sites)) { - let site: Site = Sites[siteId]; - const siteStatus: SiteStatus = siteStatuses[siteId]; - if (site.domain.find(domain => window.location.host.includes(domain)) != null) { - // Always disabled if the path doesn't match - if (site.paths.indexOf(window.location.pathname) === -1) { - return { type: 'disabled' }; - } - - if (siteStatus.type === SiteStatusTag.DISABLED) { - return { type: 'disabled' }; - } else if (siteStatus.type === SiteStatusTag.DISABLED_TEMPORARILY) { - return { type: 'disabled-temporarily', until: siteStatus.until }; - } - return { type: 'enabled' }; - } - } - return { type: 'disabled' }; + const siteStatuses = getSiteStatus(state); + for (let siteId of Object.keys(Sites)) { + let site: Site = Sites[siteId]; + const siteStatus: SiteStatus = siteStatuses[siteId]; + // Check if the current host matches any domain in the site's domain array + if (site.domain.some(domain => window.location.host.includes(domain))) { + // Check if the current path is in the list of paths for this site + if (!pathMatches(window.location.pathname, site.paths)) { + return { type: 'disabled' }; + } + if (siteStatus.type === SiteStatusTag.DISABLED) { + return { type: 'disabled' }; + } else if (siteStatus.type === SiteStatusTag.DISABLED_TEMPORARILY) { + return { type: 'disabled-temporarily', until: siteStatus.until }; + } + return { type: 'enabled' }; + } + } + return { type: 'disabled' }; } From f51acf1b215be524748261a444264bd265e58d7a Mon Sep 17 00:00:00 2001 From: Sean Patrick Wallace Date: Tue, 2 Jul 2024 18:56:34 +0200 Subject: [PATCH 4/5] Alter redirect behavior --- package-lock.json | 4 ++-- src/sites/youtube.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6213a06..e9b32ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "news-feed-eradicator", - "version": "2.2.7", + "version": "2.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "news-feed-eradicator", - "version": "2.2.7", + "version": "2.3.0", "license": "MIT", "dependencies": { "@types/node": "^8.10.36" diff --git a/src/sites/youtube.ts b/src/sites/youtube.ts index f35cd07..fc70cc3 100644 --- a/src/sites/youtube.ts +++ b/src/sites/youtube.ts @@ -30,7 +30,8 @@ export function eradicate(store: Store) { // Redirect the user to the main page and quote if they are on the shorts page if (shorts) { - window.location.href = '/'; + history.pushState({}, '', '/'); + window.dispatchEvent(new Event('popstate')); } // Add News Feed Eradicator quote/info panel From 084cc14235b4858e770086c24b9db703dd502035 Mon Sep 17 00:00:00 2001 From: Sean Patrick Wallace Date: Tue, 2 Jul 2024 19:18:27 +0200 Subject: [PATCH 5/5] Remove incorrectly merge background.ts --- src/background/background.ts | 69 ------------------------------------ 1 file changed, 69 deletions(-) delete mode 100644 src/background/background.ts diff --git a/src/background/background.ts b/src/background/background.ts deleted file mode 100644 index a245040..0000000 --- a/src/background/background.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { createBackgroundStore } from './store/store'; -import { getBrowser, TabId } from '../webextension'; -import {Site, Sites} from '../sites'; - -createBackgroundStore(); - -const browser = getBrowser(); -browser.browserAction.onClicked.addListener(() => { - browser.runtime.openOptionsPage(); -}); - -const tabMutex = new Set(); -const onTabChange = async (tabId: TabId) => { - // Ensures we're only running this listener once per tab simultaneously - if (tabMutex.has(tabId)) return; - tabMutex.add(tabId); - - try { - // Check first if the script has already been injected - const injectInfo = await getBrowser().tabs.executeScript<{ - host: string; - loaded: boolean; - }>(tabId, { - code: '({ host: window.location.host, loaded: document.nfeScriptsInjected || false });', - runAt: 'document_start', - }); - - if ( - injectInfo != null && - injectInfo[0] != null && - injectInfo[0].loaded !== true - ) { - // Inject them scripts (and CSS) - browser.tabs.insertCSS(tabId, { - file: 'eradicate.css', - runAt: 'document_start', - }); - - // Site specific CSS - for (let siteKey in Sites) { - const site: Site = Sites[siteKey]; - if (site.domain.find(domain => injectInfo[0]?.host.endsWith(domain)) != null) { - const css = site.css; - if (css != null) { - browser.tabs.insertCSS(tabId, { - code: css, - runAt: 'document_start', - }); - } - } - } - - browser.tabs.executeScript(tabId, { - file: 'intercept.js', - runAt: 'document_start', - }); - await browser.tabs.executeScript(tabId, { - code: 'document.nfeScriptsInjected = true;', - runAt: 'document_start', - }); - } else { - // already injected or unavailable - } - } finally { - tabMutex.delete(tabId); - } -}; - -browser.tabs.onUpdated.addListener(onTabChange);