]> git.aero2k.de Git - dfde/hide-stuff.git/commitdiff
init
authorThorsten S. <mail@aero2k.de>
Sun, 17 Dec 2023 22:33:14 +0000 (23:33 +0100)
committerThorsten S. <mail@aero2k.de>
Sun, 17 Dec 2023 22:33:14 +0000 (23:33 +0100)
hide-stuff.user.js [new file with mode: 0755]

diff --git a/hide-stuff.user.js b/hide-stuff.user.js
new file mode 100755 (executable)
index 0000000..5ec9f60
--- /dev/null
@@ -0,0 +1,238 @@
+// ==UserScript==
+// @name          debianforum.de-hide-stuff
+// @namespace     org.free.for.all
+// @include       /^https?://debianforum\.de/.*/
+// @author        Thorsten Sperber
+// @version       0.3
+// ==/UserScript==
+
+/**
+ TODO: provide a reasonably good UI to hide stuff, maybe like xkill.
+ */
+
+(function() {
+    'use strict';
+
+    const opacity_default = 0.4;
+    const opacity_highlight = 1.0;
+
+    function get_hidden_threads() {
+        var storage = localStorage.getItem('hidden_threads') || "[]";
+        return JSON.parse(storage);
+    }
+
+    function get_hidden_users() {
+        var storage = localStorage.getItem('hidden_users') || "[]";
+        return JSON.parse(storage);
+    }
+    
+    function set_thread_visibility(visible) {
+        var hidden_threads = get_hidden_threads();
+
+        hidden_threads.forEach(function(val) {
+            var thread = document.querySelector(`ul.topiclist a[href$='t=${val}']`);
+            if (thread) {
+                thread.closest('li').style.display = visible ? "none" : "block";
+            }
+        });
+        update_info_buttons();
+    }
+
+    function hide_thread(thread_id) {
+        const hidden_threads = get_hidden_threads();
+        if (hidden_threads.indexOf(thread_id) < 0) { // and hidden_threads is a list
+            if (!Array.isArray(hidden_threads)) {hidden_threads = [];}
+            hidden_threads.push(thread_id);
+            console.log("Thread %s is now ignored", thread_id);
+            localStorage.setItem("hidden_threads", JSON.stringify(hidden_threads));
+        } else {
+            console.log("Thread %s already in %s",thread_id, hidden_threads);
+        }
+        // hide it within a moment
+        hide_threads(1000);
+    }
+
+    function unhide_thread(thread_id) {
+        const hidden_threads = get_hidden_threads();
+        const idx = hidden_threads.indexOf(thread_id);
+        if (idx >= 0) {
+            console.debug("unhide thread", thread_id)
+            hidden_threads.splice(idx, 1)
+            localStorage.setItem("hidden_threads", JSON.stringify(hidden_threads));
+        }
+    }
+
+    function hide_user(user_id) {
+        var hidden_users = get_hidden_users();
+        if (hidden_users.indexOf(user_id) < 0) {
+            if (!Array.isArray(hidden_users)) {hidden_users = [];}
+            hidden_users.push(user_id);
+            localStorage.setItem("hidden_users", JSON.stringify(hidden_users));
+        } else {
+            console.log("User %s already in %s",user_id, hidden_users);
+        }
+        // hide it within a moment
+        hide_users(1000);
+    }
+
+    function hide_threads() {
+        set_thread_visibility(true);
+    }
+
+    function hide_users() {
+        var hidden_users = get_hidden_users();
+
+        hidden_users.forEach(function(val) {
+            var threads = document.querySelectorAll(`ul.topiclist a[href$='u=${val}']`);
+            console.log("banning user %s", val);
+            threads.forEach(function(thread) {
+                if (thread) {
+                    thread.closest('li').style.display = "none";
+                }
+            });
+        });
+        update_info_buttons();
+    }
+
+    function unhide_threads() {
+        set_thread_visibility(false);
+    }
+
+    function unhide_users() {
+        var hidden_users = get_hidden_users();
+
+        hidden_users.forEach(function(val) {
+            var threads = document.querySelectorAll(`ul.topiclist a[href$='u=${val}']`);
+            threads.forEach(function(thread) {
+                console.log(thread);
+                thread.closest('li').style.display = "block";
+            });
+        });
+        update_info_buttons();
+    }
+
+    function add_control_buttons() {
+        const hidden_threads = get_hidden_threads();
+        const hidden_users = get_hidden_users();
+        const button_bar = document.querySelector("#page-body div.search-box fieldset");
+        const btn_base_classes = "button button-search-start dfde-hide-stuff";
+        if (!button_bar) { return; }
+
+        // TODO toggle state
+        function get_button(id, label) {
+            var btn = document.createElement("a");
+            btn.title = "Doppelklick zum Reset"
+            btn.className = btn_base_classes;
+            btn.innerHTML = `<span id='${id}'>${label}</span>`;
+            btn.style.fontWeight = "bold";
+            btn.style.color = "#8f8f8f";
+            return btn;
+        }
+        var thread_button = get_button('hidden_threads_btn', `T (${hidden_threads.length})`);
+        thread_button.addEventListener("click", () => {
+            if (!thread_button.classList.contains("show_hidden")) {
+                thread_button.className = `${btn_base_classes} show_hidden`;
+                thread_button.style.backgroundImage = "linear-gradient(to bottom, #fff 0%,#bbb 100%)";
+                unhide_threads();
+            } else {
+                thread_button.className = btn_base_classes;
+                thread_button.style.backgroundImage = null;
+                hide_threads();
+            }
+        });
+        thread_button.addEventListener("dblclick", function() {
+            unhide_threads();
+            localStorage.setItem("hidden_threads", JSON.stringify([]));
+            update_info_buttons();
+        });
+        button_bar.append(thread_button);
+
+        var user_button = get_button('hidden_users_btn', `U (${hidden_users.length})`);
+        user_button.addEventListener("click", unhide_users);
+        user_button.addEventListener("dblclick", function() {
+            unhide_users();
+            localStorage.setItem("hidden_users", JSON.stringify([]));
+            update_info_buttons();
+        });
+        button_bar.append(user_button);
+    }
+
+    function add_user_icons() {
+        /*
+    document.querySelectorAll(".lastpost a.username[href*='u=']").forEach(function(el) {
+        var trash = document.createElement('a');
+        trash.innerHTML = '<i class="icon fa-trash fa-fw icon-gray icon-md"></i>';
+        trash.style.opacity = 0.2;
+
+        trash.addEventListener("mouseenter", function() { trash.style.opacity = 1; });
+        trash.addEventListener("mouseleave", function() { trash.style.opacity = 0.2; });
+        trash.addEventListener("click", function(e) {
+            var user_id = el.getAttribute("href").match(/u=([0-9]+)/)[1];
+            hide_user(parseInt(user_id));
+            update_info_buttons();
+        });
+
+        el.parentNode.insertBefore(trash, el.nextSibling);
+    });
+    */
+        var profile = document.querySelector('#viewprofile');
+        if (!profile) { return; }
+
+        var trash = document.createElement('a');
+        trash.innerHTML = '<i class="icon fa-trash fa-fw icon-gray icon-xl"></i>';
+
+        trash.addEventListener("mouseenter", function() { trash.style.opacity = 1; });
+        trash.addEventListener("mouseleave", function() { trash.style.opacity = 0.2; });
+        trash.addEventListener("click", function(e) {
+            var user_id = profile.querySelector("a[href*='author_id']").getAttribute("href").match(/author_id=([0-9]+)/)[1];
+            hide_user(parseInt(user_id));
+            update_info_buttons();
+        });
+        // TODO: mach rot wenn geblockt
+        profile.querySelector('.inner').append(trash);
+    }
+
+    function add_thread_icons() {
+        const hidden_threads = get_hidden_threads();
+        // TODO hide/unhide buttons
+        document.querySelectorAll(".topics .row").forEach(function(el) {
+            const thread_link = el.querySelector("a.topictitle[href^='./view']");
+            const thread_id = parseInt(thread_link.getAttribute("href").match(/t=([0-9]+)/)[1]);
+
+            var btn = document.createElement('a');
+            btn.className = "dfde-hide-stuff-btn"
+            btn.style.float = "right";
+            btn.style.opacity = opacity_default;
+            let icon = hidden_threads.indexOf(thread_id) >= 0 ? "fa-eye" : "fa-eye-slash";
+            btn.innerHTML = `<i class="icon ${icon} fa-fw icon-lightgrey icon-xl"></i>`;
+
+            btn.addEventListener("mouseenter", function() { btn.style.opacity = opacity_highlight; });
+            btn.addEventListener("mouseleave", function() { btn.style.opacity = opacity_default; });
+            btn.addEventListener("click", function(e) {
+                hidden_threads.indexOf(thread_id) >= 0 ? unhide_thread(thread_id) : hide_thread(thread_id);
+                update_info_buttons();
+                // just re-add icons
+                add_thread_icons();
+            });
+            el.querySelectorAll(".lastpost span .dfde-hide-stuff-btn").forEach(btn => btn.remove())
+            el.querySelector(".lastpost span").append(btn);
+        });
+    }
+
+    function update_info_buttons() {
+        try {
+            var hidden_threads = get_hidden_threads();
+            var hidden_users = get_hidden_users();
+            document.querySelector('#hidden_threads_btn').innerHTML = `T (${hidden_threads.length})`;
+            document.querySelector('#hidden_users_btn').innerHTML = `U (${hidden_users.length})`;
+        } catch(e) {
+            // just on the wrong page, I guess
+        }
+    }
+
+    add_control_buttons();
+    add_thread_icons();
+    add_user_icons();
+    hide_threads();
+    hide_users();
+})();