Skip to content

Commit

Permalink
Remove unused imports and declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
mschae23 committed Jun 22, 2024
1 parent 3a48266 commit 756e17f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 39 deletions.
2 changes: 1 addition & 1 deletion maze-utils
41 changes: 19 additions & 22 deletions src/titles/titleRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { VideoID, getVideoID } from "../../maze-utils/src/video";
import Config, { TitleFormatting } from "../config/config";
import Config from "../config/config";
import { getVideoTitleIncludingUnsubmitted } from "../dataFetching";
import { logError } from "../utils/logger";
import { MobileFix, addNodeToListenFor, getOrCreateTitleButtonContainer } from "../utils/titleBar";
import { BrandingLocation, ShowCustomBrandingInfo, extractVideoIDFromElement, getActualShowCustomBranding, toggleShowCustom } from "../videoBranding/videoBranding";
import { cleanEmojis, formatTitle } from "./titleFormatter";
import { formatTitle } from "./titleFormatter";
import { setCurrentVideoTitle } from "./pageTitleHandler";
import { getTitleFormatting, shouldCleanEmojis, shouldDefaultToCustom, shouldReplaceTitles, shouldReplaceTitlesFastCheck, shouldUseCrowdsourcedTitles } from "../config/channelOverrides";
import { shouldDefaultToCustom, shouldReplaceTitles, shouldReplaceTitlesFastCheck, shouldUseCrowdsourcedTitles } from "../config/channelOverrides";
import { countTitleReplacement } from "../config/stats";
import { onMobile } from "../../maze-utils/src/pageInfo";
import { isFirefoxOrSafari } from "../../maze-utils/src";
Expand All @@ -28,13 +28,11 @@ export async function replaceTitle(element: HTMLElement, videoID: VideoID, showC
showOriginalTitle(element, brandingLocation);
return false;
}

if (brandingLocation === BrandingLocation.Watch) {
const currentWatchPageType = document.URL.includes("watch") ? WatchPageType.Video : WatchPageType.Miniplayer;

console.log("replacing", videoID, lastWatchVideoID, originalTitleElement.textContent, lastWatchTitle, currentWatchPageType, lastUrlWatchPageType, element)

if (lastWatchVideoID && originalTitleElement?.textContent
if (lastWatchVideoID && originalTitleElement?.textContent
&& videoID !== lastWatchVideoID && originalTitleElement.textContent === lastWatchTitle
&& lastUrlWatchPageType === currentWatchPageType) {
// Don't reset it if it hasn't changed videos yet, will be handled by title change listener
Expand Down Expand Up @@ -74,21 +72,20 @@ export async function replaceTitle(element: HTMLElement, videoID: VideoID, showC
if (!await isOnCorrectVideo(element, brandingLocation, videoID)) return false;

const title = titleData?.title;
const originalTitle = originalTitleElement?.textContent?.trim?.() ?? "";
if (title && await shouldUseCrowdsourcedTitles(videoID)) {
const formattedTitle = await formatTitle(title, true, videoID);
if (!await isOnCorrectVideo(element, brandingLocation, videoID)) return false;

if (originalTitleElement?.textContent
if (originalTitleElement?.textContent
&& originalTitleElement.textContent.trim() === formattedTitle) {
showOriginalTitle(element, brandingLocation);
return false;
}

if (onMobile()) {
hideOriginalTitle(element, brandingLocation);
}

setCustomTitle(formattedTitle, element, brandingLocation);
countTitleReplacement(videoID);
} else if (originalTitleElement?.textContent) {
Expand Down Expand Up @@ -137,7 +134,7 @@ export async function replaceTitle(element: HTMLElement, videoID: VideoID, showC
}

async function isOnCorrectVideo(element: HTMLElement, brandingLocation: BrandingLocation, videoID: VideoID): Promise<boolean> {
return brandingLocation === BrandingLocation.Watch ? getVideoID() === videoID
return brandingLocation === BrandingLocation.Watch ? getVideoID() === videoID
: await extractVideoIDFromElement(element, brandingLocation) === videoID;
}

Expand All @@ -156,7 +153,7 @@ function hideOriginalTitle(element: HTMLElement, brandingLocation: BrandingLocat
function showOriginalTitle(element: HTMLElement, brandingLocation: BrandingLocation) {
const originalTitleElement = getOriginalTitleElement(element, brandingLocation);
const titleElement = getOrCreateTitleElement(element, brandingLocation, originalTitleElement);

titleElement.style.display = "none";
if (!originalTitleElement.classList.contains("ta-title-container")) {
originalTitleElement.style.setProperty("display", "-webkit-box", "important");
Expand Down Expand Up @@ -265,7 +262,7 @@ function getTitleSelector(brandingLocation: BrandingLocation): string[] {
switch (brandingLocation) {
case BrandingLocation.Watch:
return [
"yt-formatted-string",
"yt-formatted-string",
".ytp-title-link.yt-uix-sessionlink",
".yt-core-attributed-string"
];
Expand Down Expand Up @@ -301,15 +298,15 @@ function getTitleSelector(brandingLocation: BrandingLocation): string[] {
}

export function getOrCreateTitleElement(element: HTMLElement, brandingLocation: BrandingLocation, originalTitleElement?: HTMLElement): HTMLElement {
return element.querySelector(".cbCustomTitle") as HTMLElement ??
return element.querySelector(".cbCustomTitle") as HTMLElement ??
createTitleElement(element, originalTitleElement ?? getOriginalTitleElement(element, brandingLocation), brandingLocation);
}

function createTitleElement(element: HTMLElement, originalTitleElement: HTMLElement, brandingLocation: BrandingLocation): HTMLElement {
const titleElement = brandingLocation !== BrandingLocation.Watch
const titleElement = brandingLocation !== BrandingLocation.Watch
|| originalTitleElement.classList.contains("miniplayer-title")
|| originalTitleElement.classList.contains("ytp-title-link")
? originalTitleElement.cloneNode() as HTMLElement
? originalTitleElement.cloneNode() as HTMLElement
: document.createElement("span");
titleElement.classList.add("cbCustomTitle");

Expand Down Expand Up @@ -413,7 +410,7 @@ export async function hideAndUpdateShowOriginalButton(element: HTMLElement, bran
buttonElement.title = chrome.i18n.getMessage("ShowModified");
}

const isDefault = showCustomBranding.knownValue === null
const isDefault = showCustomBranding.knownValue === null
|| showCustomBranding.knownValue === showCustomBranding.originalValue;
if (isDefault
&& brandingLocation !== BrandingLocation.Watch
Expand All @@ -429,15 +426,15 @@ export async function hideAndUpdateShowOriginalButton(element: HTMLElement, bran
}

export async function findShowOriginalButton(originalTitleElement: HTMLElement, brandingLocation: BrandingLocation): Promise<HTMLElement> {
const referenceNode = brandingLocation === BrandingLocation.Watch
const referenceNode = brandingLocation === BrandingLocation.Watch
? (await getOrCreateTitleButtonContainer()) : originalTitleElement.parentElement;
return referenceNode?.querySelector?.(".cbShowOriginal") as HTMLElement;
}

export async function findOrCreateShowOriginalButton(element: HTMLElement, brandingLocation: BrandingLocation,
videoID: VideoID): Promise<HTMLElement> {
const originalTitleElement = getOriginalTitleElement(element, brandingLocation);
const buttonElement = await findShowOriginalButton(originalTitleElement, brandingLocation)
const buttonElement = await findShowOriginalButton(originalTitleElement, brandingLocation)
?? await createShowOriginalButton(originalTitleElement, brandingLocation, videoID);

buttonElement.setAttribute("videoID", videoID);
Expand Down Expand Up @@ -466,7 +463,7 @@ async function createShowOriginalButton(originalTitleElement: HTMLElement,
}

buttonElement.classList.add("cbButton");
if (brandingLocation === BrandingLocation.Watch
if (brandingLocation === BrandingLocation.Watch
|| Config.config!.alwaysShowShowOriginalButton) {
buttonElement.classList.add("cbDontHide");
}
Expand Down Expand Up @@ -549,7 +546,7 @@ async function createShowOriginalButton(originalTitleElement: HTMLElement,

if (brandingLocation === BrandingLocation.Watch) {
const referenceNode = await getOrCreateTitleButtonContainer();

// Verify again it doesn't already exist
const existingButton = referenceNode?.querySelector?.(".cbShowOriginal");
if (existingButton) {
Expand Down
28 changes: 13 additions & 15 deletions src/videoBranding/videoBranding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Config, { ThumbnailCacheOption } from "../config/config";
import { logError } from "../utils/logger";
import { getVideoTitleIncludingUnsubmitted } from "../dataFetching";
import { handleOnboarding } from "./onboarding";
import { cleanEmojis, cleanResultingTitle } from "../titles/titleFormatter";
import { shouldDefaultToCustom, shouldDefaultToCustomFastCheck, shouldUseCrowdsourcedTitles } from "../config/channelOverrides";
import { onMobile } from "../../maze-utils/src/pageInfo";
import { addMaxTitleLinesCssToPage } from "../utils/cssInjector";
Expand Down Expand Up @@ -48,8 +47,8 @@ export interface VideoBrandingInstance {
updateBrandingCallbacks: Array<() => Promise<void>>;
}

export const brandingBoxSelector = !onMobile()
? "ytd-rich-grid-media, ytd-video-renderer, ytd-movie-renderer, ytd-compact-video-renderer, ytd-compact-radio-renderer, ytd-compact-movie-renderer, ytd-playlist-video-renderer, ytd-playlist-panel-video-renderer, ytd-grid-video-renderer, ytd-grid-movie-renderer, ytd-rich-grid-slim-media, ytd-radio-renderer, ytd-reel-item-renderer, ytd-compact-playlist-renderer, ytd-playlist-renderer, ytd-grid-playlist-renderer, ytd-grid-show-renderer, ytd-structured-description-video-lockup-renderer, ytd-hero-playlist-thumbnail-renderer"
export const brandingBoxSelector = !onMobile()
? "ytd-rich-grid-media, ytd-video-renderer, ytd-movie-renderer, ytd-compact-video-renderer, ytd-compact-radio-renderer, ytd-compact-movie-renderer, ytd-playlist-video-renderer, ytd-playlist-panel-video-renderer, ytd-grid-video-renderer, ytd-grid-movie-renderer, ytd-rich-grid-slim-media, ytd-radio-renderer, ytd-reel-item-renderer, ytd-compact-playlist-renderer, ytd-playlist-renderer, ytd-grid-playlist-renderer, ytd-grid-show-renderer, ytd-structured-description-video-lockup-renderer"
: "ytm-video-with-context-renderer, ytm-compact-radio-renderer, ytm-reel-item-renderer, ytm-channel-featured-video-renderer, ytm-compact-video-renderer, ytm-playlist-video-renderer, .playlist-immersive-header-content, ytm-compact-playlist-renderer, ytm-video-card-renderer, ytm-vertical-list-renderer, ytm-playlist-panel-video-renderer";

export const watchPageThumbnailSelector = ".ytp-cued-thumbnail-overlay";
Expand All @@ -66,7 +65,7 @@ export async function replaceCurrentVideoBranding(): Promise<[boolean, boolean]>
const possibleSelectors = getPossibleSelectors(onWatchPage, onEmbedPage);

// Find first invisible one, or wait for the first one to be visible
const mainTitle = possibleSelectors.map((selector) => getElement(selector.selector, selector.checkVisibility, true) as HTMLElement).filter((element) => isVisible(element, true))[0] ||
const mainTitle = possibleSelectors.map((selector) => getElement(selector.selector, selector.checkVisibility, true) as HTMLElement).filter((element) => isVisible(element, true))[0] ||
await waitForElement(possibleSelectors[0].selector, !onClipPage, true) as HTMLElement;
const titles = (possibleSelectors.map((selector) => getElement(selector.selector, selector.checkVisibility && !onClipPage, true)).filter((e) => !!e)) as HTMLElement[];
const promises: [Promise<boolean>, Promise<boolean>] = [Promise.resolve(false), Promise.resolve(false)]
Expand All @@ -79,7 +78,7 @@ export async function replaceCurrentVideoBranding(): Promise<[boolean, boolean]>
const showCustomBranding = videoBrandingInstance.showCustomBranding;

// Replace each title and return true only if all true
promises[0] = Promise.all(titles.map((title) =>
promises[0] = Promise.all(titles.map((title) =>
replaceTitle(title, videoID, showCustomBranding, brandingLocation)))
.then((results) => results.every((result) => result));

Expand Down Expand Up @@ -175,8 +174,8 @@ export async function replaceVideoCardBranding(element: HTMLElement, brandingLoc
knownValue: false,
originalValue: false
} : showCustomBranding);
const titlePromise = !isPlaylistOrClipTitleStatus
? replaceTitle(element, videoID, showCustomBranding, brandingLocation)
const titlePromise = !isPlaylistOrClipTitleStatus
? replaceTitle(element, videoID, showCustomBranding, brandingLocation)
: Promise.resolve(false);

if (isPlaylistOrClipTitleStatus) {
Expand Down Expand Up @@ -261,7 +260,7 @@ async function extractVideoID(link: HTMLAnchorElement) {
await waitForImageSrc(image);
href = image.getAttribute("src");
}

if (href) {
videoID = href.match(/\/vi\/(\S{11})/)?.[1] as VideoID;
}
Expand All @@ -274,15 +273,15 @@ async function extractVideoID(link: HTMLAnchorElement) {

export async function extractVideoIDFromElement(element: HTMLElement, brandingLocation: BrandingLocation): Promise<VideoID | null> {
const link = getLinkElement(element, brandingLocation);
if (link) {
if (link) {
return await extractVideoID(link);
} else {
return null;
}
}

function isPlaylistOrClipTitle(element: HTMLElement, link: HTMLAnchorElement) {
return (link.href?.match(/list=/)?.[0] !== undefined
return (link.href?.match(/list=/)?.[0] !== undefined
&& link.href?.match(/index=/)?.[0] === undefined)
|| link.href?.match(/\/clip\//)?.[0] !== undefined;
}
Expand All @@ -296,14 +295,13 @@ export async function handleShowOriginalButton(element: HTMLElement, videoID: Vi
const result = await Promise.race(promises);
if (result || (await Promise.all(promises)).some((r) => r)) {
const title = await getVideoTitleIncludingUnsubmitted(videoID, brandingLocation);
const originalTitle = getOriginalTitleElement(element, brandingLocation)?.textContent;
const customTitle = title && !title.original
&& await shouldUseCrowdsourcedTitles(videoID);

if (!customTitle && !Config.config!.showIconForFormattedTitles && !await promises[1]) {
return;
}

const button = await findOrCreateShowOriginalButton(element, brandingLocation, videoID);
const image = button.querySelector("img") as HTMLImageElement;
if (image) {
Expand Down Expand Up @@ -360,7 +358,7 @@ export async function setShowCustom(videoID: VideoID, value: boolean): Promise<v
* If a video is currently at the default state, it will be updated to it's newest state
*/
async function updateCurrentlyDefaultShowCustom(videoID: VideoID): Promise<void> {
if (videoBrandingInstances[videoID]
if (videoBrandingInstances[videoID]
&& [null, videoBrandingInstances[videoID].showCustomBranding.originalValue]
.includes(videoBrandingInstances[videoID].showCustomBranding.knownValue)) {

Expand Down Expand Up @@ -444,7 +442,7 @@ export function setupOptionChangeListener(): void {
}
}

if (changes.titleMaxLines
if (changes.titleMaxLines
&& changes.titleMaxLines.newValue !== changes.titleMaxLines.oldValue) {
addMaxTitleLinesCssToPage();
}
Expand Down Expand Up @@ -483,7 +481,7 @@ function waitForImageSrc(image: HTMLImageElement): Promise<void> {
}

export function getActualShowCustomBranding(showCustomBranding: ShowCustomBrandingInfo): Promise<boolean> {
return showCustomBranding.knownValue === null
return showCustomBranding.knownValue === null
? showCustomBranding.actualValue
: Promise.resolve(showCustomBranding.knownValue);
}

0 comments on commit 756e17f

Please sign in to comment.