Add Reddit Message Sorting Links

Add links for sorting user messages (excluding sent) in Reddit.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        Add Reddit Message Sorting Links
// @namespace   AddRedditMessageSortingLinks
// @description Add links for sorting user messages (excluding sent) in Reddit.
// @version     1.0.1
// @author      jcunews
// @include     https://www.reddit.com/message/*
// @grant       none
// ==/UserScript==

(function(){
  var eleLinkTemplate = document.querySelectorAll(".menuarea li:not(.selected)"), eleMenu;

  function compareTitle(ele1, ele2) {
    var e1 = ele1.querySelector(".title"), e2 = ele2.querySelector(".title");
    if (!e1) e1 = ele1.querySelector(".subject-text");
    e1 = e1.textContent.toLowerCase();
    if (!e2) e2 = ele2.querySelector(".subject-text");
    e2 = e2.textContent.toLowerCase();
    if (e1 < e2) {
      return -1;
    } else if (e1 > e2) {
      return 1;
    } else return 0;
  }

  function compareUser(ele1, ele2) {
    ele1 = ele1.querySelector(".author,.correspondent>a").textContent.toLowerCase();
    ele2 = ele2.querySelector(".author,.correspondent>a").textContent.toLowerCase();
    if (ele1 < ele2) {
      return -1;
    } else if (ele1 > ele2) {
      return 1;
    } else return 0;
  }

  function compareSub(ele1, ele2) {
    var e1 = ele1.querySelector(".subreddit>a");
    e1 = e1 ? e1.href.toLowerCase() : "";
    e2 = ele2.querySelector(".subreddit>a");
    e2 = e2 ? e2.href.toLowerCase() : "";
    if (!e1 && !e2) return compareTime(ele1, ele2);
    if (e1 < e2) {
      return -1;
    } else if (e1 > e2) {
      return 1;
    } else return 0;
  }

  function compareTime(ele1, ele2) {
    ele1 = new Date(ele1.querySelector(".live-timestamp,time").getAttribute("datetime"));
    ele2 = new Date(ele2.querySelector(".live-timestamp,time").getAttribute("datetime"));
    if (ele1 < ele2) {
      return 1;
    } else if (ele1 > ele2) {
      return -1;
    } else return 0;
  }

  function doSort() {
    var eles = Array.prototype.slice.call(document.querySelectorAll("#siteTable>.thing")), i;
    if (!eles.length) return;
    var list = eles[0].parentNode;
    switch (this.getAttribute("by")) {
      case "title": eles.sort(compareTitle); break;
      case "user": eles.sort(compareUser); break;
      case "sub": eles.sort(compareSub); break;
      default: eles.sort(compareTime);
    }
    eles.forEach(function(val, idx) {
      list.insertBefore(val, list.children[idx]);
    });
  }

  if (!eleLinkTemplate.length) return;
  eleLinkTemplate = eleLinkTemplate[eleLinkTemplate.length-1];
  eleMenu = eleLinkTemplate.parentNode;
  [
    { caption: "Sort by title", by: "title" },
    { caption: "Sort by user", by: "user" },
    { caption: "Sort by subreddit", by: "sub" },
    { caption: "Sort by time", by: "time" }
  ].forEach(function(val, idx) {
    var menuitem = eleLinkTemplate.cloneNode(true), link = menuitem.lastElementChild;
    if (!idx) {
      menuitem.style.marginLeft = "3ex";
      menuitem.removeChild(menuitem.firstElementChild);
    }
    link.textContent = val.caption;
    link.setAttribute("by", val.by);
    link.href = "javascript:void(0)";
    link.onclick = doSort;
    eleMenu.appendChild(menuitem);
  });
})();