// ==UserScript== // @name Patreon: Sort by most liked comment // @namespace http://tampermonkey.net/ // @version 2024-05-14-v2 // @description On a post's page, this adds a button to sort all VISIBLE root comments by most liked (then by date) // @author You // @match https://www.patreon.com/posts/* // @icon https://www.google.com/s2/favicons?sz=64&domain=patreon.com // @grant none // @downloadURL none // ==/UserScript== window.addEventListener('load', loaded); function loaded(){ const getLikeCount = commentRow => parseInt((commentRow.querySelector('div[data-tag="comment-row"] span[data-tag="like-count"]') || {textContent: 0}).textContent); const compare = (a, b) => getLikeCount(b) - getLikeCount(a); function sortComments() { const commentContainer = document.querySelector('div[data-tag="content-card-comment-thread-container"] > div'); const allcomments = Array.from(commentContainer.children).sort(compare); commentContainer.innerHTML = ''; commentContainer.append(...allcomments); } //create button const commentCountButton = document.querySelector('button[data-tag="comment-post-icon"]'); let sortButton = commentCountButton.cloneNode(true); sortButton.removeAttribute('data-tag'); sortButton.removeAttribute('aria-label'); sortButton.querySelector('p').textContent = 'Sort Comments'; //replace button icon sortButton.querySelector('div svg').remove(); const newSvg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); newSvg.setAttribute("xmlns", "http://www.w3.org/2000/svg"); newSvg.setAttribute("viewBox", "0 0 10 10"); const newPath = document.createElementNS("http://www.w3.org/2000/svg", "path"); newPath.setAttribute("d", "M2 0v6h-2l2.5 2 2.5-2h-2v-6h-1zm2 0v1h4v-1h-4zm0 2v1h3v-1h-3zm0 2v1h2v-1h-2z"); newSvg.appendChild(newPath); sortButton.querySelector('div').appendChild(newSvg); sortButton.onclick = sortComments; commentCountButton.parentElement.appendChild(sortButton); }