Skip to content

Instantly share code, notes, and snippets.

@TheDiscordian
Last active May 9, 2023 22:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheDiscordian/8694dea73cfb5410ad10e8c8ff0d74bb to your computer and use it in GitHub Desktop.
Save TheDiscordian/8694dea73cfb5410ad10e8c8ff0d74bb to your computer and use it in GitHub Desktop.
Bluesky Word Filters
// ==UserScript==
// @name Bluesky Word Filters
// @namespace https://staging.bsky.app/
// @version 0.3
// @description remove junk posts from feeds (does't hide from profiles)
// @author Discordian
// @match https://staging.bsky.app/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant none
// ==/UserScript==
// BUG: Sometimes we only see old posts, and don't see the feed. I believe this is the result of loading in from a thread or profile post.
// Should all be lowercase, words to filter out
const keywords = ["skeet"];
// If fixUI is true then certain elements will be expanded to 100% of their container, the "New Post" button will also be adjusted to be on one line.
// This results in less broken text and a nicer new post button.
const fixUI = true;
// In debugging mode a lot of text is output into the console, and posts are red instead of hidden
const debug = false;
function hidePost(post) {
if (!debug) {
post.style = "display:none;";
} else {
post.style = "background-color:red;";
}
}
function fixWidth(ele) {
ele.style.width = "100%";
}
function getPostText(post) {
let text = "";
try {
let i = 0;
if (post.children[0].className != "css-175oi2r r-18u37iz r-1cvj4g8") {
i = 1;
}
let ii = 1;
if (post.children[i].children[0].children[0].className == "css-175oi2r r-1awozwy r-18u37iz r-1w6e6rj r-2yi16 r-1qfoi16 r-1mi0q7o r-mk0yit") {
if (fixUI) {
fixWidth(post.children[i].children[0].children[0].children[0]);
}
return post.children[i].children[0].children[0].children[0].innerText;
}
let iii = 1;
if (post.children[i].children[1].children[1].className == "css-175oi2r r-18u37iz r-zl2h9q r-1awozwy") {
iii = 2;
}
text = post.children[i].children[1].children[iii].children[0].innerText;
if (fixUI) {
fixWidth(post.children[i].children[1].children[iii].children[0].style);
}
} catch {
console.log("no text found in post");
}
if (text == undefined) {
return "";
}
return text;
}
var filtering = false;
function filterAndHide(keywords) {
if (filtering) {
return;
}
filtering = true;
if (debug) {
console.log("[debug] running filterAndHide");
}
var feed = document.getElementById("root").children[0].children[0].children[0].children[0].children[0].children[0].children[0].children[0].children[0];
let feedn = 1;
if (feed.children.length == 1) {
feedn = 0;
} else if (feed.children[1].className.includes("r-hvic4v")) {
feedn = 2;
}
if (feedn > 0) {
feed = feed.children[feedn].children[0].children[0].children[0].children[0].children[0].children;
} else {
feed = feed.children[feedn].children[0].children[0].children[0].children;
}
for (let i = 0; i < feed.length; i++) {
for (let ii = 0; ii < feed[i].children.length; ii++) {
let post = feed[i].children[ii];
let text = getPostText(post).toLowerCase();
if (debug) {
console.log(text);
}
for (let iii = 0; iii < keywords.length; iii++) {
if (text.includes(keywords[iii])) {
if (debug) {
console.log("Hiding post...");
}
hidePost(post);
break;
}
}
}
}
filtering = false;
}
(function() {
'use strict';
setTimeout(function(){
try {
if (fixUI) {
document.getElementsByClassName("r-ezt3gj")[0].style.width = "9em";
}
filterAndHide(keywords);
} catch {
filtering = false;
}
}, 1500);
document.addEventListener("scroll", function(evnt){
if (!filtering) {
setTimeout(function(){
try {
filterAndHide(keywords);
} catch {
filtering = false;
}
}, 200);
}
}, true);
document.addEventListener("click", function(evnt){
if (!filtering) {
setTimeout(function(){
try {
filterAndHide(keywords);
} catch {
filtering = false;
}
}, 1);
}
}, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment