4chan post sort

Sorts 4chan posts in a thread by number of replies to a post

  1. // ==UserScript==
  2. // @name 4chan post sort
  3. // @namespace 4chanpostsort
  4. // @description Sorts 4chan posts in a thread by number of replies to a post
  5. // @include http://boards.4chan.org/*/*
  6. // @include https://boards.4chan.org/*/*
  7. // @version 0.0.1.20150629054247
  8. // ==/UserScript==
  9.  
  10.  
  11. if (!GM_registerMenuCommand)
  12. alert('Please upgrade to the latest version of Greasemonkey.');
  13. else
  14. {
  15. function toArray(obj) {
  16. var array = [];
  17. // iterate backwards ensuring that length is an UInt32
  18. for (var i = obj.length >>> 0; i--;) {
  19. array[i] = obj[i];
  20. }
  21. return array;
  22. };
  23.  
  24. function compareChildrenCount (a,b) {
  25. if (a.children.length < b.children.length) { return -1; }
  26. if (a.children.length > b.children.length) { return 1; }
  27. return 0;
  28. };
  29.  
  30. function PS_sort()
  31. {
  32. var reps = document.getElementsByClassName("backlink");
  33. reps = toArray(reps);
  34. reps.sort(compareChildrenCount);
  35. t=reps[0].parentNode.parentNode.parentNode.parentNode;
  36. for(i=0,x=reps.length;i<x;i++) {
  37. var e = reps[i].parentNode.parentNode.parentNode;
  38. e = t.removeChild(e);
  39. t.insertBefore(e, t.childNodes[0]);
  40. };
  41. };
  42.  
  43. GM_registerMenuCommand('4chan Sort Posts by Reply Count', PS_sort);
  44. };
  45.  
  46.