diff --git a/background.js b/background.js index d60dfb84..171b5f39 100644 --- a/background.js +++ b/background.js @@ -265,11 +265,5 @@ function sendRequestToServer(type, address, callback) { xmlhttp.send(); } -function getYouTubeVideoID(url) { // Return video id or false - var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; - var match = url.match(regExp); - return (match && match[7].length == 11) ? match[7] : false; -} - //uuid generator function from https://gist.github.com/jed/982883 function generateUUID(a){return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,generateUUID)} diff --git a/content.js b/content.js index 73348973..656b0e83 100644 --- a/content.js +++ b/content.js @@ -1143,26 +1143,3 @@ function sendRequestToCustomServer(type, fullAddress, callback) { //submit this request xmlhttp.send(); } - -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; -} - -//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; -} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 2391ccb5..544b9501 100644 --- a/manifest.json +++ b/manifest.json @@ -11,6 +11,7 @@ "all_frames": true, "js": [ "config.js", + "utils.js", "content.js", "popup.js" ], @@ -46,6 +47,7 @@ }, "background": { "scripts":[ + "utils.js", "config.js", "background.js" ], diff --git a/popup.html b/popup.html index be45142b..30195294 100644 --- a/popup.html +++ b/popup.html @@ -193,5 +193,6 @@ + - \ No newline at end of file + diff --git a/popup.js b/popup.js index 8368f274..d1186984 100644 --- a/popup.js +++ b/popup.js @@ -1127,13 +1127,6 @@ function runThePopup() { xmlhttp.send(); } - 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"); - return (match && match[7].length == 11) ? id : false; - } - //end of function } @@ -1143,4 +1136,4 @@ if (chrome.tabs != undefined) { //this means it is actually opened in the popup runThePopup(); -} \ No newline at end of file +} diff --git a/utils.js b/utils.js new file mode 100644 index 00000000..1a2374b7 --- /dev/null +++ b/utils.js @@ -0,0 +1,36 @@ +function getYouTubeVideoID(url) { + //Attempt to parse url + try { + let urlObject = new URL(url); + } catch (e) { + console.error("[SB] Unable to parse URL: " + url); + return false + } + + //Check if valid hostname + if(!["www.youtube.com","www.youtube-nocookie.com"].includes(urlObject.host)) return false; + + //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; + } else if (urlObject.pathname.startsWith("/embed/")) { + try { + return urlObject.pathname.substr(7, 11); + } catch (e) { + console.error("[SB] Video ID not valid for " + url); + return false; + } + } +} + +//returns the start time of the video if there was one specified (ex. ?t=5s) +function getYouTubeVideoStartTime(url) { + let searchParams = new URL(url).searchParams; + let startTime = searchParams.get("t"); + if (startTime == null) { + startTime = searchParams.get("time_continue"); + } + + return startTime; +}