generated from JohnBra/vite-web-extension
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa4bcfe
commit 84f457f
Showing
12 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { hideShorts, observeShortsElements, showShorts } from "@/src/features/hideShorts/utils"; | ||
import { waitForSpecificMessage } from "@/src/utils/utilities"; | ||
let shortsObserver: MutationObserver | null = null; | ||
export async function enableHideShorts() { | ||
// Wait for the "options" message from the content script | ||
const optionsData = await waitForSpecificMessage("options", "request_data", "content"); | ||
const { | ||
data: { | ||
options: { enable_hide_shorts } | ||
} | ||
} = optionsData; | ||
// If the hide shorts option is disabled, return | ||
if (!enable_hide_shorts) return; | ||
hideShorts(); | ||
// Observe changes to the short sections and hides them | ||
shortsObserver = observeShortsElements(); | ||
} | ||
|
||
export function disableHideShorts() { | ||
showShorts(); | ||
// Disconnect the observer | ||
if (shortsObserver) { | ||
shortsObserver.disconnect(); | ||
shortsObserver = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
const sideBarOpenedShortsButtonSelector = "ytd-guide-entry-renderer:has(a[title=Shorts])"; | ||
const sideBarClosedShortsButtonSelector = "ytd-guide-entry-renderer:has(a[title=Shorts])"; | ||
const homePageShortsSectionSelector = "ytd-rich-section-renderer:has(#rich-shelf-header)"; | ||
const channelHomePageShortsSectionSelector = "ytd-reel-shelf-renderer:has(#title-container)"; | ||
const channelPageShortsTabSelector = "yt-tab-shape[tab-title=Shorts]"; | ||
const searchResultsShortsTabSelector = "yt-chip-cloud-chip-renderer:has(yt-formatted-string[title=Shorts])"; | ||
const shortsVideoRendererSelector = "ytd-video-renderer:has([overlay-style=SHORTS])"; | ||
|
||
type ElementVisibilityAction = (element: HTMLElement) => void; | ||
|
||
function toggleElementVisibility(selector: string, action: ElementVisibilityAction) { | ||
const elements = document.querySelectorAll<HTMLDivElement>(selector); | ||
if (elements.length === 0) return; | ||
elements.forEach((element) => action(element)); | ||
} | ||
|
||
function hideElement(element: HTMLElement) { | ||
element.classList.add("yte-hide-shorts"); | ||
} | ||
|
||
function showElement(element: HTMLElement) { | ||
element.classList.remove("yte-hide-shorts"); | ||
} | ||
|
||
function hideSideBarShortsButton() { | ||
toggleElementVisibility(sideBarOpenedShortsButtonSelector, hideElement); | ||
toggleElementVisibility(sideBarClosedShortsButtonSelector, hideElement); | ||
} | ||
|
||
function showSideBarShortsButton() { | ||
toggleElementVisibility(sideBarOpenedShortsButtonSelector, showElement); | ||
toggleElementVisibility(sideBarClosedShortsButtonSelector, showElement); | ||
} | ||
function hideShortsTabOnSearchResultsPage() { | ||
toggleElementVisibility(searchResultsShortsTabSelector, hideElement); | ||
} | ||
function showShortsTabOnSearchResultsPage() { | ||
toggleElementVisibility(searchResultsShortsTabSelector, showElement); | ||
} | ||
function hideShortsSectionOnHomePage() { | ||
toggleElementVisibility(homePageShortsSectionSelector, hideElement); | ||
} | ||
|
||
function showShortsSectionOnHomePage() { | ||
toggleElementVisibility(homePageShortsSectionSelector, showElement); | ||
} | ||
|
||
function hideShortsSectionOnChannelHomePage() { | ||
toggleElementVisibility(channelHomePageShortsSectionSelector, hideElement); | ||
} | ||
|
||
function showShortsSectionOnChannelHomePage() { | ||
toggleElementVisibility(channelHomePageShortsSectionSelector, showElement); | ||
} | ||
|
||
function hideShortsTabOnChannelPage() { | ||
toggleElementVisibility(channelPageShortsTabSelector, hideElement); | ||
} | ||
|
||
function showShortsTabOnChannelPage() { | ||
toggleElementVisibility(channelPageShortsTabSelector, showElement); | ||
} | ||
function hideShortsVideoRenderers() { | ||
toggleElementVisibility(shortsVideoRendererSelector, hideElement); | ||
} | ||
function showShortsVideoRenderers() { | ||
toggleElementVisibility(shortsVideoRendererSelector, showElement); | ||
} | ||
export function hideShorts() { | ||
// Hide the shorts tab on the channel page | ||
hideShortsTabOnChannelPage(); | ||
// Hide the shorts tab on the search results page | ||
hideShortsTabOnSearchResultsPage(); | ||
// Hide the shorts section on the homepage | ||
hideShortsSectionOnHomePage(); | ||
// Hide the shorts section on the channel home page | ||
hideShortsSectionOnChannelHomePage(); | ||
// Hide the shorts sidebar items | ||
hideSideBarShortsButton(); | ||
// Hide the shorts video renderers | ||
hideShortsVideoRenderers(); | ||
} | ||
export function showShorts() { | ||
// Show the shorts sidebar items | ||
showSideBarShortsButton(); | ||
// Show the shorts section on the homepage | ||
showShortsSectionOnHomePage(); | ||
// Show the shorts section on the channel home page | ||
showShortsSectionOnChannelHomePage(); | ||
// Show the shorts tab on the channel page | ||
showShortsTabOnChannelPage(); | ||
// Show the shorts tab on the search results page | ||
showShortsTabOnSearchResultsPage(); | ||
// Show the shorts video renderers | ||
showShortsVideoRenderers(); | ||
} | ||
|
||
export function observeShortsElements() { | ||
const observerOptions: MutationObserverInit = { | ||
childList: true, | ||
subtree: true | ||
}; | ||
|
||
const observer = new MutationObserver((mutations) => { | ||
// Check if any mutation contains one of the specified selectors | ||
const containsShortsSelector = mutations.some((mutation) => { | ||
return ( | ||
mutation.target instanceof Element && | ||
(mutation.target.matches(sideBarOpenedShortsButtonSelector) || | ||
mutation.target.matches(sideBarClosedShortsButtonSelector) || | ||
mutation.target.matches(homePageShortsSectionSelector) || | ||
mutation.target.matches(channelHomePageShortsSectionSelector) || | ||
mutation.target.matches(channelPageShortsTabSelector) || | ||
mutation.target.matches(searchResultsShortsTabSelector) || | ||
mutation.target.matches(shortsVideoRendererSelector)) | ||
); | ||
}); | ||
|
||
// Only call hideShorts if one of the mutations contains one of the selectors | ||
if (containsShortsSelector) { | ||
hideShorts(); | ||
} | ||
}); | ||
|
||
observer.observe(document.body, observerOptions); | ||
|
||
// Return the observer so it can be disconnected later | ||
return observer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters