支持洛谷一键跳转vjudge,只需在题目页面点击右键,选择“跳转vjudge”即可。
// ==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;
// Codeforces
if (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] };
}
// AtCoder
if (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] };
}
// SPOJ
if (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;
// CF
if (path.startsWith('/problem/CF')) {
const match = path.match(/CF(\d+[A-Za-z\d]*)/);
if (match) return { platform: 'CodeForces', id: match[1] };
}
// AT
if (path.startsWith('/problem/AT_')) {
const match = path.match(/AT_(\w+)/);
if (match) return { platform: 'AtCoder', id: match[1] };
}
// UVA
if (path.startsWith('/problem/UVA')) {
const match = path.match(/UVA(\d+)/);
if (match) return { platform: 'UVA', id: match[1] };
}
// SP
if (path.startsWith('/problem/SP')) {
const match = path.match(/SP(\w+)/);
if (match) return { platform: 'SPOJ', id: match[1] };
}
// 洛谷 P
if (path.startsWith('/problem/P')) {
const match = path.match(/P(\d+)/);
if (match) return { platform: '洛谷', id: 'P' + match[1] };
}
// 洛谷 B
if (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();
})();