// ==UserScript==
// @name 水源低质量发帖过滤器
// @license MIT
// @author benderbd42
// @namespace https://gf.qytechs.cn/scripts/480756
// @version 2.4.1.1
// @description 从水源主页直接屏蔽常见类型的低质量发帖,如重复提问、戾气帖子、日经抱怨等。支持手机电脑双平台,支持自定义屏蔽规则。默认规则开箱即用。
// @match https://shuiyuan.sjtu.edu.cn/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var removeMode = "remove";
var device;
var counter = 0;//屏蔽计数器
var blacklistKeywords = ["😅", "贵物", "插头", "图书馆键盘", "占座", "交小苗", "拔电", "电动车充", "充电桩", "拔充", "拔插", "车充", "拔线", "恶心", "分流", "被拔", "乱拔", "偷充", "别拔", "拔我", "拔人", "你妈", "傻逼", "脑瘫", "死妈", "死个妈", "奇葩", "能洗鞋", "宿舍洗衣机", "有病", "素质"];
var whitelistWords = ["投喂", "公告", "搭子", "分享", "建一", "指北", "指南", "生存", "教程", "日常", "笔记", "接龙", "飞花令", "交流", "学长", "学姐", "脱单", "电台", "挑战", "每日", "闲谈"];//最高优先级:有白名单总会不屏蔽
var clickbaitList = ["!", ":", ",", "禁", "官", "竟"];
var whatsgoingonList = ["发生什", "发生啥", "怎么了", "怎么回事", "了?", "救护车", "向?", "西一", "西二", "西三", "东一", "东二", "东三", "什么事"];
var customBlackList = [];
// 检测 customBlacklistWord 是否存在自定义值
if (localStorage.getItem('customBlacklistWord')) {
var customBlacklistWord = localStorage.getItem('customBlacklistWord');
blacklistKeywords = customBlacklistWord.split(',');
}
// 检测 customWhitelistWord 是否存在于 localStorage
if (localStorage.getItem('customWhitelistWord')) {
var customWhitelistWord = localStorage.getItem('customWhitelistWord');
whitelistWords = customWhitelistWord.split(',');
}
// 检测 customcustomBlacklistWord 是否存在于 localStorage
if (localStorage.getItem('customcustomBlacklistWord')) {
var customcustomBlacklistWord = localStorage.getItem('customcustomBlacklistWord');
customBlackList = customcustomBlacklistWord.split(',');
}
// 检测各屏蔽组件启用情况
var isClickbaitBlockingOn = getRuleStatus(0);
var isWhatsgoingonBlockingOn = getRuleStatus(1);
var isCutsomBlacklistBlockingOn = getRuleStatus(2);
// 主页已屏蔽数量文字提示
var blockTextInfo = "😍: 0";
var blocktext = document.createElement('div');
blocktext.innerText = blockTextInfo;
blocktext.style.height = '100%';
blocktext.style.display = 'flex';
blocktext.style.alignItems = 'center';
blocktext.style.justifyContent = 'center';
blocktext.style.padding = '0 10px';
blocktext.classList.add('block-number-text');
blocktext.addEventListener('click', function() {
if(confirm("水源低质量发帖过滤器运行中,前往“偏好设置-界面”以自定义过滤器功能。\n\n确认前往?")){
console.log('userId: page jump, id = '+ getUserId());
window.open('https://shuiyuan.sjtu.edu.cn/u/' + getUserId() + '/preferences/interface', '_blank');
}
});
//屏蔽模式自动识别
var htmlElement = document.documentElement;
var isDeviceMobile = htmlElement.classList.contains('mobile-view');//已解决~~移动端设备采用无感屏蔽存在无法无限加载的问题。移动端不启用移除模式,而是使用隐藏模式,保留控件占位~~
if(isDeviceMobile){
device = "mobile";
}
else{
device = "others";
}
if (localStorage.getItem('removeMode') === null) {
//if (device == "mobile"){
// removeMode = "hide";
//}
removeMode = 'remove';
localStorage.setItem('removeMode', removeMode);
}
else{
removeMode = localStorage.getItem('removeMode');
}
//顺便读取过短标题阈值长度等阈值信号
var shortTitleThresh = localStorage.getItem('shortTitleThresh');
if (shortTitleThresh === null){
shortTitleThresh = 6;//默认
}
var fewReplyThresh = localStorage.getItem('fewReplyThresh');
if (fewReplyThresh === null){
fewReplyThresh = 0;//默认
}
function getCurrentDate() {
var today = new Date();
var year = today.getFullYear();
var month = String(today.getMonth() + 1).padStart(2, '0');
var day = String(today.getDate()).padStart(2, '0');
var currentDate = year + '-' + month + '-' + day;
console.log('currentDate:'+currentDate);
return currentDate;
}
//屏蔽日志
function blockLogWrite(blockExplanation){
//blockExplanation
var blocklogtext = "";
var blockLogDate = localStorage.getItem("blockLogDateV1")
if(blockLogDate==getCurrentDate()){
blocklogtext = localStorage.getItem("blockLogV1")
}
//if(removeMode=="hide"){
// //hide模式会反复写入同一个标题
if(!blocklogtext.includes(blockExplanation)){
localStorage.setItem('blockLogV1', blockExplanation+"\n\n"+blocklogtext);
localStorage.setItem('blockLogDateV1', getCurrentDate());
}
//}
//else{
// localStorage.setItem('blockLogV1', blockExplanation+"\n"+blocklogtext);
// localStorage.setItem('blockLogDateV1', getCurrentDate());
//}
}
function blockLogRead() {
event.preventDefault();
var blockLogDate = localStorage.getItem("blockLogDateV1");
var blockLogText = localStorage.getItem("blockLogV1");
console.log("blockLogRead_blockLogDateV1:" + blockLogDate);
// 创建对话框的内容
var blockLogDialogContent = `
blockLogDate: ${blockLogDate}
blockLogText:
${blockLogText}
[end]`;
// 弹出对话框
window.alert(blockLogDialogContent);
}
//屏蔽函数
function block(subject){
if(removeMode=="remove"){
if(isDeviceMobile){
subject.parentElement.remove();
}
else{
subject.remove();
}
}
else if(removeMode=="hide"){
subject.style.visibility = 'hidden';
}
else{
console.log("错误:不存在的removeMode");
}
}
// 查找目标位置并添加按钮
function addTextToContainer(num) {
if(num == 0){
blockTextInfo = "😍: 0";
}
else{
blockTextInfo = "🚫: "+num;
}
var container = document.getElementById('navigation-bar');
if(container){
var existingBlockText = container.querySelector('.block-number-text');
if(existingBlockText){
existingBlockText.innerText = blockTextInfo;
}
else{
var firstChild = container.firstElementChild;
container.insertBefore(blocktext, firstChild);
}
}
}
setInterval(function() {
// 获取所有需要检测的元素
var elements = document.querySelectorAll('a[role="heading"]');
console.log('元素数量:', elements.length);
var isTrashContentFlag;
var blockExplanation;
// 屏蔽检测与删除
for (var i = 0; i < elements.length; i++) {
isTrashContentFlag = false;
//---白名单检测---
var element = elements[i];
var replyNum = getReplyNum(element);
var textContent = element.innerText.trim();
blockExplanation = '['+textContent+'] '
var containsWhitelistWord = false;
for (var j = 0; j < whitelistWords.length; j++) {
if (textContent.includes(whitelistWords[j])) {
containsWhitelistWord = true;
console.log('白名单通过:', textContent, ' ', whitelistWords[j]);
break;
}
}
//---白名单检测:结束---
var parentElement = element.parentElement;
// 1. 如果文本内容少于等于六个字,判定为过短标题
if (textContent.length <= shortTitleThresh) {
console.log('标记:', textContent);
if (parentElement && parentElement.parentElement && parentElement.parentElement.parentElement) {
isTrashContentFlag = true;
blockExplanation = blockExplanation+"规则1:过短标题(文本内容少于等于一定长度:"+shortTitleThresh+");";
//parentElement.parentElement.parentElement.style.display = 'none';
//parentElement.parentElement.parentElement.style.height = '0';
//parentElement.parentElement.parentElement.style.visibility = 'hidden';
//parentElement.parentElement.parentElement.style.opacity = '0';
//parentElement.parentElement.parentElement.remove();
//block(parentElement.parentElement.parentElement);
//counter++;
}
}
// 2. 检查文本是否包含关键词
var containsKeyword = false;
for (j = 0; j < blacklistKeywords.length; j++) {
if (textContent.includes(blacklistKeywords[j])) {
containsKeyword = true;
console.log('关键词检测:', textContent, ' ', blacklistKeywords[j]);
break;
}
}
if (containsKeyword) {
console.log('标记:', textContent);
if (parentElement && parentElement.parentElement && parentElement.parentElement.parentElement) {
isTrashContentFlag = true;
blockExplanation = blockExplanation+"规则2:黑名单屏蔽(标题命中黑名单关键词);";
}
}
// 3. 标题党专项屏蔽:逻辑为,短字段、含有敏感字符(!:,禁官竟),将导致指标增加
var clickbaitScore = 0;
for (j = 0; j < clickbaitList.length; j++) {
if (textContent.includes(clickbaitList[j])) {
clickbaitScore++;
}
}
if(textContent.length <= 12){clickbaitScore++;}
if(textContent.length <= 6){clickbaitScore++;}
if(textContent.length >= 20){clickbaitScore--;}
if (clickbaitScore>=3 && isClickbaitBlockingOn) {
console.log('标记:', textContent);
parentElement = element.parentElement;
if (parentElement && parentElement.parentElement && parentElement.parentElement.parentElement) {
isTrashContentFlag = true;
blockExplanation = blockExplanation+"规则3:疑似标题党(标题党指数过高:clickbaitScore="+clickbaitScore+");";
}
}
// 4. 发生什么事了专项屏蔽:逻辑为,短字段、含有敏感字符将导致指标上升
var whatsgoingonScore = 0;
for (j = 0; j < whatsgoingonList.length; j++) {
if (textContent.includes(whatsgoingonList[j])) {
whatsgoingonScore++;
}
}
if(textContent.length <= 15){whatsgoingonScore++;}
if(textContent.length >= 25){whatsgoingonScore--;}
if (whatsgoingonScore>=2 && isWhatsgoingonBlockingOn) {
console.log('标记:', textContent);
parentElement = element.parentElement;
if (parentElement && parentElement.parentElement && parentElement.parentElement.parentElement) {
isTrashContentFlag = true;
blockExplanation = blockExplanation+"规则4:疑似“发生什么事了”帖(“吃瓜”指数过高:whatsgoingonScore="+whatsgoingonScore+");";
}
}
//customBlackList
//5. 额外黑名单
var customblacklistScore = 0;
for (j = 0; j < customBlackList.length; j++) {
if (textContent.includes(customBlackList[j])) {
customblacklistScore++;
}
}
//if(textContent.length <= 12){customblacklistScore++;}
//if(textContent.length >= 20){customblacklistScore--;}
if (customblacklistScore>=1 && isCutsomBlacklistBlockingOn) {
console.log('标记:', textContent);
parentElement = element.parentElement;
if (parentElement && parentElement.parentElement && parentElement.parentElement.parentElement) {
isTrashContentFlag = true;
blockExplanation = blockExplanation+"规则5:命中额外黑名单关键词";
}
}
//6. 回复数量过少
console.log('回复+数量:',textContent + ' ' +replyNum);
if(replyNum<fewReplyThresh){
if (parentElement && parentElement.parentElement && parentElement.parentElement.parentElement) {
isTrashContentFlag = true;
blockExplanation = blockExplanation+"规则6:回复数量过少,replyNum="+replyNum+'<'+fewReplyThresh;
}
}
//---开始屏蔽----
if(isTrashContentFlag && !containsWhitelistWord){
block(parentElement.parentElement.parentElement);
blockLogWrite(blockExplanation);
counter++;
}
if(isTrashContentFlag && containsWhitelistWord){
blockLogWrite(blockExplanation+'【白名单保护】未屏蔽');
}
//---开始屏蔽:完成----
}
//显示屏蔽数量
addTextToContainer(counter);
if(removeMode=="hide"){
counter = 0;
}
}, 500);
function addSettingArea() {
//<div class="control-group theme" data-setting-name="user-theme">
var targetElement = document.querySelector('div[data-setting-name="user-theme"]');
var createdElement = document.querySelector('.lowQualityPostsBlocker');
// 检测指定代码是否存在
if (targetElement && !createdElement) {
console.log("已找到control-group theme标签");
// 创建外层容器
var containerElement = document.createElement('div');
containerElement.style.backgroundColor = '#f4f4f4';
containerElement.style.padding = '10px';
containerElement.style.marginBottom = '10px';
containerElement.classList.add("lowQualityPostsBlocker");
// 创建标题元素
var titleElement = document.createElement('h2');
titleElement.style.fontWeight = 'bold';
titleElement.style.color = 'black';
titleElement.style.marginBottom = '10px';
titleElement.textContent = '低质量发帖过滤器';
containerElement.appendChild(titleElement);
//分割线
var separator0 = document.createElement('hr');
containerElement.appendChild(separator0);
var titleElement0 = document.createElement('h3');
titleElement0.style.fontWeight = 'bold';
titleElement0.style.color = 'black';
titleElement0.style.marginBottom = '10px';
titleElement0.textContent = '关于与调试';
containerElement.appendChild(titleElement0);
// 创建按钮容器
var buttonsContainer = document.createElement('div');
buttonsContainer.style.display = 'flex';
buttonsContainer.style.gap = '10px';
buttonsContainer.style.marginTop = '10px';
containerElement.appendChild(buttonsContainer);
// 创建脚本主页按钮
var homepageButton = document.createElement('button');
homepageButton.textContent = '脚本主页';
homepageButton.style.padding = '6px 12px';
homepageButton.style.backgroundColor = '#ccc';
homepageButton.style.border = 'none';
homepageButton.style.color = '#000';
homepageButton.addEventListener('click', function() {
event.preventDefault();
window.open('https://gf.qytechs.cn/scripts/480756', '_blank');
});
buttonsContainer.appendChild(homepageButton);
// 创建查看日志按钮
var logButton = document.createElement('button');
logButton.textContent = '查看日志';
logButton.style.padding = '6px 12px';
logButton.style.backgroundColor = '#ccc';
logButton.style.border = 'none';
logButton.style.color = '#000';
logButton.addEventListener('click', blockLogRead);
buttonsContainer.appendChild(logButton);
//分割线
var separator1 = document.createElement('hr');
containerElement.appendChild(separator1);
var titleElement1 = document.createElement('h3');
titleElement1.style.fontWeight = 'bold';
titleElement1.style.color = 'black';
titleElement1.style.marginBottom = '10px';
titleElement1.textContent = '屏蔽词库自定义';
containerElement.appendChild(titleElement1);
// 创建文本框
var textBox = document.createElement('textarea');
textBox.rows = 3;
textBox.placeholder = '输入关键词,用英文逗号分割。例如:拔插头,震惊,占座';
textBox.style.marginTop = '10px';
containerElement.appendChild(textBox);
// 创建单选框容器
var radioContainer = document.createElement('div');
radioContainer.style.display = 'flex';
radioContainer.style.gap = '10px';
radioContainer.style.marginTop = '10px';
containerElement.appendChild(radioContainer);
// 创建黑名单词单选框
var blacklistRadio = document.createElement('input');
blacklistRadio.type = 'radio';
blacklistRadio.name = 'filterType';
blacklistRadio.checked = true; // 设置为默认选中
radioContainer.appendChild(blacklistRadio);
// 创建黑名单词标签
var blacklistLabel = document.createElement('label');
blacklistLabel.textContent = '黑名单';
radioContainer.appendChild(blacklistLabel);
// 创建白名单词单选框
var whitelistRadio = document.createElement('input');
whitelistRadio.type = 'radio';
whitelistRadio.name = 'filterType';
radioContainer.appendChild(whitelistRadio);
// 创建白名单词标签
var whitelistLabel = document.createElement('label');
whitelistLabel.textContent = '白名单';
radioContainer.appendChild(whitelistLabel);
// 创建额外黑名单词单选框
var customBlacklistRadio = document.createElement('input');
customBlacklistRadio.type = 'radio';
customBlacklistRadio.name = 'filterType';
radioContainer.appendChild(customBlacklistRadio);
// 创建额外黑名单词标签
var customBlacklistLabel = document.createElement('label');
customBlacklistLabel.textContent = '额外黑名单';
radioContainer.appendChild(customBlacklistLabel);
// 创建按钮容器
var actionButtonsContainer = document.createElement('div');
actionButtonsContainer.style.display = 'flex';
actionButtonsContainer.style.gap = '10px';
actionButtonsContainer.style.marginTop = '10px';
containerElement.appendChild(actionButtonsContainer);
// 创建读取按钮
var readButton = document.createElement('button');
readButton.textContent = '读取';
readButton.style.padding = '6px 12px';
readButton.style.backgroundColor = '#ccc';
readButton.style.border = 'none';
readButton.style.color = '#000';
actionButtonsContainer.appendChild(readButton);
// 创建覆写按钮
var overrideButton = document.createElement('button');
overrideButton.textContent = '覆写';
overrideButton.style.padding = '6px 12px';
overrideButton.style.backgroundColor = '#ccc';
overrideButton.style.border = 'none';
overrideButton.style.color = '#000';
actionButtonsContainer.appendChild(overrideButton);
// 创建重写按钮
var rewriteButton = document.createElement('button');
rewriteButton.textContent = '默认';
rewriteButton.style.padding = '6px 12px';
rewriteButton.style.backgroundColor = '#ccc';
rewriteButton.style.border = 'none';
rewriteButton.style.color = '#000';
actionButtonsContainer.appendChild(rewriteButton);
// 创建说明文字
var descriptionText = document.createElement('p');
descriptionText.style.fontSize = '12px';
descriptionText.innerHTML = '标题命中黑名单词的帖子会被直接屏蔽;命中白名单的帖子永远不会被屏蔽;额外黑名单同黑名单,但不提供随脚本自动更新。<br>白名单规则具有最高优先级,如若一个符合其它规则的帖子没有被屏蔽,请优先检查其是否被白名单规则保护。<br>请注意:黑名单和白名单在您未自定义时,会使用随脚本更新的默认词库。一旦自定义,内置默认词库随即失效,直到您点击恢复默认。';
containerElement.appendChild(descriptionText);
overrideButton.addEventListener('click', function(event) {handleOverrideButtonClick(textBox, blacklistRadio, whitelistRadio, customBlacklistRadio)});
readButton.addEventListener('click', function(event) {handleReadButtonClick(textBox, blacklistRadio, whitelistRadio, customBlacklistRadio)});
rewriteButton.addEventListener('click', function(event) {handleRewriteButtonClick(textBox, blacklistRadio, whitelistRadio, customBlacklistRadio)});
//分割线
var separator2 = document.createElement('hr');
containerElement.appendChild(separator2);
var titleElement2 = document.createElement('h3');
titleElement2.style.fontWeight = 'bold';
titleElement2.style.color = 'black';
titleElement2.style.marginBottom = '10px';
titleElement2.textContent = '屏蔽模式';
containerElement.appendChild(titleElement2);
// 创建单选框容器
var radioContainer2 = document.createElement('div');
radioContainer2.style.display = 'flex';
radioContainer2.style.gap = '10px';
radioContainer2.style.marginTop = '10px';
containerElement.appendChild(radioContainer2);
// 创建兼容模式单选框
var compatibilityRadio = document.createElement('input');
compatibilityRadio.type = 'radio';
compatibilityRadio.name = 'removeMode';
compatibilityRadio.value = 'hide';
radioContainer2.appendChild(compatibilityRadio);
// 创建兼容模式标签
var compatibilityLabel = document.createElement('label');
compatibilityLabel.textContent = '兼容模式';
radioContainer2.appendChild(compatibilityLabel);
// 创建无感屏蔽单选框
var removeRadio = document.createElement('input');
removeRadio.type = 'radio';
removeRadio.name = 'removeMode';
removeRadio.value = 'remove';
radioContainer2.appendChild(removeRadio);
// 创建无感屏蔽标签
var removeLabel = document.createElement('label');
removeLabel.textContent = '自动模式';
radioContainer2.appendChild(removeLabel);
// 创建按钮容器
var actionButtonsContainer2 = document.createElement('div');
actionButtonsContainer2.style.display = 'flex';
actionButtonsContainer2.style.gap = '10px';
actionButtonsContainer2.style.marginTop = '10px';
containerElement.appendChild(actionButtonsContainer2);
// 创建保存按钮
var saveButton4RemoveType = document.createElement('button');
saveButton4RemoveType.textContent = '保存';
saveButton4RemoveType.style.padding = '6px 12px';
saveButton4RemoveType.style.backgroundColor = '#ccc';
saveButton4RemoveType.style.border = 'none';
saveButton4RemoveType.style.color = '#000';
actionButtonsContainer2.appendChild(saveButton4RemoveType);
// 创建说明文本
var descriptionTextRemoveMode = document.createElement('p');
descriptionTextRemoveMode.style.fontSize = '12px';
descriptionTextRemoveMode.innerHTML = '兼容模式适用于不同设备,屏蔽帖子时会保留其占位,也即显示一块空白区域。自动模式则会根据设备类型选择代码,将帖子直接移除。如若识别错误导致无法加载主页,请切换至兼容模式即可。';
containerElement.appendChild(descriptionTextRemoveMode);
// 从 localStorage 获取保存的单选框状态
var savedMode = localStorage.getItem('removeMode');
if (savedMode === 'hide') {
compatibilityRadio.checked = true;
} else if (savedMode === 'remove') {
removeRadio.checked = true;
}
// 保存按钮点击事件处理函数
saveButton4RemoveType.addEventListener('click', function() {
var selectedMode = '';
if (compatibilityRadio.checked) {
selectedMode = 'hide';
} else if (removeRadio.checked) {
selectedMode = 'remove';
}
localStorage.setItem('removeMode', selectedMode);
alert('已保存选择的屏蔽模式:' + selectedMode +",刷新网页生效。");
});
// 分割线
var separator3 = document.createElement('hr');
containerElement.appendChild(separator3);
// 创建标题元素
var titleElement3 = document.createElement('h3');
titleElement3.style.fontWeight = 'bold';
titleElement3.style.color = 'black';
titleElement3.style.marginBottom = '10px';
titleElement3.textContent = '过短标题屏蔽';
containerElement.appendChild(titleElement3);
// 创建数字调整框
var numberInput4ShortTitle = document.createElement('input');
numberInput4ShortTitle.type = 'number';
numberInput4ShortTitle.value = localStorage.getItem('shortTitleThresh') || 6; // 默认值为6
containerElement.appendChild(numberInput4ShortTitle);
// 创建保存按钮
var saveButton4ShortTitle = document.createElement('button');
saveButton4ShortTitle.textContent = '保存';
saveButton4ShortTitle.style.padding = '6px 12px';
saveButton4ShortTitle.style.backgroundColor = '#ccc';
saveButton4ShortTitle.style.border = 'none';
saveButton4ShortTitle.style.marginLeft = '10px';
saveButton4ShortTitle.style.color = '#000';
containerElement.appendChild(saveButton4ShortTitle);
// 创建说明文本
var descriptionText4ShortTitle = document.createElement('p');
descriptionText4ShortTitle.style.fontSize = '12px';
descriptionText4ShortTitle.innerHTML = '【激进选项】当标题过短时,经验判断该帖为重复伸手党提问或牢骚帖的概率较大。你可以设置当标题长度小于等于某个值时默认屏蔽该帖。请注意,此选项不为0时,总会存在误伤,但过小的阈值可能导致漏网之鱼过多,且难以被其它规则屏蔽。';
containerElement.appendChild(descriptionText4ShortTitle);
// 点击保存按钮时的事件处理函数
saveButton4ShortTitle.addEventListener('click', function() {
var value = numberInput4ShortTitle.value;
if (value>20||value<0){alert("阈值过大可能影响网页正常加载,请设置为0-20之间的值,默认为6。");}
else{
localStorage.setItem('shortTitleThresh', value);
alert('已保存选择的阈值:' + value+",刷新网页生效。");
}
});
// 分割线
var separator4 = document.createElement('hr');
containerElement.appendChild(separator4);
// 创建标题元素
var titleElement4 = document.createElement('h3');
titleElement4.style.fontWeight = 'bold';
titleElement4.style.color = 'black';
titleElement4.style.marginBottom = '10px';
titleElement4.textContent = '专项可选屏蔽项';
containerElement.appendChild(titleElement4);
// 创建多选项容器
var checkboxContainer = document.createElement('div');
checkboxContainer.style.display = 'flex';
checkboxContainer.style.flexDirection = 'column';
containerElement.appendChild(checkboxContainer);
// 创建多选项1:启用标题党专项屏蔽
var optionalBlockingRulesCheck01 = createCheckbox("optionalBlockingRulesCheck01", "启用标题党专项屏蔽", checkboxContainer);
optionalBlockingRulesCheck01.checked = getRuleStatus("optionalBlockingRulesCheck01");
// 创建多选项2:启用“发生什么事了”屏蔽
var optionalBlockingRulesCheck02 = createCheckbox("optionalBlockingRulesCheck02", "启用“发生什么事了”屏蔽", checkboxContainer);
optionalBlockingRulesCheck02.checked = getRuleStatus("optionalBlockingRulesCheck02");
// 创建多选项3:启用“额外黑名单”屏蔽
var optionalBlockingRulesCheck03 = createCheckbox("optionalBlockingRulesCheck03", "启用额外黑名单屏蔽", checkboxContainer);
optionalBlockingRulesCheck03.checked = getRuleStatus("optionalBlockingRulesCheck03");
// 创建保存按钮
var saveButton4optinalBlockingRules = document.createElement('button');
saveButton4optinalBlockingRules.textContent = '保存';
saveButton4optinalBlockingRules.style.padding = '6px 12px';
saveButton4optinalBlockingRules.style.backgroundColor = '#ccc';
saveButton4optinalBlockingRules.style.border = 'none';
saveButton4optinalBlockingRules.style.color = '#000';
containerElement.appendChild(saveButton4optinalBlockingRules);
// 从 localStorage 获取保存的多选项勾选状态
restoreCheckboxStatus(checkboxContainer);
// 保存按钮点击事件处理函数
saveButton4optinalBlockingRules.addEventListener('click', function() {
saveCheckboxStatus(checkboxContainer);
alert('已保存选择的多选项配置,刷新网页生效。');
});
// 分割线
var separator5 = document.createElement('hr');
containerElement.appendChild(separator5);
// 创建标题元素
var titleElement5 = document.createElement('h3');
titleElement5.style.fontWeight = 'bold';
titleElement5.style.color = 'black';
titleElement5.style.marginBottom = '10px';
titleElement5.textContent = '低回复帖屏蔽';
containerElement.appendChild(titleElement5);
// 创建数字调整框
var numberInput4FewReply = document.createElement('input');
numberInput4FewReply.type = 'number';
numberInput4FewReply.value = localStorage.getItem('fewReplyThresh') || 0; // 默认值为0
containerElement.appendChild(numberInput4FewReply);
// 创建保存按钮
var saveButton4FewReply = document.createElement('button');
saveButton4FewReply.textContent = '保存';
saveButton4FewReply.style.padding = '6px 12px';
saveButton4FewReply.style.backgroundColor = '#ccc';
saveButton4FewReply.style.border = 'none';
saveButton4FewReply.style.marginLeft = '10px';
saveButton4FewReply.style.color = '#000';
containerElement.appendChild(saveButton4FewReply);
// 创建说明文本
var descriptionText4FewReply = document.createElement('p');
descriptionText4FewReply.style.fontSize = '12px';
descriptionText4FewReply.innerHTML = '【激进选项】默认关闭,如需启动,推荐设置为3。回复过少的帖子为重复伸手党提问的概率较大,然而也可能是刚发出的高质量帖。<br>你可以设置当主题帖回复数量少于某个值时默认屏蔽该帖,别担心,如果该帖后续回复数提升,你仍然能重新看到该帖。';
containerElement.appendChild(descriptionText4FewReply);
// 点击保存按钮时的事件处理函数
saveButton4FewReply.addEventListener('click', function() {
var value = numberInput4FewReply.value;
if (value>30||value<0){alert("阈值过大可能影响网页正常加载,请设置为0-30之间的值,默认为0,推荐设置为3。");}
else{
localStorage.setItem('fewReplyThresh', value);
alert('已保存选择的阈值:' + value+",刷新网页生效。");
}
});
// 将外层容器插入到目标元素之前
targetElement.parentNode.insertBefore(containerElement, targetElement);
}
}
// 创建多选框并添加到容器中的辅助函数
function createCheckbox(checkboxId, labelText, containerElement) {
var checkboxWrapper = document.createElement('div');
checkboxWrapper.style.display = 'flex';
checkboxWrapper.style.alignItems = 'center';
containerElement.appendChild(checkboxWrapper);
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = checkboxId;
checkboxWrapper.appendChild(checkbox);
var label = document.createElement('label');
label.textContent = labelText;
label.setAttribute('for', checkboxId);
checkboxWrapper.appendChild(label);
return checkbox;
}
// 从 localStorage 恢复多选项的勾选状态
function restoreCheckboxStatus(checkboxContainer) {
var savedRules = localStorage.getItem('optionalBlockingRules');
var checkboxes = checkboxContainer.getElementsByTagName('input');
if (savedRules) {
var rules = JSON.parse(savedRules);
for (var i = 0; i < checkboxes.length; i++) {
var checkbox = checkboxes[i];
if (rules.hasOwnProperty(checkbox.id)) {
checkbox.checked = rules[checkbox.id];
} else {
checkbox.checked = true;
}
}
} else {
// 如果没有检测到 optionalBlockingRules,则将所有值视为 true
for (var i_ckbox = 0; i_ckbox < checkboxes.length; i_ckbox++) {
checkbox = checkboxes[i_ckbox];
checkbox.checked = true;
}
}
}
// 保存多选项的勾选状态到 localStorage
function saveCheckboxStatus(checkboxContainer) {
var rules = {};
var checkboxes = checkboxContainer.getElementsByTagName('input');
for (var i = 0; i < checkboxes.length; i++) {
var checkbox = checkboxes[i];
rules[checkbox.id] = checkbox.checked;
}
localStorage.setItem('optionalBlockingRules', JSON.stringify(rules));
}
// 通过规则名称获取是否启用该规则的状态
function getRuleStatus(ruleName) {
var savedRules = localStorage.getItem('optionalBlockingRules');
if (savedRules) {
var rules = JSON.parse(savedRules);
// 如果之前没有写入过该索引,则视为启用状态
if (rules[ruleName] === undefined) {
return true;
} else {
return rules[ruleName];
}
}
return false;
}
function getSelectedFilterType(blacklistRadio, whitelistRadio, customBlacklistRadio) {
var selectedRadio = document.querySelector('input[name="filterType"]:checked');
if (selectedRadio === blacklistRadio) {
return 'customBlacklistWord';
} else if (selectedRadio === whitelistRadio) {
return 'customWhitelistWord';
} else if (selectedRadio === customBlacklistRadio) {
return 'customcustomBlacklistWord';
}
}
// 点击"覆写"按钮时的处理函数
function handleOverrideButtonClick(textBox, blacklistRadio, whitelistRadio, customBlacklistRadio) {
event.preventDefault();
var text = textBox.value.trim(); // 获取文本框内容并去除首尾空格
if (text !== '') {
var localStorageKey = getSelectedFilterType(blacklistRadio, whitelistRadio, customBlacklistRadio); // 获取选中的过滤类型
localStorage.setItem(localStorageKey, text); // 将文本框内容存储到对应的 localStorage
window.alert("已保存,请刷新网页以生效");
} else {
textBox.placeholder = '不能为空'; // 修改文本框的提示词为"不能为空"
}
}
// 点击"读取"按钮时的处理函数
function handleReadButtonClick(textBox, blacklistRadio, whitelistRadio, customBlacklistRadio) {
event.preventDefault();
var localStorageKey = getSelectedFilterType(blacklistRadio, whitelistRadio, customBlacklistRadio); // 获取选中的过滤类型
var text = localStorage.getItem(localStorageKey); // 从对应的 localStorage 中读取内容
if (text !== null) {
textBox.value = text; // 将内容显示在文本框中
} else {
var nullKeywordsDefaultValue;
if(localStorageKey == "customBlacklistWord") nullKeywordsDefaultValue = blacklistKeywords;
else if(localStorageKey == "customWhitelistWord") nullKeywordsDefaultValue = whitelistWords;
else if(localStorageKey == "customcustomBlacklistWord") nullKeywordsDefaultValue = [];
else nullKeywordsDefaultValue = [];
textBox.value = nullKeywordsDefaultValue.join(',');
}
}
// 点击"默认"按钮时的处理函数
function handleRewriteButtonClick(textBox, blacklistRadio, whitelistRadio, customBlacklistRadio) {
event.preventDefault();
var localStorageKey = getSelectedFilterType(blacklistRadio, whitelistRadio, customBlacklistRadio); // 获取选中的过滤类型
localStorage.removeItem(localStorageKey); // 删除对应的 localStorage 中的内容
textBox.value = '';
textBox.placeholder = '已恢复默认设置,请刷新网页以生效';
}
// 执行函数并每秒检测
function loadSetting() {
//console.log("寻找control-group theme标签");
setInterval(addSettingArea, 1000);
}
loadSetting();
function extractUserIdCoreSearch(tagname, regextext) {
var debugMode4extractUserId = false; //关闭调试模式
// 获取所有的链接元素
var links = document.getElementsByTagName(tagname);
console.log('extractUserId: linknum=', links.length);
for (var i = 0; i < links.length; i++) {
var link = links[i];
var href = link.getAttribute('href');
console.log('extractUserId: href=', href);
// 使用正则表达式匹配目标部分并提取内容
var regex = regextext;
var match = regex.exec(href);
if (match) {
var targetText = match[1];
if (debugMode4extractUserId) {
console.log('链接:', href);
console.log('提取到的内容:', targetText);
}
// 返回提取到的内容
var olduserId = localStorage.getItem("shuiyuanUserId");
if(olduserId!=targetText && targetText != ''){
localStorage.setItem("shuiyuanUserId", targetText);
}
return targetText;
}
}
// 如果没有匹配到任何链接内容,返回空字符串
return '';
}
function extractUserIdCore() {
var try1 = extractUserIdCoreSearch('a', /\/u\/(.*)\/preferences/);
if(try1 == null || try1 == ''){
return extractUserIdCoreSearch('button', /\/u\/(.*)/);
}
else{
return try1;
}
}
function getUserId(){
extractUserIdCore();
return localStorage.getItem("shuiyuanUserId");
}
setInterval(extractUserIdCore, 5000);
function getReplyNum(element) {
var parentDiv = element.parentElement.parentElement;
var pullRightDiv = parentDiv.querySelector('.pull-right');
var numberElement = pullRightDiv ? pullRightDiv.querySelector('.number') : null;
if(!numberElement){
//PC端页面结构略有不同
parentDiv = parentDiv.parentElement;
var pullRightDiv4PC = parentDiv.querySelector('.posts');
numberElement = pullRightDiv4PC ? pullRightDiv4PC.querySelector('.number') : null;
}
var replyNumText = numberElement ? numberElement.textContent.trim() : '';
var replyNumReturn;
if (replyNumText.includes('k')) {
replyNumReturn = parseFloat(replyNumText) * 1000;
} else {
replyNumReturn = parseInt(replyNumText);
}
if (isNaN(replyNumReturn)) {
//可能是由于用户设置了关注,导致系统使用“新回复数量”代替了总回复数量,导致replynum在移动端为null。此处直接默认不屏蔽。
replyNumReturn = 9999;
}
return replyNumReturn;
}
})();