CC98 Tools - Block List

CC98 tools for blocking user, title, content and board.

Versione datata 26/02/2022. Vedi la nuova versione l'ultima versione.

// ==UserScript==
// @name         CC98 Tools - Block List
// @version      0.0.1
// @description  CC98 tools for blocking user, title, content and board.
// @icon         https://www.cc98.org/static/98icon.ico

// @author       ml98
// @namespace    https://www.cc98.org/user/name/ml98
// @license      MIT

// @match        https://www.cc98.org/*
// @match        http://www-cc98-org-s.webvpn.zju.edu.cn:8001/*
// @grant        none
// ==/UserScript==

/* 屏蔽列表 */
const CONFIG = {
    userName: [], /* 用户名,如 ["ml98"] */
    title: [], /* 标题关键字,如 ["男生进", "女生进"] */
    content: [], /* 帖子内容关键字,如 ["欧蓝德"] */
    board: [], /* 版面id,如 [182, 357, 758] */
};


const log = () => {};
CONFIG.content.push(...(CONFIG.userName).map(userName => `楼:用户${userName}在`));
log('config', CONFIG);

const isBlockedId = (_id) => _id && CONFIG.userName.includes(_id);
const isBlockedTitle = (_title) => _title && CONFIG.title.some((t) => _title.includes(t));
const isBlockedContent = (_content) => _content && CONFIG.content.some((c) => _content.includes(c));
const isBlockedBoard = (_boardId) => _boardId && CONFIG.board.includes(_boardId);

const topicRegExp = new RegExp("/board/\\d+/topic");
const postRegExp = new RegExp("/Topic/\\d+/(hot-)?post");

const isTopicAPI = (url) => url.includes("/topic/new") ||
      url.includes("/me/custom-board/topic") ||
      url.includes("/topic/search?keyword=") ||
      topicRegExp.test(url);
const isPostAPI = (url) => postRegExp.test(url);
const isIndexAPI = (url) => url.includes("/config/index");

const resolve = (url, data) => {
    log(url);
    log('before', data);
    if (isTopicAPI(url)) {
        data = data.filter(
            (r) =>
            !(
                isBlockedId(r.userName) ||
                isBlockedTitle(r.title) ||
                isBlockedBoard(r.boardId)
            )
        );
    } else if (isPostAPI(url)) {
        data = data.filter(
            (r) =>
            !(
                isBlockedId(r.userName) ||
                isBlockedContent(r.content) ||
                isBlockedBoard(r.boardId)
            )
        );
    } else if (isIndexAPI(url)) {
        data.hotTopic = data.hotTopic.filter(
            (r) =>
            !(
                isBlockedId(r.authorName) ||
                isBlockedTitle(r.title) ||
                isBlockedBoard(r.boardId)
            )
        );
    }
    log('atfer', data);
    return data;
};

const origFetch = window.fetch;
window.fetch = async (...args) => {
    log('fetch', args);
    const response = await origFetch(...args);
    response.json = function () {
        return Response.prototype.json
            .call(this)
            .then((data) => resolve(response.url, data));
    };
    return response;
};