// ==UserScript== // @name 小黑盒屏蔽插眼cy // @namespace https://scriptcat.org/zh-CN/script-show-page/3404 // @version 0.5 // @description 屏蔽小黑盒中带有插眼的评论,屏蔽cy (建议使用Chrome浏览器(电脑端)或者狐猴浏览器(手机端)) // @author beibeibeibei // @license MIT // @match https://www.xiaoheihe.cn/app/bbs/link/* // @grant GM_addStyle // ==/UserScript== (function() { 'use strict'; function hide_cha_yan() { const css = ` #beibeibeibei-hide-cha-yan-btn { position: fixed; top: 20px; left: 20px; z-index: 9999; padding: 10px 20px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3); &:hover { background-color: #45a049; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4); } &:active { background-color: #3e8e41; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.6); transform: translate(1px, 1px); } } .beibeibeibei-hide-child-cha-yan-comment { /* display: none; */ height: 0px; padding: 0; } .beibeibeibei-hide-cha-yan-comment { /* display: none; */ height: 0px; padding: 0; } .link-comment__comment-item, .comment-children-item { transition: all 0.3s; interpolate-size: allow-keywords; overflow: hidden; } `; GM_addStyle(css); // 定义一个通用方法来处理显示和隐藏操作 // hide_cha_yan_core function toggleDisplay(cySelector, cyParentSelector, styleClass, initialIsDisplayed = null) { const cha_yan_collects = document.querySelectorAll(cySelector); if (cha_yan_collects.length === 0) { return false; // 如果没有找到元素,返回 false 表示未显示 } let isDisplayed = false; if (initialIsDisplayed === null) { // 如果未手动设置 initialIsDisplayed,使用当前状态 const firstParentElement = cha_yan_collects[0].closest(cyParentSelector); isDisplayed = !firstParentElement.classList.contains(styleClass); } else { isDisplayed = Boolean(initialIsDisplayed); } // console.log({isDisplayed}); cha_yan_collects.forEach((cy) => { if ( isDisplayed ) { cy.closest(cyParentSelector).classList.add(styleClass); } else { cy.closest(cyParentSelector).classList.remove(styleClass); } }); // 返回显示状态 return isDisplayed; } const button = document.createElement('button'); const BUTTON_SHOW_TEXT = '显示cy'; const BUTTON_HIDE_TEXT = '屏蔽cy'; button.textContent = BUTTON_HIDE_TEXT; button.id = "beibeibeibei-hide-cha-yan-btn"; button.addEventListener('click', (ev) => { let isDisplayed; let child_cy_count = document.querySelectorAll(".children-item__comment-content.cy").length; let cy_count = document.querySelectorAll(".comment-item__content.cy").length; if (child_cy_count > 0 && cy_count > 0) { // 如果两种插眼评论都存在,那么就使用同一个 isDisplayed // 楼中楼插眼 // .children-item__comment-content.cy // .comment-children-item isDisplayed = toggleDisplay(".children-item__comment-content.cy", ".comment-children-item", 'beibeibeibei-hide-child-cha-yan-comment'); // 普通插眼 // .comment-item__content.cy // .link-comment__comment-item toggleDisplay(".comment-item__content.cy", ".link-comment__comment-item", 'beibeibeibei-hide-cha-yan-comment', isDisplayed); } else if (child_cy_count === 0 && cy_count === 0) { // 两种插眼评论都没有,那就显示弹窗提示 alert("没有插眼的评论"); } else if (child_cy_count !== 0 && cy_count === 0) { // 只有楼中楼插眼评论 isDisplayed = toggleDisplay(".children-item__comment-content.cy", ".comment-children-item", 'beibeibeibei-hide-child-cha-yan-comment'); } else if (child_cy_count === 0 && cy_count !== 0) { // 只有普通楼插眼评论 isDisplayed = toggleDisplay(".comment-item__content.cy", ".link-comment__comment-item", 'beibeibeibei-hide-cha-yan-comment'); } // 动态修改按钮文本 button.textContent = isDisplayed ? BUTTON_SHOW_TEXT : BUTTON_HIDE_TEXT; }); document.body.appendChild(button); } if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", hide_cha_yan); } else { hide_cha_yan(); } })();