本帖最后由 Corei7 于 2022-8-13 12:25 编辑
参考自:https://www.52pojie.cn/thread-1058012-1-1.html 备份帖
屏蔽黑名单用户的帖子/评论/点评,代码有点丑,没学过JS
如果实在忍不住要键政,可以点击按钮显示垃圾内容
测试用例们稍后将会在本贴回复,进行测试 let blockList = new Array("苏州思杰马克丁", "小学生", "lbw", "chmood", "比特鼻", "表妹", "HOH");
有人可能会问,你这个for循环,垃圾用户一多会不会卡啊,如果确实黑名单用户太多,说明这个论坛已经不适合你了,早点撤退
效果图
- // ==UserScript==
- // [url=home.php?mod=space&uid=5839]@name[/url] LOC垃圾桶
- // @namespace Violentmonkey Scripts
- // @match https://hostloc.com/*
- // @grant none
- // @version 1.0
- // @author mjj
- // @description 2022/8/12 23:34:55
- // ==/UserScript==
- (function () {
- ‘use strict’;
- let showContext = "显示垃圾";
- let hideContext = "隐藏垃圾";
- let BUT_NONE = "none";
- let BUT_BLOCK = "block";
- let DIV_PREFIX = "bdiv";
- let DIV_COMMENT_PREFIX = "bdogCommentDiv";
- let x;
-
- // 需要屏蔽的用户名列表
- let blockList = new Array("苏州思杰马克丁", "小学生", "lbw", "chmood", "比特鼻", "表妹", "HOH");
-
-
-
- // 处理评论
- function handleDiv(x) {
- let divTemp = document.evaluate(‘//table/tbody[tr[1]/td[1]//a[text()="’ + blockList[x] + ‘"]]’, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
- if (!divTemp.snapshotLength) {
- return;
- }
- for (let i = 0, c = ""; i < divTemp.snapshotLength; i++) {
- let curDiv = divTemp.snapshotItem(i).firstChild.childNodes[3];
- if (curDiv.childNodes[2] == null || curDiv.childNodes[2].tagName !== ‘DIV’) {
- continue;
- }
- let id = x + i;
- curDiv.setAttribute("id", DIV_PREFIX + id);
- curDiv.childNodes[2].style.display = BUT_NONE;
- let button = createButton(curDiv, id, DIV_PREFIX);
- button.style.height = "40px";
- button.style.width = "80%";
- }
- }
-
- // 处理点评
- function handleComment(x) {
- let commentTemp = document.evaluate(‘//table/tbody[tr[1]/td[2]/div[2]//a[text()="’ + blockList[x] + ‘"]]’, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
- if (!commentTemp.snapshotLength) {
- return;
- }
- for (let i = 0, c = ""; i < commentTemp.snapshotLength; i++) {
- let curDiv = commentTemp.snapshotItem(i).firstChild.childNodes[3].childNodes[2].childNodes[0].childNodes[3];
- if (!curDiv.hasChildNodes()) {
- continue;
- }
- let children = curDiv.childNodes;
- for (let j = 0; i < children.length; i++) {
- let curComment = children[i];
- if (curComment.className !== "pstl xs1 cl" || blockList[x] !== curComment.childNodes[1].childNodes[3].innerText) {
- continue;
- }
- let id = x + i + j;
- let curCommentDiv = curComment.childNodes[3];
- curCommentDiv.setAttribute("id", DIV_COMMENT_PREFIX + id);
- curCommentDiv.style.display = BUT_NONE;
- let button = createButton(curComment, id, DIV_COMMENT_PREFIX);
- button.style.width = "25%";
- }
- }
- }
-
- // 生成按钮-显示/屏蔽内容
- function createButton(curDiv, buttonId, buttonPrefix) {
- let button = document.createElement("input");
- button.setAttribute("type", "button");
- button.setAttribute("id", buttonId);
- button.style.textalign = "center";
- button.setAttribute("value", showContext);
- button.onclick = function () {
- let dg = document.getElementById(buttonPrefix + this.id);
- if (this.value === hideContext) {
- this.value = showContext;
- if (buttonPrefix === DIV_PREFIX) {
- dg.childNodes[2].style.display = BUT_NONE;
- } else {
- dg.style.display = BUT_NONE;
- }
- } else {
- this.value = hideContext;
- if (buttonPrefix === DIV_PREFIX) {
- dg.childNodes[2].style.display = BUT_BLOCK;
- } else {
- dg.style.display = BUT_BLOCK;
- }
- }
- };
- curDiv.appendChild(button);
- return button;
- }
-
- for (x in blockList) {
- handleDiv(x);
- handleComment(x);
- }
-
- })();
复制代码
|