From 02e11503cba1ee420a338e200e1a873ff71d081e Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Tue, 6 Aug 2019 22:06:23 +0100 Subject: [PATCH] Removed Regex as "looks bad" --- utils.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/utils.js b/utils.js index 916c2c73..c1d6c23d 100644 --- a/utils.js +++ b/utils.js @@ -1,8 +1,9 @@ -function getYouTubeVideoID(url) { // Returns with video id else returns false - var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; - var match = url.match(regExp); - var id = url.includes("/embed/") ? match[7] : new URL(url).searchParams.get("v"); - return (match && id.length === 11) ? id : false; +function getYouTubeVideoID(url) { + let obj = new URL(url); + if(obj.host !== "www.youtube.com" || "www.youtube-nocookie.com") return false // Check if valid hostname + if (obj.pathname == "/watch") id = obj.searchParams.get("v"); // Get ID from searchParam + if (obj.pathname.startsWith("/embed/")) id = obj.pathname.slice("/embed/".length); // Get ID from end or URL + return id.length == 11 ? id : false; } //returns the start time of the video if there was one specified (ex. ?t=5s)