From 5f632e437e10401543ccbc448428ef1db5b43302 Mon Sep 17 00:00:00 2001 From: ChunkyProgrammer <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Tue, 28 Nov 2023 18:14:39 -0500 Subject: [PATCH] use dom manipulation instead of regex to update reply links --- assets/js/comments.js | 19 +++++++++++++------ assets/js/community.js | 13 +++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/assets/js/comments.js b/assets/js/comments.js index a26f1f7be..86ec15438 100644 --- a/assets/js/comments.js +++ b/assets/js/comments.js @@ -10,9 +10,13 @@ String.prototype.supplant = function (o) { }); }; -function updateReplyLinkHtml(contentHtml) { - return contentHtml.replace(/target="_blank" href="\/comment_viewer\?[^"]*"/g, 'href="javascript:void(0)"'); -}; +function updateReplyLinks() { + document.querySelectorAll("a[href^='/comment_viewer']").forEach(function (replyLink) { + replyLink.setAttribute("href", "javascript:void(0)"); + replyLink.removeAttribute("target"); + }); +} +updateReplyLinks() function toggle_comments(event) { var target = event.target; @@ -97,7 +101,7 @@ function get_youtube_comments() {
{contentHtml}
\
' commentInnerHtml = commentInnerHtml.supplant({ - contentHtml: updateReplyLinkHtml(response.contentHtml), + contentHtml: response.contentHtml, redditComments: video_data.reddit_comments_text, commentsText: video_data.comments_text.supplant({ // toLocaleString correctly splits number with local thousands separator. e.g.: @@ -108,6 +112,7 @@ function get_youtube_comments() { }) }); comments.innerHTML = commentInnerHtml; + updateReplyLinks() comments.children[0].children[0].children[0].onclick = toggle_comments; if (video_data.support_reddit) { comments.children[0].children[1].children[0].onclick = swap_comments; @@ -146,7 +151,8 @@ function get_youtube_replies(target, load_more, load_replies) { if (load_more) { body = body.parentNode.parentNode; body.removeChild(body.lastElementChild); - body.insertAdjacentHTML('beforeend', updateReplyLinkHtml(response.contentHtml)); + body.insertAdjacentHTML('beforeend', response.contentHtml); + updateReplyLinks() } else { body.removeChild(body.lastElementChild); @@ -161,10 +167,11 @@ function get_youtube_replies(target, load_more, load_replies) { a.textContent = video_data.hide_replies_text; var div = document.createElement('div'); - div.innerHTML = updateReplyLinkHtml(response.contentHtml); + div.innerHTML = response.contentHtml; body.appendChild(p); body.appendChild(div); + updateReplyLinks() } }, onNon200: function (xhr) { diff --git a/assets/js/community.js b/assets/js/community.js index 4c7080907..f076af9ac 100644 --- a/assets/js/community.js +++ b/assets/js/community.js @@ -6,9 +6,13 @@ var initialLoadMore = document.querySelector('a[data-onclick="get_youtube_replie initialLoadMore.setAttribute('href', 'javascript:void(0);'); initialLoadMore.removeAttribute('target'); -function updateReplyLinkHtml(contentHtml) { - return contentHtml.replace(/target="_blank" href="\/comment_viewer\?[^"]*"/g, 'href="javascript:void(0)"'); -}; +function updateReplyLinks() { + document.querySelectorAll("a[href^='/comment_viewer']").forEach(function (replyLink) { + replyLink.setAttribute("href", "javascript:void(0)"); + replyLink.removeAttribute("target"); + }); +} +updateReplyLinks() function get_youtube_replies(target) { var continuation = target.getAttribute('data-continuation'); @@ -28,7 +32,8 @@ function get_youtube_replies(target) { on200: function (response) { body = body.parentNode.parentNode; body.removeChild(body.lastElementChild); - body.insertAdjacentHTML('beforeend', updateReplyLinkHtml(response.contentHtml)); + body.insertAdjacentHTML('beforeend', response.contentHtml); + updateReplyLinks() }, onNon200: function (xhr) { body.innerHTML = fallback;