oldip / 删除淘宝/天猫默认评论

// ==UserScript==
// @name         删除淘宝/天猫默认评论
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  删除淘宝/天猫包含指定关键词的评论
// @author       oldip
// @match        https://item.taobao.com/item.htm?*
// @match        https://detail.tmall.com/item.htm?*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // 定义需要完全匹配的关键词数组
    const keywords = [
        "评价方未及时做出评价,系统默认好评!",
        "此用户没有填写评价。",
        "系统默认评论",
        "15天内买家未作出评价"
    ];

    // 主函数:搜索并删除符合条件的评论内容
    function removeDefaultFeedback(root = document) {
        // 选取所有包含 "--content--_" 的元素
        const contentElements = root.querySelectorAll('[class*="--content--_"]');
        contentElements.forEach(contentEl => {
            // 当前元素的文本内容完全匹配任一关键词
            if (keywords.includes(contentEl.innerText.trim())) {
                // 必须存在一个包含 "--Comment--_" 的父容器,若找不到,则认定它不是评论,直接返回
                const parentComment = contentEl.closest('[class*="--Comment--_"]');
                if (!parentComment) {
                    return;
                }
                // 检查该父容器是否存在多个 "--content--_" 的元素
                const contentItems = parentComment.querySelectorAll('[class*="--content--_"]');
                if (contentItems.length > 1) {
                    // 若存在多个,则跳过删除(可能为追评或其他扩展内容)
                    return;
                }
                // 如果父容器内有任何包含 "--photo--" 的图片或影片,也不进行删除
                const hasPhoto = parentComment.querySelector('[class*="--photo--"]') !== null;
                if (hasPhoto) {
                    return;
                }
                // 条件满足,删除该评论父容器
                parentComment.remove();
            }
        });
    }

    // DOM观察器,用于处理动态加载的内容
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === 1) {
                    removeDefaultFeedback(node); // 检查新增的节点
                }
            });
        });
    });

    // 开启观察整个页面
    observer.observe(document.body, { childList: true, subtree: true });

    // 初次加载时清理页面上的匹配评论
    removeDefaultFeedback();
})();