From ec1644c72845f27d4a76efba9197975d8d975279 Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Fri, 20 May 2016 22:09:34 +0200 Subject: [PATCH 01/12] Closes #193 Add support for linking to a specific time position --- js/everything.js | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/js/everything.js b/js/everything.js index 812a7fce..f96e351f 100644 --- a/js/everything.js +++ b/js/everything.js @@ -162,6 +162,13 @@ var ZenPlayer = { that.videoDescription = that.getVideoDescription(videoID); that.videoUrl = plyrPlayer.plyr.embed.getVideoUrl(); + // Updates the time position by a given argument in URL + // IE https://zenplayer.audio/?v=koJv-j1usoI&t=30 starts at 0:30 + var t = getCurrentTimePosition(); + if(t){ + window.sessionStorage[videoID] = t; + } + // Initialize UI that.setupTitle(); that.setupVideoDescription(); @@ -357,6 +364,11 @@ function getCurrentVideoID() { return v; } +function getCurrentTimePosition() { + var t = getParameterByName(window.location.search, "t"); + return t; +} + function getCurrentSearchQuery() { var q = getParameterByName(window.location.search, "q"); return q; @@ -368,12 +380,11 @@ function removeSearchQueryFromURL(url) { } return url; } - function makeListenURL(videoID) { + var url = removeSearchQueryFromURL(window.location.href); // Remove any #s which break functionality url = url.replace("#", ""); - return url + "?v=" + videoID; } @@ -438,8 +449,15 @@ $(function() { // How do we know if the value is truly invalid? // Preload the form from the URL var currentVideoID = getCurrentVideoID(); + var currentVideoPosition = getCurrentTimePosition(); + if(currentVideoPosition){ + currentVideoPosition = "&t=" + currentVideoPosition; + } + else{ + currentVideoPosition = ""; + } if (currentVideoID) { - $("#v").attr("value", currentVideoID); + $("#v").attr("value", currentVideoID + currentVideoPosition); } else { var currentSearchQuery = getCurrentSearchQuery(); @@ -490,13 +508,17 @@ $(function() { // Handle form submission $("#form").submit(function(event) { event.preventDefault(); - var formValue = $.trim($("#v").val()); + var formValueTime = formValue.match(/(&t=\d*)$/g); + if(formValueTime){ + formValueTime = formValueTime[0]; + currentVideoPosition = formValueTime; + formValue = formValue.replace(/(&t=\d*)$/g, ""); + } if (formValue) { var videoID = wrapParseYouTubeVideoID(formValue, true); ga("send", "event", "form submitted", videoID); sendKeenEvent("Form submitted", {videoID: videoID}); - if (isFileProtocol()) { errorMessage.show("Skipping video lookup request as we're running the site locally."); } @@ -509,13 +531,17 @@ $(function() { key: youTubeDataApiKey, part: "snippet", fields: "items/snippet/description", - id: videoID + id: videoID, + startSeconds:50 }, success: function(data) { if (data.items.length === 0) { window.location.href = makeSearchURL(formValue); } else { + if(formValueTime){ + videoID += formValueTime; + } window.location.href = makeListenURL(videoID); } } @@ -550,7 +576,6 @@ $(function() { sendKeenEvent("demo", {action: "already had video ID in URL"}); } }); - // Load the player ZenPlayer.init(currentVideoID); }); From bd8073ff9d0f06db383d9f2784fea82ad67ff6c1 Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Sat, 21 May 2016 03:29:06 +0200 Subject: [PATCH 02/12] updated --- js/everything.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/js/everything.js b/js/everything.js index c8b66da8..c8fb5851 100644 --- a/js/everything.js +++ b/js/everything.js @@ -380,12 +380,16 @@ function removeSearchQueryFromURL(url) { } return url; } -function makeListenURL(videoID) { - +function makeListenURL(videoID,videoPosition) { var url = removeSearchQueryFromURL(window.location.href); // Remove any #s which break functionality url = url.replace("#", ""); - return url + "?v=" + videoID; + if(videoPosition){ + return url + "?v=" + videoID + videoPosition; + } + else { + return url + "?v=" + videoID; + } } function makeSearchURL(searchQuery) { @@ -512,7 +516,6 @@ $(function() { var formValueTime = formValue.match(/(&t=\d*)$/g); if(formValueTime){ formValueTime = formValueTime[0]; - currentVideoPosition = formValueTime; formValue = formValue.replace(/(&t=\d*)$/g, ""); } if (formValue) { @@ -531,18 +534,14 @@ $(function() { key: youTubeDataApiKey, part: "snippet", fields: "items/snippet/description", - id: videoID, - startSeconds:50 + id: videoID }, success: function(data) { if (data.items.length === 0) { window.location.href = makeSearchURL(formValue); } else { - if(formValueTime){ - videoID += formValueTime; - } - window.location.href = makeListenURL(videoID); + window.location.href = makeListenURL(videoID,formValueTime); } } }).fail(function(jqXHR, textStatus, errorThrown) { From b5a438f9a7fc4af21bcaf7c3b2d4566fcff32eff Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Sat, 21 May 2016 03:36:14 +0200 Subject: [PATCH 03/12] update for test --- js/everything.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/js/everything.js b/js/everything.js index c8fb5851..3294b99a 100644 --- a/js/everything.js +++ b/js/everything.js @@ -165,7 +165,7 @@ var ZenPlayer = { // Updates the time position by a given argument in URL // IE https://zenplayer.audio/?v=koJv-j1usoI&t=30 starts at 0:30 var t = getCurrentTimePosition(); - if(t){ + if (t) { window.sessionStorage[videoID] = t; } @@ -384,7 +384,7 @@ function makeListenURL(videoID,videoPosition) { var url = removeSearchQueryFromURL(window.location.href); // Remove any #s which break functionality url = url.replace("#", ""); - if(videoPosition){ + if (videoPosition) { return url + "?v=" + videoID + videoPosition; } else { @@ -454,10 +454,10 @@ $(function() { // Preload the form from the URL var currentVideoID = getCurrentVideoID(); var currentVideoPosition = getCurrentTimePosition(); - if(currentVideoPosition){ + if (currentVideoPosition) { currentVideoPosition = "&t=" + currentVideoPosition; } - else{ + else { currentVideoPosition = ""; } if (currentVideoID) { @@ -514,7 +514,7 @@ $(function() { event.preventDefault(); var formValue = $.trim($("#v").val()); var formValueTime = formValue.match(/(&t=\d*)$/g); - if(formValueTime){ + if (formValueTime) { formValueTime = formValueTime[0]; formValue = formValue.replace(/(&t=\d*)$/g, ""); } From 06b54432e2720e43f2a206dc03145871ed8f1f83 Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Sat, 21 May 2016 17:08:34 +0200 Subject: [PATCH 04/12] minor fixes --- js/everything.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/js/everything.js b/js/everything.js index 3294b99a..b97a8164 100644 --- a/js/everything.js +++ b/js/everything.js @@ -385,11 +385,12 @@ function makeListenURL(videoID,videoPosition) { // Remove any #s which break functionality url = url.replace("#", ""); if (videoPosition) { - return url + "?v=" + videoID + videoPosition; + videoPosition = "&t=" + videoPosition; } else { - return url + "?v=" + videoID; + videoPosition = ""; } + return url + "?v=" + videoID + videoPosition; } function makeSearchURL(searchQuery) { @@ -453,15 +454,8 @@ $(function() { // How do we know if the value is truly invalid? // Preload the form from the URL var currentVideoID = getCurrentVideoID(); - var currentVideoPosition = getCurrentTimePosition(); - if (currentVideoPosition) { - currentVideoPosition = "&t=" + currentVideoPosition; - } - else { - currentVideoPosition = ""; - } if (currentVideoID) { - $("#v").attr("value", currentVideoID + currentVideoPosition); + $("#v").attr("value", currentVideoID); } else { var currentSearchQuery = getCurrentSearchQuery(); @@ -515,7 +509,7 @@ $(function() { var formValue = $.trim($("#v").val()); var formValueTime = formValue.match(/(&t=\d*)$/g); if (formValueTime) { - formValueTime = formValueTime[0]; + formValueTime = parseInt(formValueTime[0].substring(3)); formValue = formValue.replace(/(&t=\d*)$/g, ""); } if (formValue) { From 6b7b3a04090bb753cc6a55fb65e69139cf3cc2e7 Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Sat, 21 May 2016 17:14:20 +0200 Subject: [PATCH 05/12] minor fixes --- .eslintrc.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index d5a3eb49..c7682188 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -50,7 +50,8 @@ "space-before-blocks": 2, "spaced-comment": 2, "space-infix-ops": 2, - "space-after-keywords": 2 + "space-after-keywords": 2, + "comma-spacing": 2 }, "extends": "eslint:recommended" } From 3316d151196b1073f88a33efb0d1c6ff474e678d Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Sat, 21 May 2016 17:21:49 +0200 Subject: [PATCH 06/12] test fix --- js/everything.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/everything.js b/js/everything.js index b97a8164..c77405cb 100644 --- a/js/everything.js +++ b/js/everything.js @@ -380,7 +380,7 @@ function removeSearchQueryFromURL(url) { } return url; } -function makeListenURL(videoID,videoPosition) { +function makeListenURL(videoID, videoPosition) { var url = removeSearchQueryFromURL(window.location.href); // Remove any #s which break functionality url = url.replace("#", ""); @@ -535,7 +535,7 @@ $(function() { window.location.href = makeSearchURL(formValue); } else { - window.location.href = makeListenURL(videoID,formValueTime); + window.location.href = makeListenURL(videoID, formValueTime); } } }).fail(function(jqXHR, textStatus, errorThrown) { From 4cb4c05f46c050204ccd0a4edcfc3a8e8f43224e Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Sat, 21 May 2016 23:15:32 +0200 Subject: [PATCH 07/12] minor fix this and that --- js/everything.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/js/everything.js b/js/everything.js index c77405cb..4298e55a 100644 --- a/js/everything.js +++ b/js/everything.js @@ -365,8 +365,13 @@ function getCurrentVideoID() { } function getCurrentTimePosition() { - var t = getParameterByName(window.location.search, "t"); - return t; + var t = parseInt(getParameterByName(window.location.search, "t"), 10); + if (t >= 0 && t < 999999) { + return t; + } + else { + return; + } } function getCurrentSearchQuery() { @@ -384,13 +389,11 @@ function makeListenURL(videoID, videoPosition) { var url = removeSearchQueryFromURL(window.location.href); // Remove any #s which break functionality url = url.replace("#", ""); + url += "?v=" + videoID; if (videoPosition) { - videoPosition = "&t=" + videoPosition; + url += "&t=" + videoPosition; } - else { - videoPosition = ""; - } - return url + "?v=" + videoID + videoPosition; + return url; } function makeSearchURL(searchQuery) { @@ -508,8 +511,8 @@ $(function() { event.preventDefault(); var formValue = $.trim($("#v").val()); var formValueTime = formValue.match(/(&t=\d*)$/g); - if (formValueTime) { - formValueTime = parseInt(formValueTime[0].substring(3)); + if (formValueTime && formValueTime.length > 0) { + formValueTime = parseInt(formValueTime[0].substring(3), 10); formValue = formValue.replace(/(&t=\d*)$/g, ""); } if (formValue) { From 63ff66cf8adf23e2066562357a04780a5e66b913 Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Sun, 22 May 2016 15:44:32 +0200 Subject: [PATCH 08/12] minor fixes --- js/everything.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/js/everything.js b/js/everything.js index 4298e55a..d7c1d69c 100644 --- a/js/everything.js +++ b/js/everything.js @@ -366,12 +366,9 @@ function getCurrentVideoID() { function getCurrentTimePosition() { var t = parseInt(getParameterByName(window.location.search, "t"), 10); - if (t >= 0 && t < 999999) { + if (t > 0 && t < Number.MAX_VALUE) { return t; } - else { - return; - } } function getCurrentSearchQuery() { @@ -510,7 +507,7 @@ $(function() { $("#form").submit(function(event) { event.preventDefault(); var formValue = $.trim($("#v").val()); - var formValueTime = formValue.match(/(&t=\d*)$/g); + var formValueTime = formValue.match(/(&t=\d+)$/g); if (formValueTime && formValueTime.length > 0) { formValueTime = parseInt(formValueTime[0].substring(3), 10); formValue = formValue.replace(/(&t=\d*)$/g, ""); From 47b46004c16254a9931acf670ecf49ae10e0c44b Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Tue, 24 May 2016 18:19:45 +0200 Subject: [PATCH 09/12] regexp fix and current time position delivery to Youtube link --- js/everything.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/js/everything.js b/js/everything.js index d7c1d69c..899ab117 100644 --- a/js/everything.js +++ b/js/everything.js @@ -166,6 +166,7 @@ var ZenPlayer = { // IE https://zenplayer.audio/?v=koJv-j1usoI&t=30 starts at 0:30 var t = getCurrentTimePosition(); if (t) { + that.videoPosition = t; window.sessionStorage[videoID] = t; } @@ -210,10 +211,11 @@ var ZenPlayer = { plyrPlayer.addEventListener("timeupdate", function() { // Store the current time of the video. + var resumeTime; if (window.sessionStorage) { var currentTime = plyrPlayer.plyr.embed.getCurrentTime(); var videoDuration = plyrPlayer.plyr.embed.getDuration(); - var resumeTime = 0; + resumeTime = 0; // Only store the current time if the video isn't done // playing yet. If the video finished already, then it @@ -225,6 +227,8 @@ var ZenPlayer = { } window.sessionStorage[videoID] = resumeTime; } + var updatedUrl = that.videoUrl + "&t=" + parseInt(resumeTime, 10); + $("#zen-video-title").attr("href", updatedUrl); }); plyrPlayer.plyr.source({ @@ -507,9 +511,9 @@ $(function() { $("#form").submit(function(event) { event.preventDefault(); var formValue = $.trim($("#v").val()); - var formValueTime = formValue.match(/(&t=\d+)$/g); - if (formValueTime && formValueTime.length > 0) { - formValueTime = parseInt(formValueTime[0].substring(3), 10); + var formValueTime = /&t=(\d+)$/g.exec(formValue); + if (formValueTime && formValueTime.length > 1) { + formValueTime = parseInt(formValueTime[1], 10); formValue = formValue.replace(/(&t=\d*)$/g, ""); } if (formValue) { From 3ca4c2f336bb8c7e0a0b910e1021f7aa0bf0ce91 Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Sat, 2 Jul 2016 23:33:06 +0000 Subject: [PATCH 10/12] fixes on time position --- js/everything.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/js/everything.js b/js/everything.js index 687768e9..4c63ea7b 100644 --- a/js/everything.js +++ b/js/everything.js @@ -214,11 +214,10 @@ var ZenPlayer = { plyrPlayer.addEventListener("timeupdate", function() { // Store the current time of the video. - var resumeTime; + var resumeTime = 0; if (window.sessionStorage) { var currentTime = plyrPlayer.plyr.embed.getCurrentTime(); var videoDuration = plyrPlayer.plyr.embed.getDuration(); - resumeTime = 0; // Only store the current time if the video isn't done // playing yet. If the video finished already, then it @@ -230,8 +229,13 @@ var ZenPlayer = { } window.sessionStorage[videoID] = resumeTime; } - var updatedUrl = that.videoUrl + "&t=" + parseInt(resumeTime, 10); - $("#zen-video-title").attr("href", updatedUrl); + if (resumeTime > 0) { + var updatedUrl = that.videoUrl + "&t=" + Math.round(resumeTime); + $("#zen-video-title").attr("href", updatedUrl); + } + else if (resumeTime <= 0 && $("#zen-video-title").attr("href") !== that.videoUrl) { + $("#zen-video-title").attr("href", that.videoUrl); + } }); plyrPlayer.plyr.source({ @@ -376,6 +380,7 @@ function getCurrentTimePosition() { if (t > 0 && t < Number.MAX_VALUE) { return t; } + return 0; } function getCurrentSearchQuery() { @@ -522,10 +527,10 @@ $(function() { $("#form").submit(function(event) { event.preventDefault(); var formValue = $.trim($("#v").val()); - var formValueTime = /&t=(\d+)$/g.exec(formValue); + var formValueTime = /&t=(\d*)$/g.exec(formValue); if (formValueTime && formValueTime.length > 1) { formValueTime = parseInt(formValueTime[1], 10); - formValue = formValue.replace(/(&t=\d*)$/g, ""); + formValue = formValue.replace(/&t=\d*$/g, ""); } if (formValue) { var videoID = wrapParseYouTubeVideoID(formValue, true); From bcfef2ab023936782e81d7c04aab9dcaafd24025 Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Sun, 3 Jul 2016 23:15:41 +0000 Subject: [PATCH 11/12] some fixes --- js/everything.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/js/everything.js b/js/everything.js index 4c63ea7b..0ad47640 100644 --- a/js/everything.js +++ b/js/everything.js @@ -229,13 +229,15 @@ var ZenPlayer = { } window.sessionStorage[videoID] = resumeTime; } + var updatedUrl; if (resumeTime > 0) { - var updatedUrl = that.videoUrl + "&t=" + Math.round(resumeTime); + updatedUrl = that.videoUrl + "&t=" + Math.round(resumeTime); $("#zen-video-title").attr("href", updatedUrl); } else if (resumeTime <= 0 && $("#zen-video-title").attr("href") !== that.videoUrl) { - $("#zen-video-title").attr("href", that.videoUrl); + updatedUrl = that.videoUrl; } + $("#zen-video-title").attr("href", updatedUrl); }); plyrPlayer.plyr.source({ @@ -394,6 +396,7 @@ function removeSearchQueryFromURL(url) { } return url; } + function makeListenURL(videoID, videoPosition) { var url = removeSearchQueryFromURL(window.location.href); // Remove any #s which break functionality From 2ba7031ae18bda46f42217676b953b92bc2f7ebe Mon Sep 17 00:00:00 2001 From: hjortur hjartar Date: Mon, 4 Jul 2016 07:35:53 +0000 Subject: [PATCH 12/12] final fix --- js/everything.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/everything.js b/js/everything.js index 0ad47640..eedac36f 100644 --- a/js/everything.js +++ b/js/everything.js @@ -229,7 +229,7 @@ var ZenPlayer = { } window.sessionStorage[videoID] = resumeTime; } - var updatedUrl; + var updatedUrl = that.videoUrl; if (resumeTime > 0) { updatedUrl = that.videoUrl + "&t=" + Math.round(resumeTime); $("#zen-video-title").attr("href", updatedUrl);