use dom manipulation instead of regex to update reply links

This commit is contained in:
ChunkyProgrammer 2023-11-28 18:14:39 -05:00
parent 003b0fc54c
commit 6d654e1bdf
2 changed files with 22 additions and 10 deletions

View File

@ -10,9 +10,13 @@ String.prototype.supplant = function (o) {
}); });
}; };
function updateReplyLinkHtml(contentHtml) { function updateReplyLinks() {
return contentHtml.replace(/target="_blank" href="\/comment_viewer\?[^"]*"/g, 'href="javascript:void(0)"'); document.querySelectorAll("a[href^='/comment_viewer']").forEach(function (replyLink) {
}; replyLink.setAttribute("href", "javascript:void(0)");
replyLink.removeAttribute("target");
});
}
updateReplyLinks()
function toggle_comments(event) { function toggle_comments(event) {
var target = event.target; var target = event.target;
@ -97,7 +101,7 @@ function get_youtube_comments() {
<div>{contentHtml}</div> \ <div>{contentHtml}</div> \
<hr>' <hr>'
commentInnerHtml = commentInnerHtml.supplant({ commentInnerHtml = commentInnerHtml.supplant({
contentHtml: updateReplyLinkHtml(response.contentHtml), contentHtml: response.contentHtml,
redditComments: video_data.reddit_comments_text, redditComments: video_data.reddit_comments_text,
commentsText: video_data.comments_text.supplant({ commentsText: video_data.comments_text.supplant({
// toLocaleString correctly splits number with local thousands separator. e.g.: // toLocaleString correctly splits number with local thousands separator. e.g.:
@ -108,6 +112,7 @@ function get_youtube_comments() {
}) })
}); });
comments.innerHTML = commentInnerHtml; comments.innerHTML = commentInnerHtml;
updateReplyLinks()
comments.children[0].children[0].children[0].onclick = toggle_comments; comments.children[0].children[0].children[0].onclick = toggle_comments;
if (video_data.support_reddit) { if (video_data.support_reddit) {
comments.children[0].children[1].children[0].onclick = swap_comments; 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) { if (load_more) {
body = body.parentNode.parentNode; body = body.parentNode.parentNode;
body.removeChild(body.lastElementChild); body.removeChild(body.lastElementChild);
body.insertAdjacentHTML('beforeend', updateReplyLinkHtml(response.contentHtml)); body.insertAdjacentHTML('beforeend', response.contentHtml);
updateReplyLinks()
} else { } else {
body.removeChild(body.lastElementChild); body.removeChild(body.lastElementChild);
@ -161,10 +167,11 @@ function get_youtube_replies(target, load_more, load_replies) {
a.textContent = video_data.hide_replies_text; a.textContent = video_data.hide_replies_text;
var div = document.createElement('div'); var div = document.createElement('div');
div.innerHTML = updateReplyLinkHtml(response.contentHtml); div.innerHTML = response.contentHtml;
body.appendChild(p); body.appendChild(p);
body.appendChild(div); body.appendChild(div);
updateReplyLinks()
} }
}, },
onNon200: function (xhr) { onNon200: function (xhr) {

View File

@ -6,9 +6,13 @@ var initialLoadMore = document.querySelector('a[data-onclick="get_youtube_replie
initialLoadMore.setAttribute('href', 'javascript:void(0);'); initialLoadMore.setAttribute('href', 'javascript:void(0);');
initialLoadMore.removeAttribute('target'); initialLoadMore.removeAttribute('target');
function updateReplyLinkHtml(contentHtml) { function updateReplyLinks() {
return contentHtml.replace(/target="_blank" href="\/comment_viewer\?[^"]*"/g, 'href="javascript:void(0)"'); document.querySelectorAll("a[href^='/comment_viewer']").forEach(function (replyLink) {
}; replyLink.setAttribute("href", "javascript:void(0)");
replyLink.removeAttribute("target");
});
}
updateReplyLinks()
function get_youtube_replies(target) { function get_youtube_replies(target) {
var continuation = target.getAttribute('data-continuation'); var continuation = target.getAttribute('data-continuation');
@ -28,7 +32,8 @@ function get_youtube_replies(target) {
on200: function (response) { on200: function (response) {
body = body.parentNode.parentNode; body = body.parentNode.parentNode;
body.removeChild(body.lastElementChild); body.removeChild(body.lastElementChild);
body.insertAdjacentHTML('beforeend', updateReplyLinkHtml(response.contentHtml)); body.insertAdjacentHTML('beforeend', response.contentHtml);
updateReplyLinks()
}, },
onNon200: function (xhr) { onNon200: function (xhr) {
body.innerHTML = fallback; body.innerHTML = fallback;