Lemmy Post Keyword Filter

Automatically hide posts that match keywords from Lemmy sites.

  1. // ==UserScript==
  2. // @name Lemmy Post Keyword Filter
  3. // @namespace https://zetaphor.com/
  4. // @homepageURL https://github.com/Zetaphor/lemmy-keyword-filter
  5. // @supportURL https://github.com/Zetaphor/lemmy-keyword-filter/issues
  6. // @version 1.1
  7. // @license WTFPL
  8. // @description Automatically hide posts that match keywords from Lemmy sites.
  9. // @author Zetaphor
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_registerMenuCommand
  13. // @run-at document-end
  14. // @match *://*/*
  15. // ==/UserScript==
  16.  
  17. (function () {
  18. 'use strict';
  19.  
  20. // Check if the main Lemmy selector is present
  21. const lemmySelectorPresent = document.querySelector('#app.lemmy-site');
  22. if (!lemmySelectorPresent) {
  23. console.log('Not a Lemmy site. Script will not run.');
  24. return;
  25. }
  26.  
  27. // Function to hide the post element and the preceding <hr> tag
  28. function hidePost(postElement) {
  29. postElement.style.display = 'none';
  30. const precedingHR = postElement.previousElementSibling;
  31. if (precedingHR && precedingHR.tagName === 'HR') {
  32. precedingHR.style.display = 'none';
  33. }
  34. }
  35.  
  36. // Function to check if the post title contains any of the keywords
  37. function checkKeywords(postElement, keywords) {
  38. const postTitle = postElement.querySelector('.post-title span');
  39. if (!postTitle) return;
  40.  
  41. const titleText = postTitle.innerText.toLowerCase();
  42. for (const keyword of keywords) {
  43. if (titleText.includes(keyword.toLowerCase())) {
  44. hidePost(postElement);
  45. break;
  46. }
  47. }
  48. }
  49.  
  50. // Main function to apply the keyword filtering
  51. function filterPostsByKeywords(keywords) {
  52. // Do nothing if the keyword list is empty
  53. if (keywords.length === 0) return;
  54.  
  55. const postListings = document.querySelectorAll('.post-listing');
  56. postListings.forEach((postElement) => {
  57. checkKeywords(postElement, keywords);
  58. });
  59. }
  60.  
  61. // Settings page to edit the list of keywords
  62. function createSettingsPage() {
  63. const settingsHTML = `
  64. <div>
  65. <h2 style="color: white;">Lemmy Keyword Filter Settings</h2>
  66. <label for="keyword-list" style="color: white;">Enter keywords (comma-separated):</label>
  67. <br>
  68. <input type="text" id="keyword-list" style="width: 300px;" value="">
  69. <br><br>
  70. <button id="save-button">Save</button>
  71. </div>
  72. `;
  73.  
  74. const popupDiv = document.createElement('div');
  75. popupDiv.style.position = 'fixed';
  76. popupDiv.style.top = '20%';
  77. popupDiv.style.left = '20%';
  78. popupDiv.style.width = '60%';
  79. popupDiv.style.backgroundColor = 'grey';
  80. popupDiv.style.color = 'white';
  81. popupDiv.style.padding = '20px';
  82. popupDiv.innerHTML = settingsHTML;
  83. document.body.appendChild(popupDiv);
  84.  
  85. const saveButton = document.getElementById('save-button');
  86. const keywordListInput = document.getElementById('keyword-list');
  87.  
  88. // Load the saved keywords and display them in the input field
  89. const savedKeywords = GM_getValue('lemmyKeywords', []);
  90. keywordListInput.value = savedKeywords.join(', ');
  91.  
  92. // Save the keywords when the Save button is clicked
  93. saveButton.addEventListener('click', () => {
  94. let keywords = keywordListInput.value.split(',').map((keyword) => keyword.trim());
  95. keywords = keywords.filter(keyword => keyword.length > 0); // filter out empty strings
  96. GM_setValue('lemmyKeywords', keywords);
  97. filterPostsByKeywords(keywords);
  98. popupDiv.remove(); // Close the settings page after saving
  99. });
  100. }
  101.  
  102. // Function to open the settings page
  103. function openSettingsPage() {
  104. createSettingsPage();
  105. }
  106.  
  107. // Add the settings page option to the Tampermonkey menu
  108. GM_registerMenuCommand('Lemmy Keyword Filter Settings', openSettingsPage);
  109.  
  110. // Get the saved keywords and apply the filtering
  111. const savedKeywords = GM_getValue('lemmyKeywords', []);
  112. filterPostsByKeywords(savedKeywords);
  113. })();