Advertisement
Guest User

SoundCloud - Sort comments by timestamp - Userscript fix

a guest
Aug 23rd, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Soundcloud: Sort comments by timestamp
  3. // @description  Sort comments by timestamp on Soundcloud
  4. // @include      *soundcloud.com/*
  5. // @grant        none
  6. // @run-at       document-idle
  7. // @namespace    https://greasyfork.org/users/4252
  8. // @version      0.0.3
  9. // ==/UserScript==
  10.  
  11. var SortButton = document.createElement("button");
  12. SortButton.type = "button";
  13. SortButton.className = "sort-button sc-button sc-button-medium sc-button-responsive";
  14. SortButton.style = "float: right; margin-bottom: 6px;";
  15. SortButton.innerHTML = "Sort by timestamp";
  16. SortButton.onclick = sortcomments;
  17.  
  18. // window.onload doesn't work for soundcloud which is AJAX
  19. // popstate event doesn't work reliably either.
  20. // So let's use XHR as a proxy for page load event
  21. // Basic AJAX interceptor by hijacking XMLHttpRequest.prototype.open
  22. (function() {
  23.   XMLHttpRequest.prototype.__open = XMLHttpRequest.prototype.open;
  24.   XMLHttpRequest.prototype.open = function() {
  25.     this.addEventListener('load', function() {
  26.       if(this.readyState !== 4) return;
  27.       if(this.status !== 200) return;
  28.       if(/\/(tracks)/.test(this.responseURL)) {
  29.         appendSortButton();
  30.       }
  31.     });
  32.     XMLHttpRequest.prototype.__open.apply(this, arguments);
  33.   };
  34. })();
  35.  
  36. function appendSortButton() {
  37.     if (document.getElementsByClassName("commentsList__title").length && !document.getElementsByClassName("sort-button")[0]) {
  38.        console.info("Added sort button!");
  39.        document.getElementsByClassName("commentsList__title")[0].appendChild(SortButton);
  40.     }
  41. }
  42.  
  43. function sortcomments() {
  44.     if (document.getElementsByClassName("paging-eof").length === 0) {
  45.         alert("Please scroll all the way to the bottom so that all comments load before running this script");
  46.         return;
  47.     }
  48.  
  49.     var commentContainer = document.getElementsByClassName("lazyLoadingList__list")[0];
  50.     var allcomments = [].slice.call(commentContainer.children);
  51.     k = 0.01; //decimal to stick at end of timestamp so that threads (replies) stay together
  52.     for (i = 0; i < allcomments.length; i++) {
  53.         if (allcomments[i].firstChild.classList.contains("isReply")) {
  54.             allcomments[i].setAttribute("timestamp4sort", getTimestampInSeconds(allcomments[i]) + k);
  55.             k = k + 0.01; //theoretically correctly sort 100 consecutive replies
  56.         } else {
  57.             allcomments[i].setAttribute("timestamp4sort", getTimestampInSeconds(allcomments[i]));
  58.             k = 0.01; //reset
  59.         }
  60.     }
  61.  
  62.     allcomments.sort(compare);
  63.  
  64.     while (commentContainer.lastChild) {
  65.         commentContainer.removeChild(commentContainer.lastChild);
  66.     }
  67.  
  68.     for (i = 0; i < allcomments.length; i++) {
  69.         commentContainer.appendChild(allcomments[i]);
  70.     }
  71.  
  72.     console.info("Comments sorted successfully");
  73. }
  74.  
  75.  
  76. function compare(a, b) {
  77.     var avalue = parseFloat(a.getAttribute("timestamp4sort"));
  78.     var bvalue = parseFloat(b.getAttribute("timestamp4sort"));
  79.     if (avalue < bvalue)
  80.         return -1;
  81.     if (avalue > bvalue)
  82.         return 1;
  83.     return 0;
  84. }
  85.  
  86. function hmsToSecondsOnly(str) { //This function handles "HH:MM:SS" as well as "MM:SS" or "SS".
  87.     var p = str.split(':'),
  88.         s = 0,
  89.         m = 1;
  90.  
  91.     while (p.length > 0) {
  92.         s += m * parseInt(p.pop(), 10);
  93.         m *= 60;
  94.     }
  95.  
  96.     return s;
  97. }
  98.  
  99. function getTimestampInSeconds(licomment) { //takes  the <li> element of a comment. returns an integer
  100.  
  101.     if (licomment.getElementsByClassName("commentItem__timestamp").length !== 0) {
  102.         return hmsToSecondsOnly(licomment.getElementsByClassName("commentItem__timestamp")[0].innerHTML.replace("says at ", "").slice(0, -1)); //we slice of the last character to change "1:32:25:" to "1:32:25"
  103.     } else {
  104.         return 0;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement