Greasy Fork 还支持 简体中文。

Soundcloud:Sort comments by timestamp

Sort comments by timestamp on Soundcloud

目前為 2015-10-01 提交的版本,檢視 最新版本

  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. // @namespace https://greasyfork.org/users/4252
  7. // @version 0.0.1.20151001012956
  8. // ==/UserScript==
  9. //Note: Only been tested in firefox
  10. var SortButton = document.createElement("button");
  11. SortButton.type = "button";
  12. SortButton.className = "sc-button sc-button-medium sc-button-responsive";
  13. SortButton.innerHTML = "Sort by timestamp";
  14. SortButton.onclick = sortcomments;
  15. window.addEventListener('load', WindowLoadedSortButton, false);
  16.  
  17. function WindowLoadedSortButton() {
  18. if (document.getElementsByClassName("commentsList__title").length != 0) { //if comment header exists
  19. document.getElementsByClassName("commentsList__title")[0].appendChild(SortButton);
  20. }
  21. }
  22.  
  23. function sortcomments() {
  24. if (document.getElementsByClassName("paging-eof").length == 0) {
  25. alert("Please scroll all the way to the bottom so that all comments load before running this script");
  26. return;
  27. }
  28.  
  29. var commentContainer = document.getElementsByClassName("lazyLoadingList__list")[0];
  30. var allcomments = [].slice.call(commentContainer.children);
  31. k = 0.01; //decimal to stick at end of timestamp so that threads (replies) stay together
  32. for (i = 0; i < allcomments.length; i++) {
  33. if (allcomments[i].firstChild.classList.contains("isReply")) {
  34. allcomments[i].setAttribute("timestamp4sort", getTimestampInSeconds(allcomments[i]) + k);
  35. k = k + 0.01; //theoretically correctly sort 100 consecutive replies
  36. } else {
  37. allcomments[i].setAttribute("timestamp4sort", getTimestampInSeconds(allcomments[i]));
  38. k = 0.01; //reset
  39. }
  40. }
  41.  
  42. allcomments.sort(compare);
  43.  
  44. while (commentContainer.lastChild) {
  45. commentContainer.removeChild(commentContainer.lastChild);
  46. }
  47.  
  48. for (i = 0; i < allcomments.length; i++) {
  49. commentContainer.appendChild(allcomments[i]);
  50. }
  51.  
  52. alert("sorted");
  53. }
  54.  
  55.  
  56. function compare(a, b) {
  57. var avalue = parseFloat(a.getAttribute("timestamp4sort"));
  58. var bvalue = parseFloat(b.getAttribute("timestamp4sort"));
  59. if (avalue < bvalue)
  60. return -1;
  61. if (avalue > bvalue)
  62. return 1;
  63. return 0;
  64. }
  65.  
  66. function hmsToSecondsOnly(str) { //This function handles "HH:MM:SS" as well as "MM:SS" or "SS".
  67. var p = str.split(':'),
  68. s = 0,
  69. m = 1;
  70.  
  71. while (p.length > 0) {
  72. s += m * parseInt(p.pop(), 10);
  73. m *= 60;
  74. }
  75.  
  76. return s;
  77. }
  78.  
  79. function getTimestampInSeconds(licomment) { //takes the <li> element of a comment. returns an integer
  80.  
  81. if (licomment.getElementsByClassName("commentItem__timestamp").length != 0) {
  82. 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"
  83. } else {
  84. return 0;
  85. }
  86. }