版本比较
洛谷一键跳转vjudge插件
正文
增加 175 行删除 84 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109支持洛谷一键跳转vjudge,只需在题目页面点击右键,选择“跳转vjudge”即可。```javascript// ==UserScript==// @name 洛谷一键跳转VJudge// @namespace http://tampermonkey.net/// @version 1.0.0// @description 支持洛谷所有题目跳转VJudge,修复CF带数字后缀的编号匹配// @author Tangyixao// @match https://www.luogu.com.cn/problem/CF*// @match https://www.luogu.com.cn/problem/SP*// @match https://www.luogu.com.cn/problem/AT*// @match https://www.luogu.com.cn/problem/UVA*// @match https://www.luogu.com.cn/problem/P*// @match https://www.luogu.com.cn/problem/B*// @grant none// ==/UserScript==(function () {"use strict";function getVjudgeUrl() {const luoguUrl = window.location.href;const luoguPath = window.location.pathname;if (luoguPath.startsWith("/problem/P")) {const pMatch = luoguPath.match(/P(\d+)/);return pMatch ? `https://vjudge.net/problem/洛谷-P${pMatch[1]}` : null;} else if (luoguPath.startsWith("/problem/B")) {const pMatch = luoguPath.match(/B(\d+)/);return pMatch ? `https://vjudge.net/problem/洛谷-B${pMatch[1]}` : null;} else if (luoguPath.startsWith("/problem/AT_")) {const atMatch = luoguPath.match(/AT_(\w+)/);return atMatch? `https://vjudge.net/problem/AtCoder-${atMatch[1]}`: null;} else if (luoguPath.startsWith("/problem/UVA")) {const uvaMatch = luoguPath.match(/UVA(\d+)/);return uvaMatch ? `https://vjudge.net/problem/UVA-${uvaMatch[1]}` : null;} else if (luoguPath.startsWith("/problem/CF")) {// 修复CF编号匹配const cfMatch = luoguPath.match(/CF(\d+[A-Za-z\d]*)/);return cfMatch? `https://vjudge.net/problem/CodeForces-${cfMatch[1]}`: null;} else if (luoguPath.startsWith("/problem/SP")) {let spojId = null;const originalLink = document.querySelector('a[href*="spoj.com/problems/"]',);if (originalLink) {spojId = originalLink.href.split("/problems/")[1].replace("/", "");} else {const pageText = document.body.innerText;const spojTextMatch = pageText.match(/SPOJ\s*[-\s:]([A-Za-z0-9_-]+)/i);spojId = spojTextMatch ? spojTextMatch[1].trim() : null;}return spojId ? `https://vjudge.net/problem/SPOJ-${spojId}` : null;}return null;}const copyBtn = Array.from(document.querySelectorAll("button")).find((btn) => btn.innerText.trim() === "复制题目",);const vjudgeUrl = getVjudgeUrl();if (copyBtn && vjudgeUrl) {const jumpBtn = document.createElement("button");jumpBtn.innerText = "跳转VJudge";const copyStyles = window.getComputedStyle(copyBtn);["backgroundColor","color","border","borderRadius","padding","fontSize","fontFamily","cursor","height","lineHeight","textAlign","whiteSpace","verticalAlign","display","marginTop","marginBottom",].forEach((styleProp) => {jumpBtn.style[styleProp] = copyStyles[styleProp];});jumpBtn.style.marginLeft = "5px";jumpBtn.style.minWidth = copyStyles.minWidth || "80px";jumpBtn.addEventListener("click", () => {window.open(vjudgeUrl, "_blank");});copyBtn.parentNode.insertBefore(jumpBtn, copyBtn.nextSibling);} else if (!vjudgeUrl) {const platform = luoguPath.match(/\/problem\/(\w+)/)[1].slice(0, 2);console.warn(`洛谷${platform}题目跳转VJudge:未找到有效题目标识符,请检查页面格式`,);}})();```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200支持洛谷一键跳转vjudge,只需在题目页面点击右键,选择“跳转vjudge”即可。```javascript// ==UserScript==// @name 洛谷一键跳转VJudge(精准版)// @namespace http://tampermonkey.net/// @version 1.2.0// @description 洛谷题目页添加“跳转VJudge”按钮,优先从原题链接提取标准ID,解决跳转错误问题// @author Tangyixiao (改进版)// @match https://www.luogu.com.cn/problem/CF*// @match https://www.luogu.com.cn/problem/SP*// @match https://www.luogu.com.cn/problem/AT*// @match https://www.luogu.com.cn/problem/UVA*// @match https://www.luogu.com.cn/problem/P*// @match https://www.luogu.com.cn/problem/B*// @grant none// ==/UserScript==(function() {'use strict';const MAX_RETRIES = 10;const RETRY_INTERVAL = 500;const LOG_PREFIX = '[洛谷跳转VJudge]';/*** 从页面中提取原题官方链接中的标准ID* @returns {Object|null} 返回 { platform, id },platform为VJudge平台名,id为提取的ID*/function extractStandardIdFromPage() {// 查找所有指向原题官网的链接const links = document.querySelectorAll('a[href]');for (let link of links) {const href = link.href;// Codeforcesif (href.includes('codeforces.com/problemset/problem/')) {const match = href.match(/problemset\/problem\/(\d+)\/([A-Za-z0-9]+)/);if (match) return { platform: 'CodeForces', id: match[1] + match[2] };// 另一种格式:/contest/xxx/problem/字母const contestMatch = href.match(/contest\/(\d+)\/problem\/([A-Za-z0-9]+)/);if (contestMatch) return { platform: 'CodeForces', id: contestMatch[1] + contestMatch[2] };}// AtCoderif (href.includes('atcoder.jp/contests/') && href.includes('/tasks/')) {const match = href.match(/tasks\/([A-Za-z0-9_]+)/);if (match) return { platform: 'AtCoder', id: match[1] };}// UVA (uva.onlinejudge.org 或 onlinejudge.org)if (href.includes('onlinejudge.org')) {const match = href.match(/index\.php\?option=com_onlinejudge&Itemid=8&page=show_problem&problem=(\d+)/);if (match) return { platform: 'UVA', id: match[1] };// 旧格式const oldMatch = href.match(/\/problem\/(\d+)/);if (oldMatch) return { platform: 'UVA', id: oldMatch[1] };}// SPOJif (href.includes('spoj.com/problems/')) {const match = href.match(/problems\/([A-Za-z0-9_\-]+)/);if (match) return { platform: 'SPOJ', id: match[1] };}}return null;}/*** 从洛谷URL提取ID(备选方案)* @returns {Object|null} 返回 { platform, id }*/function extractIdFromLuoguUrl() {const path = window.location.pathname;// CFif (path.startsWith('/problem/CF')) {const match = path.match(/CF(\d+[A-Za-z\d]*)/);if (match) return { platform: 'CodeForces', id: match[1] };}// ATif (path.startsWith('/problem/AT_')) {const match = path.match(/AT_(\w+)/);if (match) return { platform: 'AtCoder', id: match[1] };}// UVAif (path.startsWith('/problem/UVA')) {const match = path.match(/UVA(\d+)/);if (match) return { platform: 'UVA', id: match[1] };}// SPif (path.startsWith('/problem/SP')) {const match = path.match(/SP(\w+)/);if (match) return { platform: 'SPOJ', id: match[1] };}// 洛谷 Pif (path.startsWith('/problem/P')) {const match = path.match(/P(\d+)/);if (match) return { platform: '洛谷', id: 'P' + match[1] };}// 洛谷 Bif (path.startsWith('/problem/B')) {const match = path.match(/B(\d+)/);if (match) return { platform: '洛谷', id: 'B' + match[1] };}return null;}/*** 获取最终的VJudge跳转链接*/function getVjudgeUrl() {// 1. 优先从页面原题链接提取const pageInfo = extractStandardIdFromPage();if (pageInfo) {console.log(LOG_PREFIX, '从页面提取到标准ID:', pageInfo);return `https://vjudge.net/problem/${pageInfo.platform}-${pageInfo.id}`;}// 2. 降级使用URL提取const urlInfo = extractIdFromLuoguUrl();if (urlInfo) {console.warn(LOG_PREFIX, '使用URL备选ID,请验证准确性:', urlInfo);return `https://vjudge.net/problem/${urlInfo.platform}-${urlInfo.id}`;}console.error(LOG_PREFIX, '无法提取题目标识符');return null;}/*** 查找“复制题目”按钮(容错)*/function findCopyButton() {const buttons = document.querySelectorAll('button');for (let btn of buttons) {const text = btn.innerText.trim();if (text.includes('复制') && !text.includes('代码')) {return btn;}}return null;}/*** 插入跳转按钮*/function insertJumpButton(copyBtn) {const vjudgeUrl = getVjudgeUrl();if (!vjudgeUrl) {console.warn(LOG_PREFIX, '未生成有效跳转链接');return;}// 创建按钮const jumpBtn = document.createElement('button');jumpBtn.innerText = '跳转VJudge';jumpBtn.title = `点击跳转至:${vjudgeUrl}`; // 悬停显示目标URL// 复制样式const copyStyles = window.getComputedStyle(copyBtn);const styleProps = ['backgroundColor', 'color', 'border', 'borderRadius', 'padding','fontSize', 'fontFamily', 'cursor', 'height', 'lineHeight','textAlign', 'whiteSpace', 'verticalAlign', 'display','marginTop', 'marginBottom'];styleProps.forEach(prop => {jumpBtn.style[prop] = copyStyles[prop];});jumpBtn.style.marginLeft = '5px';jumpBtn.style.minWidth = copyStyles.minWidth || '80px';jumpBtn.addEventListener('click', () => {console.log(LOG_PREFIX, '打开链接:', vjudgeUrl);window.open(vjudgeUrl, '_blank');});copyBtn.parentNode.insertBefore(jumpBtn, copyBtn.nextSibling);console.log(LOG_PREFIX, '跳转按钮已添加,目标:', vjudgeUrl);}/*** 等待复制按钮出现*/function waitForCopyButton(retries = 0) {if (retries > MAX_RETRIES) {console.warn(LOG_PREFIX, '超过最大重试次数,未找到复制按钮');return;}const copyBtn = findCopyButton();if (copyBtn) {insertJumpButton(copyBtn);} else {setTimeout(() => waitForCopyButton(retries + 1), RETRY_INTERVAL);}}// 启动waitForCopyButton();})();```