SponsorBlock/utils.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-08-06 23:06:23 +02:00
function getYouTubeVideoID(url) {
//Attempt to parse url
try {
let urlObject = new URL(url);
2019-08-07 16:46:07 +02:00
} catch (e) {
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
}
//Check if valid hostname
if(!["www.youtube.com","www.youtube-nocookie.com"].includes(urlObject.host)) return false;
2019-08-07 16:34:21 +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;
} else if (urlObject.pathname.startsWith("/embed/")) {
2019-08-07 17:35:29 +02:00
try {
return urlObject.pathname.substr(7, 11);
2019-08-07 17:35:29 +02:00
} catch (e) {
console.error("[SB] Video ID not valid for " + url);
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;
let startTime = searchParams.get("t");
2019-08-04 20:06:07 +02:00
if (startTime == null) {
startTime = searchParams.get("time_continue");
}
return startTime;
}