Hide topics

Hide topics from the topic list

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Hide topics
// @namespace    http://tampermonkey.net/
// @version      1.01
// @license      MIT
// @description  Hide topics from the topic list
// @author       Milan
// @match        *://*.websight.blue/thread/*
// @match        *://*.websight.blue/threads/*
// @match        *://*.websight.blue/
// @match        *://*.websight.blue/multi/*
// @icon         https://lore.delivery/static/blueshi.png
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';
    const addToggleButton = (infobar, threadRef) => {
        const button = document.createElement("a");
        const buttonText = document.createTextNode(isInList(threadRef) ? "Undo hide" : "Hide");
        const seperator = document.createTextNode(" | ");
        button.href="#";
        button.id = "GM-hide-toggle-button";
        button.onclick = e => {
            e.preventDefault();
            button.text = isInList(threadRef) ? "Hide" : "Undo hide";
            button.title = "Toggle hidden status of topic";
            isInList(threadRef) ? removeHide(threadRef) : addHide(threadRef);
        }
        infobar.appendChild(seperator);
        infobar.appendChild(button);
        button.appendChild(buttonText);
    }
    const addHide = threadRef => {
        const hideList = GM_getValue("hidden-topics-list", []);
        GM_setValue("hidden-topics-list", [...hideList, threadRef]);
    }
    const removeHide = threadRef => {
        const hideList = GM_getValue("hidden-topics-list", []);
        GM_setValue("hidden-topics-list", hideList.filter(hiddenThreadRef => { return hiddenThreadRef !== threadRef }));
    }
    const isInList = threadRef => {
        const hideList = GM_getValue("hidden-topics-list", []);
        return hideList.includes(threadRef);
    }
    const hide = topic => { topic.style.display = "none"; };

    const addUnhideButton = menu => {
        const button = document.createElement("a");
        button.href = "#";
        button.id = "GM-unhide-button";
        button.title = "Reveal hidden topics";
        button.innerText = "Reveal";
        button.addEventListener('click', () => {
            button.innerText = "Revealed";
            document.querySelectorAll("#thread-list>tbody>tr").forEach( row => {
                row.style.display = "table-row";
            });
        });
        menu.append(" | ");
        menu.append(button);
    }

    const addKeyboardShortcut = (threadList) => {
        document.addEventListener('keydown', (e) => {
            if (e.key == "h") {
                threadList.forEach(row => {
                    row.onclick = (e) => {
                        e.preventDefault();
                        if(e.ctrlKey) rowClickHandler(row, threadList);
                    }
                });
            }
        });
    }

    const rowClickHandler = (row, threadList) => {
        addHide(row.dataset.threadRef);
        row.style.display = "none";
    }

    const removeAllListeners = (threadList) => {
        threadList.forEach(thread => {
            thread.removeEventListener("click", rowClickHandler);
        });
    }

    // if on message list
    if(document.getElementById("messages")) {
        const zone = document.querySelector(".message-container.post").dataset.zone;
        const id = document.querySelector(".message-container.post").dataset.thread;
        const threadRef = `${zone} ${id}`;
        const infobar = document.querySelector(".infobar");
        addToggleButton(infobar, threadRef);
    }

    // if on topic list
    if(document.getElementById("thread-list")) {
        const threadList = document.querySelectorAll("#thread-list tr");
        const menu = document.querySelector(".userbar");
        Array.prototype.filter.call(threadList, topic => { return isInList(topic.dataset?.threadRef) }).forEach( topic => { hide(topic); });
        addUnhideButton(menu);
        addKeyboardShortcut(threadList);
    }
})();