Skip to content

Instantly share code, notes, and snippets.

@AllanChain
Last active June 24, 2021 02:54
Show Gist options
  • Save AllanChain/50e2810d7abf54fe4fb09c175d5f00e7 to your computer and use it in GitHub Desktop.
Save AllanChain/50e2810d7abf54fe4fb09c175d5f00e7 to your computer and use it in GitHub Desktop.
未名 BBS 屏蔽用户脚本
// ==UserScript==
// @name BDWM Block Board Modified
// @version 2.0
// @description BDWM_BLOCK by motaguoke, modified by Allan Chain
// @icon https://bbs.pku.edu.cn/favicon.ico
// @include http://bbs.pku.edu.cn/*
// @include https://bbs.pku.edu.cn/*
// @include https://*.bdwm.net/*
// @include http://*.bdwm.net/*
// @homepageURL https://gist.github.com/AllanChain/50e2810d7abf54fe4fb09c175d5f00e7
// @supportURL https://gist.github.com/AllanChain/50e2810d7abf54fe4fb09c175d5f00e7
// @updateURL https://gist.github.com/AllanChain/50e2810d7abf54fe4fb09c175d5f00e7/raw/BDWM_BLOCK.user.js
// @downloadURL https://gist.github.com/AllanChain/50e2810d7abf54fe4fb09c175d5f00e7/raw/BDWM_BLOCK.user.js
// @note 2021-06-24 v2.0 加了个屏蔽版面内用户主题帖的功能,修复了刷新页面被屏蔽内容会闪的问题,改了一下变量名
// @note 2021-01-15 v1.1 ES6 改写
// @run-at document-body
// ==/UserScript==
/** 字符串,首页屏蔽的版面,为了匹配简单,每个版面用方括号括起 */
const BLOCK_BOARDS = '[别问我是谁]'
/** 列表,屏蔽用户的 ID */
const BLOCK_USERIDS = ['Margulies']
console.log(`BDWM_BLOCK by motaguoke Version: 2.0`)
/**
* 屏蔽首页上不想看的版面
*/
const blockHomepageBoards = () => {
for (const homepageBoardLink of document.getElementsByClassName('topic-link')) {
if (BLOCK_BOARDS.includes(homepageBoardLink.innerText)) {
homepageBoardLink.innerText = '[已屏蔽版面]'
homepageBoardLink.href = 'javascript:void(0)'
homepageBoardLink.nextSibling.href = 'javascript:void(0)'
homepageBoardLink.nextSibling.innerText = '屏蔽版面的话题'
}
}
}
/**
* 屏蔽用户发言
*/
const blockPostCard = () => {
for (const postCard of document.getElementsByClassName('post-card')) {
const usernameElement = postCard.getElementsByClassName('username')[0].firstChild
const username = usernameElement.innerText
if (BLOCK_USERIDS.includes(username)) {
//BLOCK CONTENT
const postContent = postCard.getElementsByClassName('body file-read image-click-view')[0]
postContent.innerText = '[bdwm屏蔽插件] 该用户的发言已被您屏蔽!'
postContent.style.color = 'red'
//BLOCK USERNAME
usernameElement.innerText = '屏蔽用户'
const portraitElement = postCard.getElementsByClassName('portrait')[0]
portraitElement.src = 'https://bbs.pku.edu.cn/v2/images/user/portrait-neu.png'
}
}
}
/**
* 屏蔽版面界面显示的用户发帖
*/
const blockTopicItem = () => {
for (const topicItem of document.getElementsByClassName('list-item-topic')) {
for (const authorElement of topicItem.getElementsByClassName('author')) {
const authorNameElement = authorElement.getElementsByClassName('name')[0]
const authorName = authorNameElement.innerText
if (BLOCK_USERIDS.includes(authorName)) {
authorNameElement.innerText = '屏蔽用户'
topicItem.querySelector('.title-cont .title').innerText = '屏蔽用户的话题'
topicItem.querySelector('a').href = 'javascript:void(0)'
// 如果是楼主而不是最后发言
const previousElement = authorElement.previousElementSibling
if (Array.from(previousElement.classList).includes('avatar')) {
previousElement.querySelector('img').src =
'https://bbs.pku.edu.cn/v2/images/user/portrait-neu.png'
}
}
}
}
}
new MutationObserver(() => {
blockHomepageBoards()
blockPostCard()
blockTopicItem()
}).observe(document.documentElement, {
childList: true,
subtree: true
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment