diff --git a/assets/js/player.js b/assets/js/player.js index 608415eb..2fcd970e 100644 --- a/assets/js/player.js +++ b/assets/js/player.js @@ -95,6 +95,109 @@ if (video_data.params.quality === 'dash') { }); } + +// Populated by DOMContentLoaded event +let shareWidgetDetailsBox +let shareToSiteContainer +let shareWidgetAddDurationButton + +/** + * Modifies share widget links to remove or add the current time to the invidious video URL + * + * @param {bool} setTime + */ +function modifySocialWidgetURLs(setTime = false) { + for (let shareToSite of shareToSiteContainer ) { + let socialLink = shareToSite.getElementsByTagName("a")[0]; + + if (!socialLink) { + continue; + } + + let shareUrl = URL.parse(socialLink.href); + let shareUrlParams = shareUrl.searchParams + let replaceAttr = socialLink.dataset.replaceAttr; + + let videoUrl = shareUrlParams.get(replaceAttr) + if (videoUrl.startsWith("/")) { + videoUrl = window.location.origin + videoUrl; + } + + let urlToReplace + + if (setTime) { + urlToReplace = addCurrentTimeToURL(videoUrl) + } else { + urlToReplace = URL.parse(videoUrl) + urlToReplace.searchParams.delete('t'); + urlToReplace = urlToReplace.toString() + } + + shareUrlParams.set(replaceAttr, urlToReplace); + socialLink.href = shareUrl.toString(); + } + + // Edit url for iframe + const iframeCodeElement = document.getElementById("share-widget-embed-code") + const iframeParsedDom = new DOMParser().parseFromString(iframeCodeElement.textContent, "text/html") + + const iframeElement = iframeParsedDom.getElementsByTagName("iframe")[0] + + let iframeSrc = iframeElement.src + + if (iframeSrc.startsWith("/")) { + iframeSrc = window.location.origin + iframeSrc; + } + + if (setTime) { + iframeSrc = addCurrentTimeToURL(iframeSrc) + } else { + iframeSrc = URL.parse(iframeSrc) + iframeSrc.searchParams.delete('t'); + iframeSrc = iframeSrc.toString() + } + + iframeElement.src = iframeSrc + iframeCodeElement.textContent = iframeElement.outerHTML +} + + +/** + * Updates the time of the Start at ${time} text in the share widget + * + */ +function updateStartAtDurationStr() { + const label = shareWidgetAddDurationButton.labels[0]; + const startAtDurationText = label.getElementsByTagName("span")[0]; + if (label) { + const duration = Math.floor(player.currentTime()); + if (duration == 0) { + startAtDurationText.innerHTML = "0:00" + return + } + + const durationStrVals = []; + const paddedDurationString = []; + + const days = Math.floor(duration / (60 * 60 * 24)); + const hours = Math.floor((duration / (60 * 60)) % 24); + const minutes = Math.floor((duration / 60) % 60); + const seconds = Math.floor(duration % 60); + + if (days !== 0) durationStrVals.push(days); + if (hours !== 0) durationStrVals.push(hours); + + durationStrVals.push(minutes); + durationStrVals.push(seconds); + + durationStrVals.forEach((val) => { + paddedDurationString.push(String(val).padStart(2, "0")) + }) + + startAtDurationText.innerHTML = paddedDurationString.join(":") + } +} + /** * Function for add time argument to url * @@ -154,6 +257,11 @@ player.on('timeupdate', function () { elem_iv_embed.href = addCurrentTimeToURL(base_url_iv_embed, domain); elem_iv_other.href = addCurrentTimeToURL(base_url_iv_other, domain); + + // Only modify share widget data if start at is selected + if (document.getElementById('share-add-duration').checked) { + modifySocialWidgetURLs(true); + } }); @@ -763,4 +871,31 @@ addEventListener('DOMContentLoaded', function () { if (changeInstanceLink) changeInstanceLink.addEventListener('click', function () { changeInstanceLink.href = addCurrentTimeToURL(changeInstanceLink.href); }); + + shareWidgetDetailsBox = document.getElementById("share-widget"); + shareToSiteContainer = document.getElementsByClassName('share-site'); + shareWidgetAddDurationButton = document.getElementById("share-add-duration"); + + shareWidgetAddDurationButton.addEventListener("change", () => { + if (shareWidgetAddDurationButton.checked) { + modifySocialWidgetURLs(true) + updateStartAtDurationStr() + } else { + modifySocialWidgetURLs(false) + updateStartAtDurationStr() + } + }) + + shareWidgetDetailsBox.addEventListener("toggle", () => { + // If share widget is opened and the share widget start at checkbox is checked + // then we will update the time to the current video time + if (shareWidgetDetailsBox.open && shareWidgetAddDurationButton.checked) { + modifySocialWidgetURLs(true) + updateStartAtDurationStr() + } else { + // Uncheck when closed + shareWidgetAddDurationButton.checked = false + modifySocialWidgetURLs(false) + } + }) }); diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index ab2c9303..39c33486 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -160,26 +160,26 @@ we're going to need to do it here in order to allow for translations.
    <% escaped_iv_watch_link = URI.encode_www_form(HOST_URL) + env.get?("current_page").as(String) %> - <% escaped_iv_embed_link = URI.encode_www_form(HOST_URL) + URI.encode_www_form(link_iv_embed.to_s) %> + <% escaped_iv_embed_link = URI.encode_www_form(HOST_URL) + URI.encode_www_form(link_iv_embed.to_s.rstrip("?")) %>