mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-11-10 09:07:45 +01:00
5be8ecb32b
Co-author worked on creating this url parser. Co-Authored-By: Giacomo Rossetto <jackyman_cs4@live.it>
36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
function getYouTubeVideoID(url) {
|
|
//Attempt to parse url
|
|
try {
|
|
let urlObject = new URL(url);
|
|
} catch (e) {
|
|
console.error("[SB] Unable to parse URL: " + url);
|
|
return false
|
|
}
|
|
|
|
//Check if valid hostname
|
|
if(!["www.youtube.com","www.youtube-nocookie.com"].includes(urlObject.host)) return false;
|
|
|
|
//Get ID from searchParam
|
|
if ((urlObject.pathname == "/watch" || urlObject.pathname == "/watch/") && urlObject.searchParams.has("v")) {
|
|
id = urlObject.searchParams.get("v");
|
|
return id.length == 11 ? id : false;
|
|
} else if (urlObject.pathname.startsWith("/embed/")) {
|
|
try {
|
|
return urlObject.pathname.substr(7, 11);
|
|
} catch (e) {
|
|
console.error("[SB] Video ID not valid for " + url);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
//returns the start time of the video if there was one specified (ex. ?t=5s)
|
|
function getYouTubeVideoStartTime(url) {
|
|
let searchParams = new URL(url).searchParams;
|
|
let startTime = searchParams.get("t");
|
|
if (startTime == null) {
|
|
startTime = searchParams.get("time_continue");
|
|
}
|
|
|
|
return startTime;
|
|
}
|