Greasy Fork 还支持 简体中文。

Soundcloud:Sort comments by timestamp

Sort comments by timestamp on Soundcloud

目前為 2017-02-08 提交的版本,檢視 最新版本

  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.20170208200320
  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.  
  16. (function() {//when page loads, add the button
  17. XMLHttpRequest.prototype.__open = XMLHttpRequest.prototype.open;
  18. XMLHttpRequest.prototype.open = function() {
  19. this.addEventListener('load', function() {
  20. if(this.readyState !== 4) return;
  21. if(this.status !== 200) return;
  22. if(/\/(tracks)/.test(this.responseURL)) {
  23. appendSortButton();
  24. }
  25. });
  26. XMLHttpRequest.prototype.__open.apply(this, arguments);
  27. };
  28. })();
  29.  
  30. function appendSortButton() {
  31. if (document.getElementsByClassName("commentsList__title").length && !document.getElementsByClassName("sort-button")[0]) {//if comment header exists and button hasn't already been added
  32. document.getElementsByClassName("commentsList__title")[0].appendChild(SortButton);
  33. }
  34. }
  35.  
  36. function sortcomments() {
  37. if (document.getElementsByClassName("paging-eof").length === 0) {
  38. alert("Please scroll all the way to the bottom so that all comments load before running this script");
  39. return;
  40. }
  41.  
  42. var commentContainer = document.getElementsByClassName("lazyLoadingList__list")[0];
  43. var allcomments = [].slice.call(commentContainer.children);
  44. var k = 0.001; //decimal to stick at end of timestamp so that threads (replies) stay together
  45. for (i = 0; i < allcomments.length; i++) {
  46. if (allcomments[i].firstChild.classList.contains("isReply")) {
  47. allcomments[i].setAttribute("data-timestamp4sort", getTimestampInSeconds(allcomments[i]) + k);
  48. k = k + 0.001; //theoretically correctly sort 1000 consecutive replies
  49. } else {
  50. allcomments[i].setAttribute("data-timestamp4sort", getTimestampInSeconds(allcomments[i]));
  51. k = 0.001; //reset
  52. }
  53. }
  54.  
  55. allcomments.sort(compare);
  56.  
  57. while (commentContainer.lastChild) {
  58. commentContainer.removeChild(commentContainer.lastChild);
  59. }
  60.  
  61. var docFrag = document.createDocumentFragment();
  62. for (i = 0; i < allcomments.length; i++) {
  63. docFrag.appendChild(allcomments[i]);
  64. }
  65. commentContainer.appendChild(docFrag);
  66.  
  67. alert("Comments sorted successfully");
  68. }
  69.  
  70.  
  71. function compare(a, b) {
  72. var avalue = parseFloat(a.getAttribute("data-timestamp4sort"));
  73. var bvalue = parseFloat(b.getAttribute("data-timestamp4sort"));
  74. if (avalue < bvalue)
  75. return -1;
  76. if (avalue > bvalue)
  77. return 1;
  78. return 0;
  79. }
  80.  
  81. function hmsToSecondsOnly(str) { //This function handles "HH:MM:SS" as well as "MM:SS" or "SS".
  82. var p = str.split(':'),
  83. s = 0,
  84. m = 1;
  85.  
  86. while (p.length > 0) {
  87. s += m * parseInt(p.pop(), 10);
  88. m *= 60;
  89. }
  90.  
  91. return s;
  92. }
  93.  
  94. function getTimestampInSeconds(licomment) { //takes the <li> element of a comment. returns an integer
  95. if (licomment.getElementsByClassName("commentItem__timestampLink").length !== 0) {
  96. return hmsToSecondsOnly(licomment.getElementsByClassName("commentItem__timestampLink")[0].innerHTML);
  97. } else {
  98. return 0;
  99. }
  100. }