Anilist: Hide Uncommented Activity

Hides uncommented/unliked activity on the Anilist "Social" tab

当前为 2023-09-14 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Anilist: Hide Uncommented Activity
// @namespace    https://github.com/SeyTi01/
// @version      1.2
// @description  Hides uncommented/unliked activity on the Anilist "Social" tab
// @author       SeyTi01
// @match        https://anilist.co/*/social
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const config = {
        removeUncommented: true, // Remove activities with no comments
        removeUnliked: false, // Remove activities with no likes
        targetLoadCount: 2 // Minimum number of activities to show per click on the "Load More"-button
    };

    const SELECTORS = {
        activity: 'activity-entry',
        button: 'load-more',
        replies: 'div.action.replies',
        likes: 'div.action.likes'
    };

    const observer = new MutationObserver(observeMutations);
    let currentLoadCount = 0;
    let userPressedButton = true;
    let loadMoreButton;

    observer.observe(document.body, {childList: true, subtree: true});

    function removeEntry(element) {
        let removed = false;
        if (element instanceof HTMLElement && element.classList.contains(SELECTORS.activity)) {
            const repliesDiv = element.querySelector(SELECTORS.replies);
            const likesDiv = element.querySelector(SELECTORS.likes);

            if ((config.removeUncommented && !hasCount(repliesDiv)) || (config.removeUnliked && !hasCount(likesDiv))) {
                element.remove();
                removed = true;
            }
        }

        return removed;
    }

    function observeMutations(mutations) {
        for (const mutation of mutations) {
            if (mutation.addedNodes.length !== 0) {
                mutation.addedNodes.forEach(handleAddedNode);
            }
        }

        if (currentLoadCount < config.targetLoadCount && userPressedButton) {
            clickLoadMoreButton();
        } else {
            resetState();
        }
    }

    function handleAddedNode(node) {
        if (node instanceof HTMLElement) {
            if (node.classList.contains(SELECTORS.activity)) {
                if (!removeEntry(node)) {
                    currentLoadCount++;
                }

            } else if (node.classList.contains(SELECTORS.button)) {
                loadMoreButton = node;
                loadMoreButton.addEventListener('click', function() {
                    userPressedButton = true;
                    triggerDomEvents();
                });
            }
        }
    }

    function triggerDomEvents() {
        let scrollEvent = new CustomEvent('scroll', {bubbles: true});
        let intervalId = setInterval(function() {
            if (userPressedButton) {
                window.dispatchEvent(scrollEvent);
            } else {
                clearInterval(intervalId);
            }
        }, 100);
    }

    function hasCount(element) {
        return element?.querySelector('span.count');
    }

    function clickLoadMoreButton() {
        if (loadMoreButton) {
            loadMoreButton.click();
            loadMoreButton = null;
        }
    }

    function resetState() {
        currentLoadCount = 0;
        userPressedButton = false;
    }
})();