mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-07 20:17:05 +03:00
Fixed up formatting and style. Added more detailed error messages. Changed from var to let.
Co-author worked on creating this url parser. Co-Authored-By: Giacomo Rossetto <jackyman_cs4@live.it>
This commit is contained in:
20
utils.js
20
utils.js
@@ -1,24 +1,24 @@
|
|||||||
function getYouTubeVideoID(url) {
|
function getYouTubeVideoID(url) {
|
||||||
|
|
||||||
//Attempt to parse url
|
//Attempt to parse url
|
||||||
try {
|
try {
|
||||||
var obj = new URL(url);
|
let urlObject = new URL(url);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("[SB] Unable to parser URL");
|
console.error("[SB] Unable to parse URL: " + url);
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check if valid hostname
|
//Check if valid hostname
|
||||||
if(!["www.youtube.com","www.youtube-nocookie.com"].includes(obj.host)) return false;
|
if(!["www.youtube.com","www.youtube-nocookie.com"].includes(urlObject.host)) return false;
|
||||||
|
|
||||||
if (obj.pathname == "/watch" && obj.searchParams.has("v")) {
|
//Get ID from searchParam
|
||||||
id = obj.searchParams.get("v"); // 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;
|
return id.length == 11 ? id : false;
|
||||||
} else if (obj.pathname.startsWith("/embed/")) {
|
} else if (urlObject.pathname.startsWith("/embed/")) {
|
||||||
try {
|
try {
|
||||||
return obj.pathname.substr(7, 11);
|
return urlObject.pathname.substr(7, 11);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("[SB] Video ID not valid");
|
console.error("[SB] Video ID not valid for " + url);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,7 @@ function getYouTubeVideoID(url) {
|
|||||||
//returns the start time of the video if there was one specified (ex. ?t=5s)
|
//returns the start time of the video if there was one specified (ex. ?t=5s)
|
||||||
function getYouTubeVideoStartTime(url) {
|
function getYouTubeVideoStartTime(url) {
|
||||||
let searchParams = new URL(url).searchParams;
|
let searchParams = new URL(url).searchParams;
|
||||||
var startTime = searchParams.get("t");
|
let startTime = searchParams.get("t");
|
||||||
if (startTime == null) {
|
if (startTime == null) {
|
||||||
startTime = searchParams.get("time_continue");
|
startTime = searchParams.get("time_continue");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user