代码
// ==UserScript==
// @name 洛谷题目列表-题解提交标记
// @namespace http://tampermonkey.net/
// @version 2.2
// @description 在洛谷题目列表页,为可以提交题解的题目添加标记。支持 P, B, AT, CF, SP, UVA 等多种题目编号前缀。
// @author Your Name
// @match https://www.luogu.com.cn/problem/list*
// @icon https://www.luogu.com.cn/favicon.ico
// @grant GM_addStyle
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// --- 配置区 ---
const TAG_TEXT = '可写题解';
const TAG_COLOR = '#4CAF50'; // 绿色
const DEBOUNCE_DELAY = 300; // URL变化后,延迟300毫秒再执行检查
// --- 配置结束 ---
GM_addStyle(`
.luogu-solution-tag {
display: inline-flex;
align-items: center;
justify-content: center;
background-color: ${TAG_COLOR};
color: white;
font-size: 12px;
font-weight: bold;
padding: 2px 6px;
border-radius: 4px;
margin-left: 8px;
height: 20px;
box-sizing: border-box;
}
`);
/**
* 防抖函数
*/
function debounce(func, delay) {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
/**
* 检查单个题目
*/
async function checkProblem(rowElement) {
if (rowElement.dataset.solutionChecked) return;
rowElement.dataset.solutionChecked = 'true';
// --- 修改开始 ---
// 定义所有支持的题目编号前缀
const problemPrefixes = ['P', 'B', 'AT', 'CF', 'SP', 'UVA'];
// 动态生成 CSS 选择器字符串,例如 'div[title^="P"], div[title^="B"], ...'
const selector = problemPrefixes.map(prefix => `div[title^="${prefix}"]`).join(', ');
// 使用新的选择器来查找题目编号元素
const pidElement = rowElement.querySelector(selector);
// --- 修改结束 ---
if (!pidElement) return;
const pid = pidElement.textContent.trim();
try {
const response = await fetch(`https://www.luogu.com.cn/problem/solution/${pid}`, { credentials: 'include' });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const html = await response.text();
if (html.includes('"acceptSolution":true')) {
const tagElement = document.createElement('div');
tagElement.className = 'luogu-solution-tag';
tagElement.textContent = TAG_TEXT;
const difficultyElement = rowElement.querySelector('div.difficulty');
(difficultyElement || pidElement.parentNode).appendChild(tagElement);
}
} catch (error) {
console.error(`检查题目 ${pid} 失败:`, error);
}
}
/**
* URL变化或页面加载时的处理函数
*/
function onUrlChange() {
console.log('检测到URL变化,正在重置并重新检查所有题目...');
// 清除所有旧的标记和状态
document.querySelectorAll('.luogu-solution-tag').forEach(tag => tag.remove());
document.querySelectorAll('div.row').forEach(row => delete row.dataset.solutionChecked);
// 对当前页面所有可见的题目进行检查
document.querySelectorAll('div.row').forEach(row => checkProblem(row));
}
// --- 核心逻辑 ---
const debouncedOnUrlChange = debounce(onUrlChange, DEBOUNCE_DELAY);
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
history.pushState = function(...args) {
originalPushState.apply(this, args);
debouncedOnUrlChange(); // 手动触发处理函数
};
history.replaceState = function(...args) {
originalReplaceState.apply(this, args);
debouncedOnUrlChange(); // 手动触发处理函数
};
window.addEventListener('popstate', debouncedOnUrlChange);
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE) {
const newRows = node.matches('div.row') ? [node] : node.querySelectorAll('div.row');
newRows.forEach(row => checkProblem(row));
}
});
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
// 5. 页面首次加载时,立即执行一次检查
onUrlChange();
console.log('洛谷题解标记脚本已启动。');
})();注意
在题目列表切换页面时,插件可能不会自动更新数据,遇到这种情况,请手动刷新。
