Luogu Discussion Filter
用于过滤洛谷讨论区特定评论的篡改猴脚本。
支持完全匹配和关键词匹配两种模式。
可以用于过滤 qp,zc 等无意义内容。
安装
1.安装 Tampermonkey
从浏览器扩展商店安装 Tampermonkey。
2.安装脚本
- 新建脚本。
- 粘贴脚本代码。
- 保存。
使用方法
设置屏蔽规则
如图,点击搜索框左侧的过滤器按钮进入设置界面。
完全匹配:
与此框内任意一行文本完全相同的评论会被过滤。
关键词匹配:
包含此框内任意一行文本的评论会被过滤。
AI 使用说明
代码使用 ChatGPT 编写。
脚本代码
// ==UserScript==
// @name Luogu Discussion Filter
// @namespace https://www.luogu.com.cn/user/1022924
// @version 1.2.0
// @description 在讨论区自动屏蔽指定评论
// @author qinglin_zcr
// @match https://www.luogu.com.cn/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addStyle
// @license GPL-3.0
// @run-at document-idle
// ==/UserScript==
(function () {
'use strict';
/* =========================
配置
========================= */
const STORAGE_KEY =
'luogu_discussion_filter_rules';
function getRules() {
return GM_getValue(STORAGE_KEY, []);
}
function setRules(rules) {
GM_setValue(STORAGE_KEY, rules);
}
/* =========================
样式
========================= */
GM_addStyle(`
.lg-filter-btn {
margin-right: 10px;
cursor: pointer;
width: 34px;
height: 34px;
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.2s;
user-select: none;
}
.lg-filter-btn:hover {
background: rgba(0,0,0,0.08);
}
.lg-filter-mask {
position: fixed;
inset: 0;
background: rgba(0,0,0,0.45);
z-index: 999999;
display: flex;
align-items: center;
justify-content: center;
}
.lg-filter-panel {
width: 560px;
max-width: 90vw;
background: white;
border-radius: 14px;
padding: 20px;
box-shadow: 0 0 30px rgba(0,0,0,0.25);
}
.lg-filter-title {
font-size: 22px;
font-weight: bold;
margin-bottom: 16px;
}
.lg-filter-textarea {
width: 100%;
resize: vertical;
font-size: 14px;
padding: 10px;
box-sizing: border-box;
border-radius: 8px;
border: 1px solid #ccc;
outline: none;
font-family: Consolas, monospace;
}
.lg-filter-actions {
margin-top: 16px;
display: flex;
justify-content: flex-end;
gap: 10px;
}
.lg-filter-actions button {
border: none;
padding: 8px 16px;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
}
.lg-filter-save {
background: #3498db;
color: white;
}
.lg-filter-cancel {
background: #eee;
}
`);
/* =========================
工具函数
========================= */
function normalizeText(s) {
return s
.replace(/\s+/g, ' ')
.trim();
}
function shouldBlock(text, rules) {
const normalizedText =
normalizeText(text);
for (const rule of rules) {
const value =
normalizeText(rule.value);
if (!value) {
continue;
}
// 包含关键词
if (rule.type === 'contains') {
if (
normalizedText.includes(value)
) {
return true;
}
}
// 完全匹配
else if (rule.type === 'exact') {
if (
normalizedText === value
) {
return true;
}
}
}
return false;
}
/* =========================
恢复评论
========================= */
function restoreComments() {
const hiddenComments =
document.querySelectorAll(
'.comment[data-lg-filtered="1"]'
);
hiddenComments.forEach(comment => {
comment.style.display = '';
delete comment.dataset.lgFiltered;
});
}
/* =========================
设置 UI
========================= */
function createSettingsUI() {
const old =
document.querySelector(
'.lg-filter-mask'
);
if (old) {
old.remove();
}
const mask =
document.createElement('div');
mask.className = 'lg-filter-mask';
const rules = getRules();
const exactText = rules
.filter(
rule => rule.type === 'exact'
)
.map(rule => rule.value)
.join('\n');
const containsText = rules
.filter(
rule => rule.type === 'contains'
)
.map(rule => rule.value)
.join('\n');
mask.innerHTML = `
<div class="lg-filter-panel">
<div class="lg-filter-title">
Luogu Discussion Filter
</div>
<div style="
margin-bottom: 8px;
font-weight: bold;
">
完全匹配
</div>
<textarea
class="
lg-filter-textarea
lg-filter-exact
"
style="height:120px;"
placeholder="每行一个完整评论"
>${exactText}</textarea>
<div style="
margin-top:18px;
margin-bottom:8px;
font-weight:bold;
">
关键词匹配
</div>
<textarea
class="
lg-filter-textarea
lg-filter-contains
"
style="height:120px;"
placeholder="每行一个关键词"
>${containsText}</textarea>
<div class="lg-filter-actions">
<button class="lg-filter-cancel">
取消
</button>
<button class="lg-filter-save">
保存
</button>
</div>
</div>
`;
document.body.appendChild(mask);
let maskMouseDown = false;
mask.addEventListener('mousedown', (e) => {
maskMouseDown = (e.target === mask);
});
mask.addEventListener('mouseup', (e) => {
if (
maskMouseDown &&
e.target === mask
) {
mask.remove();
}
maskMouseDown = false;
});
mask.querySelector(
'.lg-filter-cancel'
).onclick = () => {
mask.remove();
};
mask.querySelector(
'.lg-filter-save'
).onclick = () => {
const exactValue =
mask.querySelector(
'.lg-filter-exact'
).value;
const containsValue =
mask.querySelector(
'.lg-filter-contains'
).value;
const rules = [];
// 完全匹配
exactValue
.split('\n')
.forEach(line => {
line = line.trim();
if (!line) {
return;
}
rules.push({
type: 'exact',
value: line
});
});
// 关键词匹配
containsValue
.split('\n')
.forEach(line => {
line = line.trim();
if (!line) {
return;
}
rules.push({
type: 'contains',
value: line
});
});
setRules(rules);
mask.remove();
processAll();
alert('保存成功');
};
}
/* =========================
添加设置按钮
========================= */
function addSettingButton() {
if (
document.querySelector(
'.lg-filter-btn'
)
) {
return;
}
const penSwirlSvg =
document.querySelector(
'svg[data-icon="pen-swirl"]'
);
if (!penSwirlSvg) {
return;
}
const searchWrap =
document.querySelector(
'.nav-search'
) ||
document.querySelector(
'.search-wrap'
);
if (!searchWrap) {
return;
}
const btn =
document.createElement('div');
btn.className = 'lg-filter-btn';
btn.title =
'Discussion Filter';
btn.innerHTML = `
<img
src="
https://cdn.luogu.com.cn/upload/image_hosting/soky5j65.png
"
style="
width:17px;
height:17px;
object-fit:contain;
pointer-events:none;
"
>
`;
btn.onclick = createSettingsUI;
searchWrap.parentNode.insertBefore(
btn,
searchWrap
);
}
/* =========================
屏蔽逻辑
========================= */
function processAll() {
if (
!location.href.startsWith(
'https://www.luogu.com.cn/discuss/'
)
) {
return;
}
// 先恢复
restoreComments();
const rules = getRules();
if (!rules.length) {
return;
}
const markedList =
document.querySelectorAll(
'.lfe-marked'
);
markedList.forEach(marked => {
const text =
marked.innerText || '';
if (
!shouldBlock(text, rules)
) {
return;
}
const comment =
marked.closest('.comment');
if (!comment) {
return;
}
comment.style.display = 'none';
comment.dataset.lgFiltered = '1';
});
}
/* =========================
动态监听
========================= */
let processTimer = null;
const observer =
new MutationObserver(() => {
addSettingButton();
clearTimeout(processTimer);
processTimer =
setTimeout(() => {
processAll();
}, 200);
});
observer.observe(document.body, {
childList: true,
subtree: true
});
/* =========================
URL 变化监听
========================= */
let lastUrl = location.href;
setInterval(() => {
if (
location.href !== lastUrl
) {
lastUrl = location.href;
setTimeout(() => {
restoreComments();
processAll();
}, 300);
}
}, 500);
/* =========================
启动
========================= */
addSettingButton();
processAll();
})();
