hostloc 自动屏蔽黑名单用户

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

目前為 2020-04-30 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();