IT之家移动页面和虎扑网页热门评论高亮

在全部评论中匹配热门评论并高亮显示,避免看第二遍热门评论

// ==UserScript==
// @name         IT之家移动页面和虎扑网页热门评论高亮
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  在全部评论中匹配热门评论并高亮显示,避免看第二遍热门评论
// @author       hui-Zz
// @match        http*://m.ithome.com/*
// @match        http*://bbs.hupu.com/*
// @icon         https://www.emojiall.com/en/header-svg/%F0%9F%93%B0.svg
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    var num=0;
    var hn = window.location.hostname;
    if (hn == 'm.ithome.com'){
        var hotCommentElementsClass=".hot-comment .placeholder";
        var allCommentsClass=".all-comment .placeholder";
        var usernameClass=".user-name";
        var contentClass=".user-review";
        var commentContainerHotClass=".hot-comment";
        var commentContainerClass=".all-comment";
        $('.hot-comment').hide();
    }else if (hn == 'bbs.hupu.com'){
        hotCommentElementsClass=".post-reply_post-reply__D1M4P";
        allCommentsClass=".post-reply_post-reply__D1M4P";
        usernameClass=".post-reply-list-user-info-top-name";
        contentClass=".thread-content-detail";
        commentContainerHotClass=".post-reply_post-reply__D1M4P";
        commentContainerClass=".post-reply_post-reply__D1M4P";
        //$('.post-wrapper_toggle-tool__MGO_Q').click();
    }

    // 定义一个变量来存储热门评论的用户名和评论内容
    let hotComments = [];

    // 获取热门评论并保存
    function collectHotComments() {
        // 获取页面中所有热门评论的用户名和评论内容
        let hotCommentElements = document.querySelectorAll(hotCommentElementsClass); // 假设热门评论有这个类名
        if (hn == 'bbs.hupu.com'){
            hotCommentElements = hotCommentElements[0].querySelectorAll('.post-reply-list-container .reply-list-content');
        }
        hotComments = [];
        //console.info(++num);
        hotCommentElements.forEach((commentElement) => {
            const username = commentElement.querySelector(usernameClass)?.textContent.trim(); // 假设用户名有这个类名
            const content = commentElement.querySelector(contentClass)?.textContent.trim(); // 假设评论内容有这个类名
            if (username && content) {
                hotComments.push({ username, content });
            }
        });
    }

    // 在加载更多评论后进行匹配
    function highlightMatchingComments() {
        let allComments = document.querySelectorAll(allCommentsClass); // 假设所有评论有这个类名
        if (hn == 'bbs.hupu.com'){
            allComments = allComments[1].querySelectorAll('.post-reply-list-container .reply-list-content');
        }
        let readCount = 0;
        allComments.forEach((commentElement) => {
            readCount++;
            const username = commentElement.querySelector(usernameClass)?.textContent.trim();
            const content = commentElement.querySelector(contentClass)?.textContent.trim();
            hotComments.forEach((hotComment) => {
                if (username === hotComment.username && content === hotComment.content) {
                    commentElement.style.backgroundColor = 'yellow'; // 匹配成功,背景色改为黄色
                    commentElement.style.display = 'block';
                }else if (commentElement.querySelector('ul')) {
                    commentElement.style.display = 'block';
                }else if (readCount <= 10) {
                    commentElement.style.display = 'block';
                }
/*                 else if (commentElement.classList.contains('placeholder') && commentElement.classList.contains('main-floor')) {
                    commentElement.style.display = 'none';
                } */
            });
        });
    }

    // 每次热门评论加载后重新进行匹配
    const observerHot = new MutationObserver(() => {
        collectHotComments(); // 重新收集热门评论
    });

    // 监听热门评论区域的变化
    let commentContainerHot = document.querySelector(commentContainerHotClass);
    if (hn == 'bbs.hupu.com'){
        commentContainerHot = document.querySelectorAll(commentContainerHotClass);
        commentContainerHot = commentContainerHot[0];
    }
    if (commentContainerHot) {
        observerHot.observe(commentContainerHot, {
            childList: true, // 监听子元素变化
            subtree: true, // 监听子元素及其后代元素变化
        });

        // 初次加载时收集热门评论并高亮匹配
        collectHotComments();
    }

    // 每次评论加载后重新进行匹配
    const observer = new MutationObserver(() => {
        highlightMatchingComments(); // 高亮匹配评论
    });

    // 监听评论区域的变化
    let commentContainer = document.querySelector(commentContainerClass);
    if (hn == 'bbs.hupu.com'){
        commentContainer = document.querySelectorAll(commentContainerClass);
        commentContainer = commentContainer[1];
    }
    if (commentContainer) {
        observer.observe(commentContainer, {
            childList: true, // 监听子元素变化
            subtree: true, // 监听子元素及其后代元素变化
        });

        // 初次加载时收集热门评论并高亮匹配
        highlightMatchingComments();
    }
    
})();