RPS Comment Orderer

Fixes comment ordering on RPS articles

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         RPS Comment Orderer
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Fixes comment ordering on RPS articles
// @author       Bzly
// @match        https://www.rockpapershotgun.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=rockpapershotgun.com
// @grant        none
// @license      MIT
// ==/UserScript==

/* jshint esversion:10 */

function mainSorter() {
  const order = document.querySelector('div#comments > div.container').getAttribute('data-order')
  let reverse
  if (order === 'desc') {reverse = true} else {reverse = false}
  document.querySelectorAll('ol.replies').forEach(e => {
    let copy_ol = e.cloneNode(false);
    let listItems = Array.prototype.slice.call(e.children, 0)
    sortLi(listItems, reverse).forEach(i => copy_ol.appendChild(i))
    e.parentNode.replaceChild(copy_ol, e)
  })
  console.log('Ordered comment replies')
}

function sortLi(list, reverse=false) {
  list.sort(function(a, b) {
    let aCat = Date.parse(a.querySelector('.when').getAttribute('data-date'))
    let bCat = Date.parse(b.querySelector('.when').getAttribute('data-date'))
    if (aCat > bCat) return 1
    if (bCat > aCat) return -1
    return 0
  })
  if (reverse) {return list.reverse()} else {return list}
}

const sleepUntil = async (f, timeoutMs) => {
  return new Promise((resolve, reject) => {
    const timeWas = new Date();
    const wait = setInterval(function() {
      if (f()) {
        console.log("resolved after", new Date() - timeWas, "ms");
        clearInterval(wait);
        resolve();
      } else if (new Date() - timeWas > timeoutMs) { // Timeout
        console.log("rejected after", new Date() - timeWas, "ms");
        clearInterval(wait);
        reject();
      }
    }, 20);
  });
}

function callback(mutList) {mutList.forEach((m) => {
  if (m.type==="attributes") {
    mainSorter()
  }
})}

async function main() {
  try {
    await sleepUntil(() => document.querySelector('ol.posts'), 5000)
    mainSorter()
    const observer = new MutationObserver(callback);
    observer.observe(document.querySelector('div#comments > div.container'), {attributes: true, subtree: false });
  } catch {
    console.log('Timeout :(')
  }
}

main()