From 9e4d64defe700bc0b196ff12a66ee268972ed051 Mon Sep 17 00:00:00 2001 From: Dominika Liberda Date: Tue, 20 Jul 2021 23:13:04 +0000 Subject: [PATCH] + comment thread display support --- webroot/comments.shs | 43 ++++++++++++++++++++++++++++++------- webroot/css/style.css | 4 ++++ webroot/js/watch.js | 50 ++++++++++++++++++++++++++++++++----------- 3 files changed, 77 insertions(+), 20 deletions(-) diff --git a/webroot/comments.shs b/webroot/comments.shs index e2c98ad..6a5742c 100755 --- a/webroot/comments.shs +++ b/webroot/comments.shs @@ -1,16 +1,43 @@ #!/bin/bash -vid="$(curl -b cookiejar_ -H "Cookie: CONSENT=YES+1337" -s "https://www.youtube.com/watch?v=${get_data[v]}" -c cookiejar_)" # broken comment support, we need this for now - -cont="$(grep -Poh 'token":"Eg0S.*?"' <<< "$vid" | sed -E 's/token"://;s/"//g')" #' +if [[ "${get_data[v]}" == '' && "${get_data[cont]}" == '' ]]; then + echo "pffft" + return +fi + +if [[ "${get_data[cont]}" == '' ]]; then # tactical replies, INCOMING + vid="$(curl -b cookiejar_ -H "Cookie: CONSENT=YES+1337" -s "https://www.youtube.com/watch?v=${get_data[v]}" -c cookiejar_)" + cont="$(grep -Poh 'token":"Eg0S.*?"' <<< "$vid" | sed -E 's/token"://;s/"//g')" #' +else + cont="${get_data[cont]}" +fi + comments="$(curl -s 'https://www.youtube.com/youtubei/v1/next?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8' \ --compressed -H 'Content-Type: application/json' \ --data-raw '{"context":{"client":{"hl":"en","gl":"GB","clientName":"WEB","clientVersion":"2.20210719.00.00","mainAppWebInfo":{}},"adSignalsInfo":{"params":[]}},"continuation":"'"$cont"'"}' \ - | jq '.onResponseReceivedEndpoints[].reloadContinuationItemsCommand.continuationItems[]')" + | jq '.onResponseReceivedEndpoints[] | + if(.reloadContinuationItemsCommand != null) then + .reloadContinuationItemsCommand.continuationItems[] + else + .appendContinuationItemsAction.continuationItems[] + end')" - jq -r '"

\(.commentsHeaderRenderer.countText.runs[0].text) Comments

"' <<< "$comments" | head -1 - jq -r '.commentThreadRenderer.comment.commentRenderer | select(.authorText.simpleText != null) | - "
\(.authorText.simpleText)
", - (.contentText.runs[] | if .bold == true then "\(.text)" else .text end)' <<< "$comments" | sed -E 's/^$/
/g' + jq -r 'select(.commentsHeaderRenderer) | "

(whole \(.commentsHeaderRenderer.countText.runs[0].text) of them)

"' <<< "$comments" | head -1 + jq -r 'if (.commentThreadRenderer != null) then + .commentThreadRenderer | select(.comment.commentRenderer.authorText.simpleText != null) + else + .commentRenderer + end | ( + if (.comment.commentRenderer != null) then + .comment.commentRenderer + else + . + end | + "
\(.authorText.simpleText) \(.publishedTimeText.runs[].text)
",
+				(.contentText.runs[] | if .bold == true then "\(.text)" else .text end),
+				(select(.voteCount.simpleText != null) | "
\(.voteCount.simpleText) liked") + ), + (select(.replies.commentRepliesRenderer) | "
Show replies
") + ' <<< "$comments" | sed -E 's/^$/
/g' diff --git a/webroot/css/style.css b/webroot/css/style.css index e3a8e67..c7ad78c 100644 --- a/webroot/css/style.css +++ b/webroot/css/style.css @@ -2,12 +2,16 @@ font-family: sans-serif; } pre { + margin-top: 3px; white-space: pre-wrap; } .comment { margin-top: 5px; margin-bottom: 15px; } +.comment-thread, .show-more { + margin-left: 16px; +} .hide-cursor { cursor: none; diff --git a/webroot/js/watch.js b/webroot/js/watch.js index 170af7f..f5f99aa 100644 --- a/webroot/js/watch.js +++ b/webroot/js/watch.js @@ -1,17 +1,43 @@ window.addEventListener("load", () => { const comments = document.querySelector(".comments"); const id = new URLSearchParams(document.location.search).get("v"); - comments.innerText = "Loading..."; - fetch("/comments.shs?v=" + id).then( - res => { - if (res.status !== 200) { - throw new Error('HTTP Error ' + res.status); - } - return res.text(); + + function showMore(e) { + let a = e.target.cloneNode(true); + a.classList.remove("show-more"); + a.classList.add("comment-thread"); + e.target.replaceWith(a); + fetchComments(a, a.dataset.uwu); + } + + function fetchComments(div, cont) { + div.innerText = "Loading..."; + if(!cont) { + url = "/comments.shs?v=" + id; + } else { + url = "/comments.shs?cont=" + cont; } - ).then( - res => comments.innerHTML = res - ).catch( - err => comments.innerText = 'Error during fetching comments: ' + err.message - ) + fetch(url).then( + res => { + if (res.status !== 200) { + throw new Error('HTTP Error ' + res.status); + } + return res.text(); + } + ).then( + res => { + div.innerHTML = res; + if(!cont) { + let show_more = document.querySelectorAll(".show-more"); + show_more.forEach((elem) => { + elem.addEventListener("click", (e) => showMore(e)); + }); + } + } + ).catch( + err => div.innerText = 'Error during fetching comments: ' + err.message + ) + } + + fetchComments(comments); })