From f6c9e8e235bfdaa3d1c89c584aaa40fa3897a41f Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Sun, 4 Aug 2019 18:47:07 +0100 Subject: [PATCH] Added getYouTubeVideoID --- utils.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 utils.js diff --git a/utils.js b/utils.js new file mode 100644 index 00000000..c2803729 --- /dev/null +++ b/utils.js @@ -0,0 +1,11 @@ +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 = new URL(url).searchParams.get("v"); + if (url.includes("/embed/")) { + //it is an embed, don't search for v + id = match[7]; + } + + return (match && match[7].length == 11) ? id : false; +}