Greasy Fork

Greasy Fork is available in English.

BDWM-Hide

北大未名根据版面名称,屏蔽版面

当前为 2024-01-09 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         BDWM-Hide
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  北大未名根据版面名称,屏蔽版面
// @author       lanvent
// @match        https://bbs.pku.edu.cn/v2/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=pku.edu.cn
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// ==/UserScript==

let blockWords = GM_getValue('BDWM_blockblocks', []);

function setBlockWords(str){
    blockWords = str.replace(',',',').split(',').map(word => word.trim()).filter(word => word.length > 0);
    GM_setValue('BDWM_blockblocks', blockWords);
    removeBoardBlocks();
    removeTitles();
}

function setBlockWordsPrompt() {
    const userInput = prompt('请输入要屏蔽的版面名称,用逗号分隔:', blockWords.join(', '));
    if (userInput !== null) {
        setBlockWords(userInput);
    }
}

function addSettingsButton() {
    const container = document.querySelector('div.extend-menu.setting-extend-menu > div.content');
    if(container == null || container.querySelector('a[name="block-keywords-button"]')){
        return;
    }
    const settingsButton = document.createElement('a');
    settingsButton.textContent = '屏蔽版面设置';
    settingsButton.name = 'block-keywords-button';
    settingsButton.addEventListener('click', function(event) {
        event.preventDefault();
        setBlockWordsPrompt();
    });
    const logoutButton = container.querySelector('a.btn-logout');
    container.insertBefore(settingsButton,logoutButton);
}

function addCustomSettingRow() {
    const form = document.getElementById('preference-form');
    if(form==null || form.querySelector('input[name="block-keywords"]')){
        return;
    }
    const settingRow = document.createElement('div');
    settingRow.className = 'form-row';
    settingRow.innerHTML = `
            <label>屏蔽版面名称</label>
            <div style="display: inline-block">
                    <input type="text" name="block-keywords" placeholder="输入关键词,用逗号分隔" style="margin: 1px 0;
    padding: 0 0 0 0px;
    height: 28px;
    line-height: 28px;
    width: 338px;
    border: 1px solid #e5e5e5;
    border-radius: 5px;
    text-indent: 10px"/>
            </div>
        `;
    blockWords.join(', ')
    const submitButtonRow = form.querySelector('.extra-margin-top');
    form.insertBefore(settingRow, submitButtonRow);
    const keywordsInput = settingRow.querySelector('input[name="block-keywords"]');
    keywordsInput.value = blockWords.join(', ');
    const applyButton = form.querySelector('.orange-large-btn');
    applyButton.addEventListener('click', function(event){
        setBlockWords(keywordsInput.value);
    });
}

function removeBoardBlocks() {
    //版面
    let boardBlocks = document.querySelectorAll('.boards-wrapper.list > div.set');
    if(boardBlocks!=null){
        boardBlocks.forEach(block => {
            let content = block.querySelector('span.name').textContent;
            if (content && blockWords.some(word => word===content)){
                console.log("remove",block);
                block.remove();
            }
        });
    }
    boardBlocks = document.querySelectorAll('.boards-wrapper.hots > div.board-block');
    if(boardBlocks!=null){
        boardBlocks.forEach(block => {
            let content = block.querySelector('span.name').textContent;
            if (content && blockWords.some(word => word===content)){
                console.log("remove",block);
                block.remove();
            }
        });
    }
}

function removeTitles() {
    //主页榜单
    let titles = document.querySelectorAll('li');
    if(titles){
        titles.forEach(title => {
            let content = title.querySelector('a.post-link')?.textContent;
            if (content && blockWords.some(word => content.startsWith(word+' '))){
                console.log("remove",title);
                title.remove();
            }
            content = title.querySelector('a.topic-link')?.textContent;
            if (content && blockWords.some(word => content.startsWith('['+word+']'))){
                console.log("remove",title);
                title.remove();
            }
        });
    }
    titles = document.querySelectorAll('p > a.topic-link');
    if(titles){
        titles.forEach(title => {
            if (blockWords.some(word => title.textContent.startsWith('['+word+']'))){
                console.log("remove",title);
                title.remove();
            }
        });
    }
    titles = document.querySelectorAll('a.inline-link');
    if(titles){
        titles.forEach(title => {
            if (blockWords.some(word => title.textContent.startsWith('['+word+']'))){
                console.log("remove",title);
                title.remove();
            }
        });
    }
}
(function() {
    'use strict';
    const config = { childList: true, subtree: true };
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.type === 'childList') {
                observer.disconnect();
                addSettingsButton();
                addCustomSettingRow();
                observer.observe(document.body, config);
                removeBoardBlocks();
                removeTitles();
            }
        });
    });
    observer.observe(document.body, config);
})();