2019-08-06 23:06:23 +02:00
|
|
|
function getYouTubeVideoID(url) {
|
2019-08-11 01:51:08 +02:00
|
|
|
//Attempt to parse url
|
2019-08-11 02:53:43 +02:00
|
|
|
let urlObject = null;
|
2019-08-11 01:51:08 +02:00
|
|
|
try {
|
2019-08-11 02:53:43 +02:00
|
|
|
urlObject = new URL(url);
|
2019-08-07 16:46:07 +02:00
|
|
|
} catch (e) {
|
2019-08-11 01:55:39 +02:00
|
|
|
console.error("[SB] Unable to parse URL: " + url);
|
2019-08-07 16:46:07 +02:00
|
|
|
return false
|
2019-08-07 16:34:21 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 01:51:08 +02:00
|
|
|
//Check if valid hostname
|
2019-08-11 01:55:39 +02:00
|
|
|
if(!["www.youtube.com","www.youtube-nocookie.com"].includes(urlObject.host)) return false;
|
2019-08-07 16:34:21 +02:00
|
|
|
|
2019-08-11 01:55:39 +02:00
|
|
|
//Get ID from searchParam
|
|
|
|
if ((urlObject.pathname == "/watch" || urlObject.pathname == "/watch/") && urlObject.searchParams.has("v")) {
|
|
|
|
id = urlObject.searchParams.get("v");
|
2019-08-07 17:35:29 +02:00
|
|
|
return id.length == 11 ? id : false;
|
2019-08-11 01:55:39 +02:00
|
|
|
} else if (urlObject.pathname.startsWith("/embed/")) {
|
2019-08-07 17:35:29 +02:00
|
|
|
try {
|
2019-08-11 01:55:39 +02:00
|
|
|
return urlObject.pathname.substr(7, 11);
|
2019-08-07 17:35:29 +02:00
|
|
|
} catch (e) {
|
2019-08-11 01:55:39 +02:00
|
|
|
console.error("[SB] Video ID not valid for " + url);
|
2019-08-11 01:51:08 +02:00
|
|
|
return false;
|
2019-08-07 17:35:29 +02:00
|
|
|
}
|
2019-08-07 16:34:21 +02:00
|
|
|
}
|
2019-08-04 19:47:07 +02:00
|
|
|
}
|
2019-08-04 20:06:07 +02:00
|
|
|
|
|
|
|
//returns the start time of the video if there was one specified (ex. ?t=5s)
|
|
|
|
function getYouTubeVideoStartTime(url) {
|
|
|
|
let searchParams = new URL(url).searchParams;
|
2019-08-11 01:55:39 +02:00
|
|
|
let startTime = searchParams.get("t");
|
2019-08-04 20:06:07 +02:00
|
|
|
if (startTime == null) {
|
|
|
|
startTime = searchParams.get("time_continue");
|
|
|
|
}
|
|
|
|
|
|
|
|
return startTime;
|
|
|
|
}
|