Sort Soundgasm Posts

Allows you to sort soundgasm.net posts by play count

  1. // ==UserScript==
  2. // @name Sort Soundgasm Posts
  3. // @version 1
  4. // @description Allows you to sort soundgasm.net posts by play count
  5. // @author Oniisama
  6. // @match *soundgasm.net/u/*
  7. // @namespace https://greasyfork.org/users/616912
  8. // ==/UserScript==
  9.  
  10. // Get all posts on the page as a NodeList, then transform into Array
  11. let container = document.getElementsByTagName("body")[0];
  12. let posts = document.getElementsByClassName("sound-details");
  13. posts = Array.prototype.slice.call(posts, 0);
  14.  
  15. let sorting_array = [];
  16. let default_post_array = [];
  17.  
  18. // Add posts and their respective playcount to sorting_array
  19. posts.forEach(function(post){
  20. let playcount = post.getElementsByClassName("playCount")[0].textContent.slice(12);
  21.  
  22. // [0] = playcount number (1 * string = number)
  23. // [1] = the element itself
  24. sorting_array.push([1 * playcount, post]);
  25. });
  26.  
  27. // Keep a copy of this array so the page can be restored to default
  28. default_post_array = Array.from(sorting_array);
  29.  
  30. function SortPage (sorting_array){
  31. // Sort the elements on the page itself
  32. for (let i=0; i<sorting_array.length; i++)
  33. {
  34. // sorting_array[ID][ELEMENT]
  35. container.appendChild(sorting_array[i][1]);
  36. }
  37. }
  38.  
  39. function SortPostsDescending(sorting_array)
  40. {
  41. // I don't really get the function parameter but it works ig
  42. sorting_array.sort(function(x, y) {
  43. return y[0] - x[0];
  44. });
  45.  
  46. SortPage(sorting_array);
  47. }
  48.  
  49. function SortPostsAscending(sorting_array)
  50. {
  51. // I don't really get the function parameter but it works ig
  52. sorting_array.sort(function(x, y) {
  53. return x[0] - y[0];
  54. });
  55.  
  56. SortPage(sorting_array);
  57. }
  58.  
  59. function SortPostsDefault(default_post_array)
  60. {
  61. SortPage(default_post_array);
  62. }
  63.  
  64. function CreateButton(text, func)
  65. {
  66. let btn = document.createElement("button");
  67. btn.innerHTML = text;
  68. btn.style.margin = "10px 5px 5px 0";
  69. btn.onclick = func;
  70.  
  71. return btn;
  72. }
  73.  
  74. // Create buttons
  75. let insertion_location = document.getElementsByClassName("sound-details")[0];
  76. container.insertBefore(CreateButton("Sort By Play Count (Desc)", function(){SortPostsDescending(sorting_array)}), insertion_location);
  77. container.insertBefore(CreateButton("Sort By Play Count (Asc)", function(){SortPostsAscending(sorting_array)}), insertion_location);
  78. container.insertBefore(CreateButton("Reset to Default", function(){SortPostsDefault(default_post_array)}), insertion_location);