Guest User

Untitled

a guest
Sep 6th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Group By Subreddit
  3. // @author Redditlien
  4. // @namespace groupbysubreddit
  5. // @version 201710131432
  6. // @description Groups submissions by subreddit on main page
  7. // @include http://www.reddit.com/*
  8. // @include https://www.reddit.com/*
  9. // @include https://old.reddit.com/*
  10. // @exclude http://www.reddit.com/message/*
  11. // @exclude http://www.reddit.com/user/*
  12. // @exclude https://www.reddit.com/message/*
  13. // @exclude https://www.reddit.com/user/*
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17. var atags = document.getElementsByTagName('a');
  18. var sitetable = document.getElementById('siteTable');
  19. var myhash = new Object();
  20. var nextprev;
  21.  
  22. function getParentDiv(node) {
  23. while(node.classList.contains("thing") == false && node.parentNode) {
  24. node = node.parentNode;
  25. }
  26.  
  27. return node;
  28. }
  29.  
  30. // build a hash of subreddit => array<submissions from that subreddit>
  31. for(i=0;i<atags.length;i++) {
  32. var node = atags[i];
  33. var match = node.className.match(/.*subreddit.*/);
  34. if(match) {
  35. var subreddit = node.innerHTML;
  36. if(!myhash[subreddit]) {
  37. myhash[subreddit] = new Array();
  38. myhash[subreddit+"_link"] = node.href;
  39. }
  40. var parentdiv = getParentDiv(node);
  41. myhash[subreddit].push(parentdiv);
  42. }
  43. }
  44.  
  45. // Clear the sitetable of all links, then store the next/prev links
  46. if (sitetable.hasChildNodes() && Object.keys(myhash).length){
  47. while(sitetable.childNodes.length > 1){
  48. sitetable.removeChild(sitetable.firstChild);
  49. }
  50. nextprev = sitetable.firstChild;
  51. }
  52.  
  53. // Rebuild the html from the myhash links with subreddit titles
  54. for(var subreddit in myhash) {
  55. if(!subreddit.match(/.*_link$/)) {
  56. var titlenode = document.createElement('a');
  57. var mybr = document.createElement('br');
  58.  
  59. titlenode.setAttribute('href', myhash[subreddit+"_link"]);
  60. titlenode.setAttribute('style', "color: #336699; font-size: 20px;");
  61. titlenode.innerHTML = subreddit;
  62.  
  63. sitetable.appendChild(titlenode);
  64. sitetable.appendChild(mybr);
  65.  
  66. var submissions = myhash[subreddit];
  67. for(var i=0;i<submissions.length;i++) {
  68. submissions[i].style.display = 'inline';
  69. sitetable.appendChild(submissions[i]);
  70. }
  71. sitetable.appendChild(nextprev);
  72. }
  73. }
Add Comment
Please, Sign In to add comment