|
发表于 2025-5-21 19:48:05
|
显示全部楼层
本帖最后由 glowkey 于 2025-5-21 20:46 编辑
我改写了一个油猴脚本 ,可以自己安装,来屏蔽指定 id 的评论。。
把其中用户名 1 和用户名 2 换成希望屏蔽的用户 id
- // ==UserScript==
- // @name Gaoloumi Forum - Block User Content (Optimized)
- // @namespace http://tampermonkey.net/
- // @version 2025-05-21.2
- // @description Hides threads, posts, and quotes from specified users on Gaoloumi forum.
- // @author You (Optimized by Gemini)
- // @match https://gaoloumi.cc/forum.php?mod=viewthread*
- // @icon https://www.google.com/s2/favicons?sz=64&domain=gaoloumi.cc
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- // --- Configuration ---
- // Add usernames to this list. For example: ["user1", "user2", "anotherUser"]
- const blockedUsernames = ["用户名 1", "用户名2"];
- // Set to false to hide content completely without any "blocked" message
- const showBlockedMessage = true;
- // --- Helper Function to Get Elements by XPath ---
- /**
- * Evaluates an XPath expression and returns an array of matching DOM elements.
- * @param {string} xpath - The XPath expression to evaluate.
- * @param {Node} parent - The context node for the XPath evaluation (defaults to document).
- * @returns {Array<Node>} An array of matching elements.
- */
- function getElementsByXPath(xpath, parent = document) {
- const results = [];
- try {
- const query = document.evaluate(xpath, parent, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
- for (let i = 0; i < query.snapshotLength; i++) {
- results.push(query.snapshotItem(i));
- }
- } catch (e) {
- console.error("Error evaluating XPath:", xpath, e);
- }
- return results;
- }
- // --- Main Blocking Logic ---
- for (const username of blockedUsernames) {
- // Escape username for use in XPath, particularly if it contains quotes.
- const escapedUsername = username.replace(/"/g, '\"').replace(/'/g, "\\'");
- // 1. Block Threads in Thread Listings
- // Targets the <tbody> element of a thread row authored by the blocked user.
- // Original XPath for 'thread' was: '//table/tbody[tr[1]/td[2]//cite/a[text()="' + username + '"]]'
- const threadRows = getElementsByXPath(
- `//tbody[starts-with(@id, "normalthread_") or starts-with(@id, "stickthread_")]
- [.//td[contains(@class, "by")]//cite//a[text()="${escapedUsername}"]] |
- //table/tbody[tr[1]/td[2]//cite/a[text()="${escapedUsername}"]]` // Original XPath as a fallback
- );
- for (const row of threadRows) {
- if (showBlockedMessage) {
- // Replace the content of the thread row with a "blocked" message.
- row.innerHTML = `<tr>
- <td class="icn"><img src="static/image/common/folder_common.gif" alt="Blocked"></td>
- <th colspan="2" class="common new" style="text-align:left; padding-left:10px;">
- <b>已屏蔽的主题 (作者: <span style="color:grey;">${username}</span>)</b>
- </th>
- <td class="by"></td><td class="num"></td><td class="by"></td>
- </tr>`;
- } else {
- // Hide the thread row completely.
- row.style.display = 'none';
- }
- }
- // 2. Block Posts in Thread View
- // Targets the main container (div or table) of a post by the blocked user.
- const postContainers = getElementsByXPath(`//div[starts-with(@id, "post_")] | //table[starts-with(@id, "pid_")]`);
- for (const postContainer of postContainers) {
- // Check for author link within this post container.
- // Common author link selectors for Discuz forums: .authi a.xw1, .pi a[href*="uid="], td.pls div.authi a
- const authorLink = postContainer.querySelector(`.authi a.xw1, .pi a[href*="uid="], td.pls div.authi a`);
- if (authorLink && authorLink.textContent.trim() === username) {
- if (showBlockedMessage) {
- // Create and insert a "blocked post" message before hiding the actual post.
- const messageDiv = document.createElement('div');
- messageDiv.className = 'blocked-post-notice';
- messageDiv.style.cssText = `
- padding: 15px;
- border: 1px dashed #ccc;
- background-color: #f9f9f9;
- text-align: center;
- margin: 10px 0;
- color: #555;
- `;
- messageDiv.innerHTML = `已屏蔽来自 <strong style="color:grey;">${username}</strong> 的帖子内容`;
- postContainer.parentNode.insertBefore(messageDiv, postContainer);
- postContainer.style.display = 'none';
- } else {
- // Hide the post container completely.
- postContainer.style.display = 'none';
- }
- }
- }
- // 3. Block Quotes by User
- // Targets blockquote elements quoting the blocked user.
- // Original XPath for 'quote' was: '//blockquote[font/a/font[contains(text(),"' + username + '")]]'
- // Refined XPath looks for common Discuz quote structures.
- const quoteBlocks = getElementsByXPath(
- `//div[contains(@class, "quote")]/blockquote[
- .//font/strong[text()="${escapedUsername}"] or
- .//a[text()="${escapedUsername}" and (contains(@href, "space-uid-") or contains(@href, "uid="))] or
- starts-with(normalize-space(.), "引用: ${escapedUsername}") or
- contains(normalize-space(.), "引用 ${escapedUsername} ")
- ] | //blockquote[font/a/font[contains(text(),"${escapedUsername}")]]` // Original XPath as a fallback
- );
- for (const blockquote of quoteBlocks) {
- // Try to get the outer .quote div for Discuz, otherwise use the blockquote itself.
- const parentQuoteDiv = blockquote.closest('.quote');
- const elementToModify = parentQuoteDiv || blockquote;
- if (showBlockedMessage) {
- // Replace the content of the quote element with a "blocked quote" message.
- const message = `<div class="blocked-quote-message" style="padding:10px; border:1px dashed #ccc; background-color:#f9f9f9; color:#555;">已屏蔽对 <strong style="color:grey;">${username}</strong> 的引用内容</div>`;
- elementToModify.innerHTML = message;
- } else {
- // Hide the quote element completely.
- elementToModify.style.display = 'none';
- }
- }
- // 4. Block "Title" sections (as per original script's intent)
- // This was for <tbody> elements where a link in a <th> contained the username.
- // Original XPath: '//table/tbody[tr[1]/th[1]//a[contains(text(),"' + username + '")]]'
- // This is assumed to be for specific sections, not general thread titles (covered in item 1).
- const titleSections = getElementsByXPath(
- `//table/tbody[tr[1]/th[1]//a[contains(text(),"${escapedUsername}")]]`
- );
- for (const section of titleSections) {
- if (showBlockedMessage) {
- // Determine colspan for the message row based on existing table structure.
- let colspan = 5; // Default colspan
- const firstTr = section.querySelector('tr');
- if (firstTr && firstTr.cells) {
- colspan = firstTr.cells.length > 0 ? firstTr.cells.length : colspan;
- }
- // Replace the entire tbody content with a message row.
- section.innerHTML = `<tr>
- <td colspan="${colspan}" style="text-align:center; padding:10px; background-color:#f9f9f9; color:grey;">
- 与 <strong style="color:#777;">${username}</strong> 相关的内容版块已被隐藏
- </td>
- </tr>`;
- } else {
- // Hide the section completely.
- section.style.display = 'none';
- }
- }
- }
- // Optional: Add some global styles for blocked messages if not relying solely on inline styles.
- // const styleSheet = document.createElement('style');
- // styleSheet.textContent = `
- // .blocked-post-notice strong, .blocked-quote-message strong {
- // font-weight: normal; /* Example style */
- // }
- // `;
- // document.head.appendChild(styleSheet);
- })();
复制代码
插件可以用 https://www.tampermonkey.net/ind ... hrome&locale=zh |
|