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 01/17] 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; +} From aae099842663eb200e4939f5c53cb0411baad59d Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Sun, 4 Aug 2019 18:59:08 +0100 Subject: [PATCH 02/17] Added utils.js --- manifest.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/manifest.json b/manifest.json index 1fe36a7c..be3a2dc7 100644 --- a/manifest.json +++ b/manifest.json @@ -11,6 +11,7 @@ "all_frames": true, "js": [ "config.js", + "utils.js", "content.js", "popup.js" ], @@ -48,6 +49,7 @@ }, "background": { "scripts":[ + "utils.js", "config.js", "background.js" ] From 5425c54fca2889fba9e59f862e869f492512195c Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Sun, 4 Aug 2019 18:59:51 +0100 Subject: [PATCH 03/17] Added utils.js --- popup.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 + From 64befaebfcaee74d36de58dbcd2cba5c0fc4c9b0 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Sun, 4 Aug 2019 19:00:54 +0100 Subject: [PATCH 04/17] Removed getYouTubeVideoID as in utils --- background.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/background.js b/background.js index cbd45531..3ab8f8e9 100644 --- a/background.js +++ b/background.js @@ -263,11 +263,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)} \ No newline at end of file +function generateUUID(a){return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,generateUUID)} From e6fa832cb86593499f4c9d3f219d776710d8fd3a Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Sun, 4 Aug 2019 19:04:43 +0100 Subject: [PATCH 05/17] Removed getYouTubeVideoID as in utils --- content.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/content.js b/content.js index 73348973..77260012 100644 --- a/content.js +++ b/content.js @@ -1144,18 +1144,6 @@ function sendRequestToCustomServer(type, fullAddress, callback) { 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; @@ -1165,4 +1153,4 @@ function getYouTubeVideoStartTime(url) { } return startTime; -} \ No newline at end of file +} From 5ee279dec7d074dd266c23892ff27b118f5bb504 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Sun, 4 Aug 2019 19:06:07 +0100 Subject: [PATCH 06/17] Added getYouTubeVideoStartTime --- utils.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/utils.js b/utils.js index c2803729..1e4da828 100644 --- a/utils.js +++ b/utils.js @@ -9,3 +9,14 @@ function getYouTubeVideoID(url) { // Returns with video id else returns false 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; +} From d25792f39a86017529746338436efb10c7bab444 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Sun, 4 Aug 2019 19:07:00 +0100 Subject: [PATCH 07/17] Removed getYouTubeVideoID --- popup.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) 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 +} From 9a32710ef84161e73146ccaa17610c9462433fef Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Sun, 4 Aug 2019 19:09:46 +0100 Subject: [PATCH 08/17] Removed getYouTubeVideoStartTime --- content.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/content.js b/content.js index 77260012..656b0e83 100644 --- a/content.js +++ b/content.js @@ -1143,14 +1143,3 @@ function sendRequestToCustomServer(type, fullAddress, callback) { //submit this request xmlhttp.send(); } - -//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; -} From 7ad5e426fb965d4a06575377704244bd3c4dd2f3 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Tue, 6 Aug 2019 10:27:13 +0100 Subject: [PATCH 09/17] Improved Parser --- utils.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/utils.js b/utils.js index 1e4da828..c12fb6cb 100644 --- a/utils.js +++ b/utils.js @@ -2,12 +2,10 @@ 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 + if (url.includes("/embed/")) { //it is an embed, don't search for v id = match[7]; } - - return (match && match[7].length == 11) ? id : false; + return (match) ? id : false; } //returns the start time of the video if there was one specified (ex. ?t=5s) From 2b5402fa5797f95e53ffd98647c758791de1dfe1 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Tue, 6 Aug 2019 14:08:46 +0100 Subject: [PATCH 10/17] Added sanity check --- utils.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/utils.js b/utils.js index c12fb6cb..916c2c73 100644 --- a/utils.js +++ b/utils.js @@ -1,11 +1,8 @@ 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) ? id : false; + var id = url.includes("/embed/") ? match[7] : new URL(url).searchParams.get("v"); + return (match && id.length === 11) ? id : false; } //returns the start time of the video if there was one specified (ex. ?t=5s) 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 11/17] 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) From c6c8d7de491281e38d21c0d9124bdb0e6c1d7644 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Wed, 7 Aug 2019 15:34:21 +0100 Subject: [PATCH 12/17] Update utils.js --- utils.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/utils.js b/utils.js index c1d6c23d..5e470e27 100644 --- a/utils.js +++ b/utils.js @@ -1,9 +1,21 @@ 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; + + try { // Attempt to parse url + let obj = new URL(url); + } catch (e) { + return false + console.error("[SB] Unable to parser URL"); + } + + if(!["www.youtube.com","www.youtube-nocookie.com"].includes(obj.host)) return false // Check if valid hostname + + if (obj.pathname == "/watch" && obj.searchParams.has("v")) { + id = obj.searchParams.get("v"); // Get ID from searchParam + } else if (obj.pathname.startsWith("/embed/")) { + id = obj.pathname.slice("/embed/".length); // Get ID from end or URL + } + + return id.length == 11 ? id : false; // If ID is not 11 in length return false } //returns the start time of the video if there was one specified (ex. ?t=5s) From dce036b0e6bc7a5347b41665c55eb3e77202ca8f Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Wed, 7 Aug 2019 15:46:07 +0100 Subject: [PATCH 13/17] Update utils.js --- utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils.js b/utils.js index 5e470e27..117c1e61 100644 --- a/utils.js +++ b/utils.js @@ -2,9 +2,9 @@ function getYouTubeVideoID(url) { try { // Attempt to parse url let obj = new URL(url); - } catch (e) { - return false + } catch (e) { console.error("[SB] Unable to parser URL"); + return false } if(!["www.youtube.com","www.youtube-nocookie.com"].includes(obj.host)) return false // Check if valid hostname From baba619fe74fd750dc0c65cf191b5ea0251ad596 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Wed, 7 Aug 2019 16:20:07 +0100 Subject: [PATCH 14/17] Fixed scope --- utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils.js b/utils.js index 117c1e61..f7fb6ecf 100644 --- a/utils.js +++ b/utils.js @@ -1,7 +1,7 @@ function getYouTubeVideoID(url) { try { // Attempt to parse url - let obj = new URL(url); + var obj = new URL(url); } catch (e) { console.error("[SB] Unable to parser URL"); return false From b964d93ea948b94515a7a93dc573c5ab5a52be3c Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Wed, 7 Aug 2019 16:35:29 +0100 Subject: [PATCH 15/17] Update utils.js --- utils.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/utils.js b/utils.js index f7fb6ecf..80b4a1f3 100644 --- a/utils.js +++ b/utils.js @@ -11,11 +11,15 @@ function getYouTubeVideoID(url) { if (obj.pathname == "/watch" && obj.searchParams.has("v")) { id = obj.searchParams.get("v"); // Get ID from searchParam + return id.length == 11 ? id : false; } else if (obj.pathname.startsWith("/embed/")) { - id = obj.pathname.slice("/embed/".length); // Get ID from end or URL + try { + return obj.pathname.substr(7, 11); + } catch (e) { + console.error("[SB] Video ID not valid"); + return false + } } - - return id.length == 11 ? id : false; // If ID is not 11 in length return false } //returns the start time of the video if there was one specified (ex. ?t=5s) From ccafbf663c0cd3c3800a7fd70ece4b65a39a3cda Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 10 Aug 2019 19:51:08 -0400 Subject: [PATCH 16/17] Fixed typos. Co-author worked on creating this url parser. Co-Authored-By: Giacomo Rossetto --- utils.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/utils.js b/utils.js index 80b4a1f3..2134c1af 100644 --- a/utils.js +++ b/utils.js @@ -1,13 +1,15 @@ function getYouTubeVideoID(url) { - try { // Attempt to parse url + //Attempt to parse url + try { var obj = new URL(url); } catch (e) { console.error("[SB] Unable to parser URL"); return false } - if(!["www.youtube.com","www.youtube-nocookie.com"].includes(obj.host)) return false // Check if valid hostname + //Check if valid hostname + if(!["www.youtube.com","www.youtube-nocookie.com"].includes(obj.host)) return false; if (obj.pathname == "/watch" && obj.searchParams.has("v")) { id = obj.searchParams.get("v"); // Get ID from searchParam @@ -17,7 +19,7 @@ function getYouTubeVideoID(url) { return obj.pathname.substr(7, 11); } catch (e) { console.error("[SB] Video ID not valid"); - return false + return false; } } } From 5be8ecb32b3a69ebd083686d3b711f5e38131c2d Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 10 Aug 2019 19:55:39 -0400 Subject: [PATCH 17/17] 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 --- utils.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/utils.js b/utils.js index 2134c1af..1a2374b7 100644 --- a/utils.js +++ b/utils.js @@ -1,24 +1,24 @@ function getYouTubeVideoID(url) { - //Attempt to parse url try { - var obj = new URL(url); + let urlObject = new URL(url); } catch (e) { - console.error("[SB] Unable to parser URL"); + console.error("[SB] Unable to parse URL: " + url); return false } //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")) { - id = obj.searchParams.get("v"); // Get ID from searchParam + //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 (obj.pathname.startsWith("/embed/")) { + } else if (urlObject.pathname.startsWith("/embed/")) { try { - return obj.pathname.substr(7, 11); + return urlObject.pathname.substr(7, 11); } catch (e) { - console.error("[SB] Video ID not valid"); + console.error("[SB] Video ID not valid for " + url); return false; } } @@ -27,7 +27,7 @@ function getYouTubeVideoID(url) { //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"); + let startTime = searchParams.get("t"); if (startTime == null) { startTime = searchParams.get("time_continue"); }