版本比较
网站字体更换器
正文
增加 463 行删除 5 行
2 行未改动3456787891011121,804 行未改动全部展开181718181819182018211821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122821,650 行未改动全部展开39333934393539363482393739383939394039414 行未改动394639473948394934953950395139523953395439553956395739586 行未改动2 行未改动```js// ==UserScript==// @name Hydro OJ 增强工具 + 网页字体更换器// @namespace https://bzoj.org/// @version 2.4.6// @description 整合 Hydro OJ 测试点大方块(自动轮询、完成通知、亮暗模式适配、总分数垂直对齐,兼容所有 Hydro 系 OJ)、全站网页字体更换器、题目 Markdown 块内查看/复制功能(右上角按钮,可撤销,兼容所有 Hydro 系 OJ,支持在线编程模式)、以及题目列表「提交」快捷列(仅在题库/比赛/作业/训练列表页显示)// @version 2.5.3// @description 整合 Hydro OJ 测试点大方块(自动轮询、完成通知、亮暗模式适配、总分数垂直对齐,兼容所有 Hydro 系 OJ)、全站网页字体更换器、题目 Markdown 块内查看/复制功能(右上角按钮,可撤销,兼容所有 Hydro 系 OJ,支持在线编程模式)、题目列表「提交」快捷列(仅在题库/比赛/作业/训练列表页显示)、以及比赛/作业成绩表增强(黄色高亮当前用户 + 详细状态方块)// @author bzoj-enhancer & FontSwitcher// @match *://*/*// @grant GM_notification// @grant GM_addStyle1,804 行未改动全部展开 } };
// ================================================================ // 模块四:网页字体更换器(所有站点运行) // 模块四:比赛/作业成绩表增强(scoreboard) // 适用页面: // - /contest/<hex>/scoreboard // - /homework/<hex>/scoreboard // 功能: // 1. 黄色高亮当前登录用户所在行 // 2. 对当前用户行的每道题,异步 fetch 该题的提交记录页面, // 提取最新一次提交的详细信息(状态、分数、耗时、内存), // 用直角状态方块显示在单元格内。 // 兼容所有 Hydro 系 OJ。 // ================================================================
const BZOJ_ScoreboardModule = { /** 状态信息(复用 testcase 模块的颜色定义,方块改为直角) */ STATUS_MAP: { 'accepted': { bg: '#f6ffed', text: 'AC', color: '#389e0d', border: '#b7eb8f' }, 'wrong answer': { bg: '#fff2f0', text: 'WA', color: '#cf1322', border: '#ffccc7' }, 'time limit exceeded': { bg: '#e6f7ff', text: 'TLE', color: '#0958d9', border: '#91d5ff' }, 'memory limit exceeded': { bg: '#e6f7ff', text: 'MLE', color: '#0958d9', border: '#91d5ff' }, 'runtime error': { bg: '#f9f0ff', text: 'RE', color: '#722ed1', border: '#d3adf7' }, 'compile error': { bg: '#fffbe6', text: 'CE', color: '#d48806', border: '#ffe58f' }, 'output limit exceeded': { bg: '#e6f7ff', text: 'OLE', color: '#0958d9', border: '#91d5ff' }, 'system error': { bg: '#f5f5f5', text: 'SE', color: '#434343', border: '#d9d9d9' }, 'judging': { bg: '#e6f7ff', text: '⟳', color: '#0958d9', border: '#91d5ff', spin: true }, 'compiling': { bg: '#e6f7ff', text: '⟳', color: '#0958d9', border: '#91d5ff', spin: true }, 'waiting': { bg: '#e6f7ff', text: '⟳', color: '#0958d9', border: '#91d5ff', spin: true }, 'running': { bg: '#e6f7ff', text: '⟳', color: '#0958d9', border: '#91d5ff', spin: true }, }, DARK_STATUS_MAP: { 'accepted': { bg: '#1e3a1e', text: 'AC', color: '#95de64', border: '#389e0d' }, 'wrong answer': { bg: '#3d1f22', text: 'WA', color: '#ff9c9c', border: '#a8071a' }, 'time limit exceeded': { bg: '#1a2d42', text: 'TLE', color: '#69c0ff', border: '#003a8c' }, 'memory limit exceeded': { bg: '#1a2d42', text: 'MLE', color: '#69c0ff', border: '#003a8c' }, 'runtime error': { bg: '#2d1f3d', text: 'RE', color: '#c9a0f5', border: '#531dab' }, 'compile error': { bg: '#3d321e', text: 'CE', color: '#ffd666', border: '#ad6800' }, 'output limit exceeded': { bg: '#1a2d42', text: 'OLE', color: '#69c0ff', border: '#003a8c' }, 'system error': { bg: '#2e2e2e', text: 'SE', color: '#d9d9d9', border: '#434343' }, },
/** 判断当前页面是否是成绩表页 */ shouldRun: function () { const path = window.location.pathname; return /\/(contest|homework)\/[^\/]+\/scoreboard\/?$/.test(path); },
/** 从 URL 提取 tid(比赛/作业 ID) */ getTid: function () { const m = window.location.pathname.match(/\/(contest|homework)\/([^\/]+)\/scoreboard/); return m ? m[2] : ''; },
/** 检测暗色模式 */ isDarkMode: function () { const html = document.documentElement; if (html.classList.contains('theme--dark')) return true; if (html.classList.contains('theme--light')) return false; if (html.getAttribute('data-mantine-color-scheme') === 'dark') return true; if (html.getAttribute('data-mantine-color-scheme') === 'light') return false; try { const bgColor = getComputedStyle(document.body).backgroundColor; const match = bgColor.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/); if (match) { return (0.299 * +match[1] + 0.587 * +match[2] + 0.114 * +match[3]) / 255 < 0.5; } } catch (e) {} return false; },
/** 获取当前登录用户的 uid */ getCurrentUid: function () { // Hydro 导航栏: nav .nav__list--secondary a[href^="/user/"] const userLink = document.querySelector('nav .nav__list--secondary a[href^="/user/"]'); if (userLink) { const m = userLink.getAttribute('href').match(/\/user\/(\w+)/); if (m) return m[1]; } // 兜底 const altLink = document.querySelector('a[href^="/user/"][class*="user-profile"], a.uname[href^="/user/"]'); if (altLink) { const m = altLink.getAttribute('href').match(/\/user\/(\w+)/); if (m) return m[1]; } return null; },
init: function () { const self = this; if (!self.shouldRun()) return;
GM_addStyle(` /* 当前用户行高亮 */ .bzoj-scoreboard-highlight { background-color: rgba(255, 230, 0, 0.25) !important; } .theme--dark .bzoj-scoreboard-highlight, html[data-mantine-color-scheme="dark"] .bzoj-scoreboard-highlight { background-color: rgba(255, 200, 0, 0.18) !important; } /* 详细状态方块(直角,填满单元格,契合表格) */ .bzoj-score-cell { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 100%; box-sizing: border-box; padding: 4px 2px; border: 1px solid; border-radius: 0; font-family: system-ui, -apple-system, sans-serif; font-weight: 600; line-height: 1.2; cursor: default; transition: transform 0.1s; text-decoration: none; } .bzoj-score-cell:hover { transform: scale(1.05); } /* 让 td 本身也成为 flex 容器,确保方块填满 */ td.col--problem:has(.bzoj-score-cell) { padding: 2px !important; } .bzoj-score-cell__text { font-size: 11px; font-weight: 700; } .bzoj-score-cell__score { font-size: 13px; font-weight: 700; } .bzoj-score-cell__detail { font-size: 9px; font-weight: 400; opacity: 0.8; white-space: nowrap; } @keyframes bzoj-score-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } .bzoj-score-cell__text--spin { animation: bzoj-score-spin 1.5s linear infinite; } /* 加载中占位 */ .bzoj-score-loading { display: inline-block; width: 14px; height: 14px; border: 2px solid rgba(86,117,143,0.2); border-top-color: #56758f; border-radius: 50%; animation: bzoj-score-spin 0.8s linear infinite; } `);
self.enhanceScoreboard(); self._installObserver(); },
/** 标准化状态文本 */ normalizeStatus: function (raw) { if (!raw) return ''; const text = raw.trim().toLowerCase(); const aliases = { 'time exceeded': 'time limit exceeded', 'memory exceeded': 'memory limit exceeded', 'ac': 'accepted', 'wa': 'wrong answer', 'tle': 'time limit exceeded', 'mle': 'memory limit exceeded', 're': 'runtime error', 'ce': 'compile error', 'ole': 'output limit exceeded', 'se': 'system error', }; if (aliases[text]) return aliases[text]; for (const key of Object.keys(this.STATUS_MAP)) { if (text.includes(key)) return key; } return text; },
/** 获取状态信息 */ getStatusInfo: function (key) { const isDark = this.isDarkMode(); const map = isDark ? this.DARK_STATUS_MAP : this.STATUS_MAP; return map[key] || { bg: isDark ? '#2e2e2e' : '#f5f5f5', text: key.substring(0, 3).toUpperCase(), color: isDark ? '#d9d9d9' : '#434343', border: isDark ? '#434343' : '#d9d9d9', }; },
/** 从表头提取每道题的 pid 列表 */ extractProblemIds: function () { // Hydro 成绩表表头在独立的 sticky 表 (table.section__table-header) 或同表 thead const headerLinks = document.querySelectorAll( 'table.section__table-header thead th.col--problem a[href*="/p/"], ' + 'table thead th.col--problem a[href*="/p/"]' ); const pids = []; for (const a of headerLinks) { const m = a.getAttribute('href').match(/\/p\/([^\/?]+)/); if (m) pids.push(m[1]); } return pids; },
/** 增强成绩表:高亮当前用户行 + 异步加载提交详情 */ enhanceScoreboard: function () { const self = this; const uid = self.getCurrentUid(); const tid = self.getTid();
// 查找成绩表 tbody(Hydro 用 table.data-table,tbody 里有行) const tables = document.querySelectorAll('table.data-table'); for (const table of tables) { const tbody = table.querySelector('tbody'); if (!tbody) continue; const rows = tbody.querySelectorAll('tr'); if (rows.length === 0) continue; // 检查是否是成绩表(有 col--rank / col--user 列) if (!table.querySelector('col.col--rank, col.col--user, td.col--rank, td.col--user')) continue; if (table.dataset.bzojScoreboard === '1') continue; table.dataset.bzojScoreboard = '1';
// 提取每题 pid(从表头) const pids = self.extractProblemIds();
for (const row of rows) { if (row.dataset.bzojScoreRow === '1') continue; row.dataset.bzojScoreRow = '1';
// 高亮当前用户行 if (uid) { const userBtn = row.querySelector('[data-uid]'); const userLink = row.querySelector('a[href^="/user/"]'); let rowUid = null; if (userBtn) rowUid = userBtn.getAttribute('data-uid'); else if (userLink) { const m = userLink.getAttribute('href').match(/\/user\/(\w+)/); if (m) rowUid = m[1]; } if (rowUid && rowUid === uid) { row.classList.add('bzoj-scoreboard-highlight'); // 对当前用户行,异步加载每题提交详情 self._loadUserSubmissionDetails(row, uid, tid, pids); } } } } },
/** 异步加载当前用户每道题的提交详情 */ _loadUserSubmissionDetails: async function (row, uid, tid, pids) { const self = this; // 获取当前行的题目单元格(col--problem) const problemCells = row.querySelectorAll('td.col--problem'); if (problemCells.length === 0) return;
// 为每个题目单元格先放加载占位 problemCells.forEach((cell, idx) => { if (cell.querySelector('.bzoj-score-cell, .bzoj-score-loading')) return; // 保留原始分数文本 const originalScore = cell.textContent.trim(); cell.dataset.bzojOriginalScore = originalScore; const loader = document.createElement('span'); loader.className = 'bzoj-score-loading'; cell.innerHTML = ''; cell.appendChild(loader); });
// 逐题 fetch 提交记录 for (let i = 0; i < problemCells.length && i < pids.length; i++) { const cell = problemCells[i]; const pid = pids[i]; try { await self._fetchAndShowDetail(cell, pid, uid, tid); } catch (e) { self._restoreCell(cell); } } },
/** fetch 单题提交记录,提取最新提交的 record 链接,再 fetch record 详情 */ _fetchAndShowDetail: async function (cell, pid, uid, tid) { const self = this; // Hydro 提交记录列表: /record?pid=<pid>&uidOrName=<uid>&tid=<tid> const listUrl = `/record?pid=${encodeURIComponent(pid)}&uidOrName=${encodeURIComponent(uid)}${tid ? '&tid=' + tid : ''}`; const resp = await fetch(listUrl, { credentials: 'same-origin' }); const html = await resp.text(); const doc = new DOMParser().parseFromString(html, 'text/html');
// 找到第一条提交记录的链接 /record/<rid> const recordLink = doc.querySelector('a[href^="/record/"]'); if (!recordLink) { self._restoreCell(cell); return; } const ridMatch = recordLink.getAttribute('href').match(/\/record\/(\w+)/); if (!ridMatch) { self._restoreCell(cell); return; } const rid = ridMatch[1];
// fetch record 详情页 const recordUrl = `/record/${rid}`; const resp2 = await fetch(recordUrl, { credentials: 'same-origin' }); const html2 = await resp2.text(); const doc2 = new DOMParser().parseFromString(html2, 'text/html');
// 从 record 页面提取状态、分数、耗时、内存 const detail = self._extractRecordDetail(doc2); self._renderDetailCell(cell, detail, recordUrl); },
/** 从 record 详情页提取状态/分数/耗时/内存 * 页面结构: * 顶栏 #status .section__title 内含 .record-status--text(状态文字,如 Accepted) * 和 .float-right 或无 class 的 span(分数,如 60) * 侧边栏 #summary 的 dl.horizontal 内: * <dt>分数</dt><dd>60</dd> * <dt>总耗时</dt><dd>39ms</dd> * <dt>峰值时间</dt><dd>1ms</dd> * <dt>峰值内存</dt><dd>768 KiB</dd> */ _extractRecordDetail: function (doc) { const self = this;
// 状态:顶栏的 .record-status--text(排除 .float-right 分数部分) let statusKey = ''; const statusEls = doc.querySelectorAll('#status .record-status--text, .record-status--text'); for (const el of statusEls) { // 克隆后移除 .float-right 子元素,只取状态文字 const clone = el.cloneNode(true); clone.querySelectorAll('.float-right').forEach(e => e.remove()); const text = clone.textContent.trim(); if (text) { statusKey = self.normalizeStatus(text); if (statusKey) break; } }
// 从 #summary 的 dl.horizontal 提取分数/时间/内存 let score = '', time = '', memory = ''; const summaryDl = doc.querySelector('#summary, dl#summary'); if (summaryDl) { const dts = summaryDl.querySelectorAll('dt'); const dds = summaryDl.querySelectorAll('dd'); for (let i = 0; i < dts.length && i < dds.length; i++) { const label = dts[i].textContent.trim().toLowerCase(); const value = dds[i].textContent.trim(); if (label === '分数' || label === 'score') { score = value; } else if (label === '峰值时间' || label === '总耗时' || label.includes('时间') || label.includes('time')) { if (!time) time = value; // 优先取峰值时间/总耗时 } else if (label === '峰值内存' || label.includes('内存') || label.includes('memory')) { memory = value; } } }
// 兜底:分数从顶栏 .float-right 或 section__title 提取 if (!score) { const floatRight = doc.querySelector('#status .float-right, .record-status--text .float-right'); if (floatRight) { score = floatRight.textContent.trim(); } } if (!score) { const titleEl = doc.querySelector('#status .section__title'); if (titleEl) { const m = titleEl.textContent.match(/(\d+)/); if (m) score = m[1]; } }
return { statusKey, score, time, memory }; },
/** 渲染详细状态方块到单元格 */ _renderDetailCell: function (cell, detail, recordUrl) { const self = this; const originalScore = cell.dataset.bzojOriginalScore || ''; cell.innerHTML = '';
if (!detail.statusKey && !detail.score && !originalScore) { self._restoreCell(cell); return; }
const statusKey = detail.statusKey || ''; const info = statusKey ? self.getStatusInfo(statusKey) : { bg: this.isDarkMode() ? '#2e2e2e' : '#f5f5f5', text: '', color: this.isDarkMode() ? '#d9d9d9' : '#434343', border: this.isDarkMode() ? '#434343' : '#d9d9d9', };
const block = document.createElement('a'); block.href = recordUrl; block.target = '_blank'; block.className = 'bzoj-score-cell'; block.style.background = info.bg; block.style.color = info.color; block.style.borderColor = info.border;
// 状态缩写 if (info.text) { const textEl = document.createElement('span'); textEl.className = 'bzoj-score-cell__text'; if (info.spin) textEl.classList.add('bzoj-score-cell__text--spin'); textEl.textContent = info.text; block.appendChild(textEl); } // 分数 const scoreVal = detail.score || originalScore; if (scoreVal) { const scoreEl = document.createElement('span'); scoreEl.className = 'bzoj-score-cell__score'; scoreEl.textContent = scoreVal; block.appendChild(scoreEl); } // 耗时 / 内存 if (detail.time || detail.memory) { const detailEl = document.createElement('span'); detailEl.className = 'bzoj-score-cell__detail'; detailEl.textContent = [detail.time, detail.memory].filter(Boolean).join(' / '); block.appendChild(detailEl); } cell.appendChild(block); },
/** 恢复原始分数显示 */ _restoreCell: function (cell) { const original = cell.dataset.bzojOriginalScore; if (original !== undefined) { cell.innerHTML = ''; cell.textContent = original; } },
_observer: null, _debounce: null, _installObserver: function () { const self = this; if (self._observer) return; self._observer = new MutationObserver(function () { clearTimeout(self._debounce); self._debounce = setTimeout(function () { self.enhanceScoreboard(); }, 500); }); self._observer.observe(document.body, { childList: true, subtree: true }); } };
// ================================================================ // 模块五:网页字体更换器(所有站点运行) // ================================================================
// ==================== 默认常量 ====================
const FS_DEFAULT_BTN_SIZE = 42; const FS_COLLAPSED_VISIBLE = 6;1,650 行未改动全部展开 fsResetIdleTimer(); }
// ================================================================ // 主入口:初始化四个模块 // 主入口:初始化五个模块 // ================================================================
function main() { // 字体更换器:所有站点运行4 行未改动
// BZOJ 题目 Markdown 查看/复制:仅 /p/<id> 题目详情页运行(init 内部有路径与结构双重判断) BZOJ_ProblemMarkdownModule.init();
// BZOJ 题目列表「上次提交」快捷链接:所有 Hydro 系 OJ 运行 // BZOJ 题目列表「提交」快捷列:仅题库/比赛/作业/训练列表页运行 BZOJ_SubmitLinkModule.init();
// BZOJ 成绩表增强:仅 /contest/<hex>/scoreboard 和 /homework/<hex>/scoreboard 运行 BZOJ_ScoreboardModule.init(); }
if (document.readyState === 'complete' || document.readyState === 'interactive') { main();6 行未改动