hostloc 自动屏蔽黑名单用户

自动获取 hostloc 的黑名单,并屏蔽相应帖子

当前为 2020-04-30 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         hostloc 自动屏蔽黑名单用户
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动获取 hostloc 的黑名单,并屏蔽相应帖子
// @author       inch
// @match        http*://*.hostloc.com/*
// @grant        unsafeWindow
// ==/UserScript==

(function () {
    'use strict';
    var CONFIG = {
        blockList: [],
        blockedPIDs: [],
        blockedCount: 0
    }
    // 通过黑名单页面获取黑名单
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/home.php?mod=space&do=friend&view=blacklist', false);
    xhr.send();
    if (xhr.status === 200) {
        var rText = xhr.responseText;
        var rList = rText.match(/<a\shref="space-uid-.+html">([^<].+)<\/a>/g);
        var bList = rList.map(i => {
            return i.replace(/<.+?>/g, '')
        });
        CONFIG.blockList = bList;
    }
    // 帖子列表页面
    var authorNodes = document.querySelectorAll('th + .by cite a');
    authorNodes.forEach(function (item) {
        if (CONFIG.blockList.includes(item.innerText.trim())) {
            var $wrapper = item.parentElement.parentElement.parentElement.parentElement;
            var $list = $wrapper.parentElement;
            $list.removeChild($wrapper);
            CONFIG.blockedCount++;
        }
    });
    // 帖子列表点击下一页
    var $postList = document.querySelector('#threadlisttableid');
    if ($postList) {
        var post_mo = new MutationObserver(function (mList) {
            authorNodes = document.querySelectorAll('th + .by cite a');
            authorNodes.forEach(function (item) {
                if (CONFIG.blockList.includes(item.innerText.trim())) {
                    var $wrapper = item.parentElement.parentElement.parentElement.parentElement;
                    var $list = $wrapper.parentElement;
                    $list.removeChild($wrapper);
                    CONFIG.blockedCount++;
                    console.log('Blocked: ' + CONFIG.blockedCount);
                }
            });
        });
        post_mo.observe($postList, {
            childList: true
        });
    }
    // 帖子详情页面
    authorNodes = document.querySelectorAll('.authi a.xw1');
    authorNodes.forEach(function (item) {
        if (CONFIG.blockList.includes(item.innerText.trim())) {
            var $wrapper = item.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
            var id = Number(item.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.id.replace('pid', ''));
            CONFIG.blockedPIDs.push(id);
            var $list = $wrapper.parentElement;
            $list.removeChild($wrapper);
            CONFIG.blockedCount++;
        }
    });
    // 针对隐藏楼层
    if (unsafeWindow.blockedPIDs) {
        CONFIG.blockedPIDs.forEach(function (id) {
            for (var i = 0; i < unsafeWindow.blockedPIDs.length; i++) {
                if (unsafeWindow.blockedPIDs[i] === id) {
                    unsafeWindow.blockedPIDs.splice(i, 1);
                }
            }
        });
        if (!unsafeWindow.blockedPIDs.length) {
            document.querySelector('#hiddenpoststip').style.display = 'none';
        }
    }
    console.log('Blocked: ' + CONFIG.blockedCount);
})();