From 48cfee57b72430dfec0f5f2d65cd60153b3d39aa Mon Sep 17 00:00:00 2001 From: mini-bomba <55105495+mini-bomba@users.noreply.github.com> Date: Sat, 8 Oct 2022 16:40:11 +0200 Subject: [PATCH] Return null on fails in getYouTubeVideoID and add type annotations to videoIDChange --- src/content.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/content.ts b/src/content.ts index 4cc55e6e..714da296 100644 --- a/src/content.ts +++ b/src/content.ts @@ -384,7 +384,7 @@ function resetValues() { categoryPill?.setVisibility(false); } -async function videoIDChange(id): Promise { +async function videoIDChange(id: string): Promise { // don't switch to invalid value if (!id && sponsorVideoID && !document?.URL?.includes("youtube.com/clip/")) return; //if the id has not changed return unless the video element has changed @@ -441,7 +441,7 @@ async function videoIDChange(id): Promise { //close popup closeInfoMenu(); - sponsorsLookup(id); + sponsorsLookup(); // Make sure all player buttons are properly added updateVisibilityOfPlayerControlsButton(); @@ -1219,12 +1219,12 @@ function startSkipScheduleCheckingForStartSponsors() { } } -function getYouTubeVideoID(document: Document, url?: string): string | boolean { +function getYouTubeVideoID(document: Document, url?: string): string { url ||= document.URL; // pageType shortcut - if (pageType === PageType.Channel) return getYouTubeVideoIDFromDocument() + if (pageType === PageType.Channel) return getYouTubeVideoIDFromDocument(); // clips should never skip, going from clip to full video has no indications. - if (url.includes("youtube.com/clip/")) return false; + if (url.includes("youtube.com/clip/")) return null; // skip to document and don't hide if on /embed/ if (url.includes("/embed/") && url.includes("youtube.com")) return getYouTubeVideoIDFromDocument(false, PageType.Embed); // skip to URL if matches youtube watch or invidious or matches youtube pattern @@ -1235,7 +1235,7 @@ function getYouTubeVideoID(document: Document, url?: string): string | boolean { return getYouTubeVideoIDFromURL(url) || getYouTubeVideoIDFromDocument(false); } -function getYouTubeVideoIDFromDocument(hideIcon = true, pageHint = PageType.Watch): string | boolean { +function getYouTubeVideoIDFromDocument(hideIcon = true, pageHint = PageType.Watch): string { const selector = "a.ytp-title-link[data-sessionlink='feature=player-title']"; // get ID from document (channel trailer / embedded playlist) const element = pageHint === PageType.Embed ? document.querySelector(selector) @@ -1247,11 +1247,11 @@ function getYouTubeVideoIDFromDocument(hideIcon = true, pageHint = PageType.Watc pageType = pageHint; return getYouTubeVideoIDFromURL(videoURL); } else { - return false; + return null; } } -function getYouTubeVideoIDFromURL(url: string): string | boolean { +function getYouTubeVideoIDFromURL(url: string): string { if(url.startsWith("https://www.youtube.com/tv#/")) url = url.replace("#", ""); //Attempt to parse url @@ -1260,7 +1260,7 @@ function getYouTubeVideoIDFromURL(url: string): string | boolean { urlObject = new URL(url); } catch (e) { console.error("[SB] Unable to parse URL: " + url); - return false; + return null; } // Check if valid hostname @@ -1274,7 +1274,7 @@ function getYouTubeVideoIDFromURL(url: string): string | boolean { utils.wait(() => Config.config !== null).then(() => videoIDChange(getYouTubeVideoIDFromURL(url))); } - return false; + return null; } else { onInvidious = false; } @@ -1282,17 +1282,17 @@ function getYouTubeVideoIDFromURL(url: string): string | boolean { //Get ID from searchParam if (urlObject.searchParams.has("v") && ["/watch", "/watch/"].includes(urlObject.pathname) || urlObject.pathname.startsWith("/tv/watch")) { const id = urlObject.searchParams.get("v"); - return id.length == 11 ? id : false; + return id.length == 11 ? id : null; } else if (urlObject.pathname.startsWith("/embed/") || urlObject.pathname.startsWith("/shorts/")) { try { const id = urlObject.pathname.split("/")[2] if (id?.length >=11 ) return id.slice(0, 11); } catch (e) { console.error("[SB] Video ID not valid for " + url); - return false; + return null; } } - return false; + return null; } /**