2019-08-20 18:46:05 +02:00
|
|
|
// Function that can be used to wait for a condition before returning
|
|
|
|
async function wait(condition, timeout = 5000, check = 100) {
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {reject("TIMEOUT")}, timeout);
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
let result = condition();
|
|
|
|
if (result !== false) {
|
|
|
|
resolve(result);
|
|
|
|
clearInterval(interval);
|
|
|
|
};
|
|
|
|
}, check);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-21 20:04:05 +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-23 18:19:44 +02:00
|
|
|
return false;
|
2019-08-24 02:50:10 +02:00
|
|
|
}
|