Filter lemmy.ml communities

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Filter lemmy.ml communities
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  filter unwanted communities from showing up in your lemmy.ml feed
// @author       bigguy
// @match        https://lemmy.ml/home/data_type/Post/*
// @match        https://lemmy.ml
// @icon         https://www.google.com/s2/favicons?domain=lemmy.ml
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

//simple scrappy way to auto filter unwanted communities from showing up in your feed
//a link (Y) shows up next to the community name in your feed which you press to filter that community
//to undo a filtered community: you need to press H key to halt all further filtering, then refresh, now you can select 'N' link.
//press H again to toggle halting on/off - no need to refresh turning halt off.

var fucks = GM_getValue("fuckers",[]); //local storage within userscript space
var halt = GM_getValue("halt",false);
console.log("community filter on", fucks);
function removePosts() {
    document.querySelectorAll('.list-inline-item').forEach(function(postinner) {

        var links = postinner.getElementsByTagName('a');
        if (links.length > 1) {
            var curl = postinner.getElementsByTagName('a')[1].href.split('/');
            var community = curl[3]+'/'+curl[4];
            //console.log(community);

            if (!halt && community && fucks.includes(community)) {
                console.log(community + ' the fucker, has been blocked');
                //console.log(postinner.parentElement.parentElement.parentElement.parentElement.parentElement);
                postinner.parentElement.parentElement.parentElement.parentElement.parentElement.remove();
            }
        }

    });
};

function applyButtons() {
    if (document.getElementsByClassName("togg").length == 0 && document.getElementsByClassName("toff").length == 0){
        document.querySelectorAll('.list-inline-item').forEach(function(postinner) {
            //console.log(postinner.getElementsByTagName('a').length);
            var links = postinner.getElementsByTagName('a');
            if (links.length > 1) {
                var curl = postinner.getElementsByTagName('a')[1].href.split('/');
                var community = curl[3]+'/'+curl[4];
                //console.log(community);

                var a = document.createElement('a');
                a.className = 'togg';
                //a.href = 'javascript:void(0)'; //causes chrome to shit the bed with the script.
                a.innerText = " [Y] ";
                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 + "'";}
                postinner.append(a);
                if (community && fucks.includes(community)) {
                    var b = document.createElement('a');
                    b.className = 'toff';
                    b.innerText = " [N] ";
                    b.onclick = function test(){ delete fucks[fucks.indexOf(community)]; GM_setValue("fuckers", fucks); console.log(fucks);}
                    postinner.append(b);
                }
            }
        });
    }
};
setInterval(applyButtons, 5000);
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.
applyButtons();

document.addEventListener('keydown', function(event) {
        if (event.code == 'KeyS'){ //start
            applyButtons();
        }
        if (event.code == 'KeyF'){ //filter
            removePosts();
        }
        if (event.code == 'KeyR'){ //reset fully
            //GM_setValue("fuckers", [])
        }
        if (event.code == 'KeyL'){ //list
            console.log(fucks);
        }
        if (event.code == 'KeyH'){ //halt
            halt = !halt;
            console.log("halting", halt);
            GM_setValue("halt", halt);
            removePosts();
        }
});