Skip to content

Instantly share code, notes, and snippets.

@creesch
Created July 24, 2023 18:54
Show Gist options
  • Save creesch/3949a89ddc460ea4d3735abc1fa2f8b5 to your computer and use it in GitHub Desktop.
Save creesch/3949a89ddc460ea4d3735abc1fa2f8b5 to your computer and use it in GitHub Desktop.
Beehaw.org userscript for post filtering based on keywords.
'use strict';
// ==UserScript==
// @name filter out stuff
// @namespace https://www.creesch.com/
// @version 0.0.1
// @description filter out posts with keywords
// @author Creesch
// @match *://beehaw.org/*
// @grant none
// ==/UserScript==
const filterWords = [
'twitter',
'Elon',
'musk',
];
function handlePostListings (postListingElem) {
const postsElem = postListingElem.querySelectorAll('.post-listing.mt-2');
postsElem.forEach(postElem => {
const postTitle = postElem.querySelector('.post-title').textContent;
let filterMatch = false;
filterWords.forEach(filterWord => {
if (postTitle.toLowerCase().includes(filterWord.toLowerCase())) {
filterMatch = true;
}
});
if (filterMatch) {
postElem.classList.add('filtered-element');
}
});
}
(function () {
const isLemmy =
document.head.querySelector('[name~=Description][content]').content ===
'Lemmy';
if (!isLemmy) {
return;
}
function main () {
// Inject styles
const style = document.createElement('style');
style.innerHTML = `
.filtered-element {
display: none !important;
}
`;
document.head.appendChild(style);
// First load, check manually for a post listing
const postListingsOnLoad = document.querySelector('.main-content-wrapper > div > .post-listings');
if (postListingsOnLoad) {
handlePostListings(postListingsOnLoad);
}
// Set up dom observer to watch for changes now the page is loaded.
const target = document.querySelector('body');
// create an observer instance
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
const postListings = mutation.target.querySelector('div > .post-listings');
handlePostListings(postListings);
});
});
// configuration of the observer:
// We specifically want all child elements but nothing else.
const config = {
attributes: false,
childList: true,
characterData: false,
subtree: true,
};
// pass in the target node, as well as the observer options
observer.observe(target, config);
}
// run on page load
main();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment