Filter lemmy.ml communities

filter unwanted communities from showing up in your lemmy.ml feed

  1. // ==UserScript==
  2. // @name Filter lemmy.ml communities
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description filter unwanted communities from showing up in your lemmy.ml feed
  6. // @author bigguy
  7. // @match https://lemmy.ml/home/data_type/Post/*
  8. // @match https://lemmy.ml
  9. // @icon https://www.google.com/s2/favicons?domain=lemmy.ml
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // ==/UserScript==
  13.  
  14. //simple scrappy way to auto filter unwanted communities from showing up in your feed
  15. //a link (Y) shows up next to the community name in your feed which you press to filter that community
  16. //to undo a filtered community: you need to press H key to halt all further filtering, then refresh, now you can select 'N' link.
  17. //press H again to toggle halting on/off - no need to refresh turning halt off.
  18.  
  19. var fucks = GM_getValue("fuckers",[]); //local storage within userscript space
  20. var halt = GM_getValue("halt",false);
  21. console.log("community filter on", fucks);
  22. function removePosts() {
  23. document.querySelectorAll('.list-inline-item').forEach(function(postinner) {
  24.  
  25. var links = postinner.getElementsByTagName('a');
  26. if (links.length > 1) {
  27. var curl = postinner.getElementsByTagName('a')[1].href.split('/');
  28. var community = curl[3]+'/'+curl[4];
  29. //console.log(community);
  30.  
  31. if (!halt && community && fucks.includes(community)) {
  32. console.log(community + ' the fucker, has been blocked');
  33. //console.log(postinner.parentElement.parentElement.parentElement.parentElement.parentElement);
  34. postinner.parentElement.parentElement.parentElement.parentElement.parentElement.remove();
  35. }
  36. }
  37.  
  38. });
  39. };
  40.  
  41. function applyButtons() {
  42. if (document.getElementsByClassName("togg").length == 0 && document.getElementsByClassName("toff").length == 0){
  43. document.querySelectorAll('.list-inline-item').forEach(function(postinner) {
  44. //console.log(postinner.getElementsByTagName('a').length);
  45. var links = postinner.getElementsByTagName('a');
  46. if (links.length > 1) {
  47. var curl = postinner.getElementsByTagName('a')[1].href.split('/');
  48. var community = curl[3]+'/'+curl[4];
  49. //console.log(community);
  50.  
  51. var a = document.createElement('a');
  52. a.className = 'togg';
  53. //a.href = 'javascript:void(0)'; //causes chrome to shit the bed with the script.
  54. a.innerText = " [Y] ";
  55. a.onclick = function test(){ fucks.indexOf(community) === -1 ? fucks.push(community) : null; GM_setValue("fuckers", fucks); console.log(fucks); removePosts(); postinner.parentElement.parentElement.getElementsByClassName("post-title")[0].innerText = ",'" + community + "'";}
  56. postinner.append(a);
  57. if (community && fucks.includes(community)) {
  58. var b = document.createElement('a');
  59. b.className = 'toff';
  60. b.innerText = " [N] ";
  61. b.onclick = function test(){ delete fucks[fucks.indexOf(community)]; GM_setValue("fuckers", fucks); console.log(fucks);}
  62. postinner.append(b);
  63. }
  64. }
  65. });
  66. }
  67. };
  68. setInterval(applyButtons, 5000);
  69. setInterval(removePosts, 3000); //lemmy is a dynamic site so I am spamming the function execution since I can't remember what dom modification I need to listen for.
  70. applyButtons();
  71.  
  72. document.addEventListener('keydown', function(event) {
  73. if (event.code == 'KeyS'){ //start
  74. applyButtons();
  75. }
  76. if (event.code == 'KeyF'){ //filter
  77. removePosts();
  78. }
  79. if (event.code == 'KeyR'){ //reset fully
  80. //GM_setValue("fuckers", [])
  81. }
  82. if (event.code == 'KeyL'){ //list
  83. console.log(fucks);
  84. }
  85. if (event.code == 'KeyH'){ //halt
  86. halt = !halt;
  87. console.log("halting", halt);
  88. GM_setValue("halt", halt);
  89. removePosts();
  90. }
  91. });
  92.