SponsorBlock/utils.js

37 lines
1 KiB
JavaScript
Raw Normal View History

2019-08-06 23:06:23 +02:00
function getYouTubeVideoID(url) {
2019-08-07 16:34:21 +02:00
//Attempt to parse url
try {
2019-08-07 17:20:07 +02:00
var obj = new URL(url);
2019-08-07 16:46:07 +02:00
} catch (e) {
2019-08-07 16:34:21 +02:00
console.error("[SB] Unable to parser 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(obj.host)) return false;
2019-08-07 16:34:21 +02:00
if (obj.pathname == "/watch" && obj.searchParams.has("v")) {
id = obj.searchParams.get("v"); // Get ID from searchParam
2019-08-07 17:35:29 +02:00
return id.length == 11 ? id : false;
2019-08-07 16:34:21 +02:00
} else if (obj.pathname.startsWith("/embed/")) {
2019-08-07 17:35:29 +02:00
try {
return obj.pathname.substr(7, 11);
} catch (e) {
console.error("[SB] Video ID not valid");
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;
var startTime = searchParams.get("t");
if (startTime == null) {
startTime = searchParams.get("time_continue");
}
return startTime;
}