mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-11-10 09:07:45 +01:00
Return null on fails in getYouTubeVideoID and add type annotations to videoIDChange
This commit is contained in:
parent
b2ef9e5d6e
commit
48cfee57b7
1 changed files with 13 additions and 13 deletions
|
@ -384,7 +384,7 @@ function resetValues() {
|
||||||
categoryPill?.setVisibility(false);
|
categoryPill?.setVisibility(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function videoIDChange(id): Promise<void> {
|
async function videoIDChange(id: string): Promise<void> {
|
||||||
// don't switch to invalid value
|
// don't switch to invalid value
|
||||||
if (!id && sponsorVideoID && !document?.URL?.includes("youtube.com/clip/")) return;
|
if (!id && sponsorVideoID && !document?.URL?.includes("youtube.com/clip/")) return;
|
||||||
//if the id has not changed return unless the video element has changed
|
//if the id has not changed return unless the video element has changed
|
||||||
|
@ -441,7 +441,7 @@ async function videoIDChange(id): Promise<void> {
|
||||||
//close popup
|
//close popup
|
||||||
closeInfoMenu();
|
closeInfoMenu();
|
||||||
|
|
||||||
sponsorsLookup(id);
|
sponsorsLookup();
|
||||||
|
|
||||||
// Make sure all player buttons are properly added
|
// Make sure all player buttons are properly added
|
||||||
updateVisibilityOfPlayerControlsButton();
|
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;
|
url ||= document.URL;
|
||||||
// pageType shortcut
|
// 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.
|
// 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/
|
// skip to document and don't hide if on /embed/
|
||||||
if (url.includes("/embed/") && url.includes("youtube.com")) return getYouTubeVideoIDFromDocument(false, PageType.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
|
// 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);
|
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']";
|
const selector = "a.ytp-title-link[data-sessionlink='feature=player-title']";
|
||||||
// get ID from document (channel trailer / embedded playlist)
|
// get ID from document (channel trailer / embedded playlist)
|
||||||
const element = pageHint === PageType.Embed ? document.querySelector(selector)
|
const element = pageHint === PageType.Embed ? document.querySelector(selector)
|
||||||
|
@ -1247,11 +1247,11 @@ function getYouTubeVideoIDFromDocument(hideIcon = true, pageHint = PageType.Watc
|
||||||
pageType = pageHint;
|
pageType = pageHint;
|
||||||
return getYouTubeVideoIDFromURL(videoURL);
|
return getYouTubeVideoIDFromURL(videoURL);
|
||||||
} else {
|
} 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("#", "");
|
if(url.startsWith("https://www.youtube.com/tv#/")) url = url.replace("#", "");
|
||||||
|
|
||||||
//Attempt to parse url
|
//Attempt to parse url
|
||||||
|
@ -1260,7 +1260,7 @@ function getYouTubeVideoIDFromURL(url: string): string | boolean {
|
||||||
urlObject = new URL(url);
|
urlObject = new URL(url);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("[SB] Unable to parse URL: " + url);
|
console.error("[SB] Unable to parse URL: " + url);
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if valid hostname
|
// Check if valid hostname
|
||||||
|
@ -1274,7 +1274,7 @@ function getYouTubeVideoIDFromURL(url: string): string | boolean {
|
||||||
utils.wait(() => Config.config !== null).then(() => videoIDChange(getYouTubeVideoIDFromURL(url)));
|
utils.wait(() => Config.config !== null).then(() => videoIDChange(getYouTubeVideoIDFromURL(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
onInvidious = false;
|
onInvidious = false;
|
||||||
}
|
}
|
||||||
|
@ -1282,17 +1282,17 @@ function getYouTubeVideoIDFromURL(url: string): string | boolean {
|
||||||
//Get ID from searchParam
|
//Get ID from searchParam
|
||||||
if (urlObject.searchParams.has("v") && ["/watch", "/watch/"].includes(urlObject.pathname) || urlObject.pathname.startsWith("/tv/watch")) {
|
if (urlObject.searchParams.has("v") && ["/watch", "/watch/"].includes(urlObject.pathname) || urlObject.pathname.startsWith("/tv/watch")) {
|
||||||
const id = urlObject.searchParams.get("v");
|
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/")) {
|
} else if (urlObject.pathname.startsWith("/embed/") || urlObject.pathname.startsWith("/shorts/")) {
|
||||||
try {
|
try {
|
||||||
const id = urlObject.pathname.split("/")[2]
|
const id = urlObject.pathname.split("/")[2]
|
||||||
if (id?.length >=11 ) return id.slice(0, 11);
|
if (id?.length >=11 ) return id.slice(0, 11);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("[SB] Video ID not valid for " + url);
|
console.error("[SB] Video ID not valid for " + url);
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue