跳到正文

休闲·娱乐

新洛谷Problem Jumper

Queryme约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年8月22日
收录
2026年8月22日
更新
2026年11月13日
篇幅
约 13 分钟
获赞
1

版本

  1. v1当前

Luogu Problem Jumper(New)

其实之前是有老的 problem jumper,但是貌似很久没更新,洛谷很早就五位数题号了,他还只能匹配四位数。还有别的一些兼容性问题,昨天发现自己油猴又损坏了,干脆自己重新整一个。

支持题库

洛谷,CF,AT 的题号匹配以及原站链接。当你直接单击时,会跳转洛谷题目页。按住Ctrl+左键单击可以跳转回原站。更新了SPOJ的匹配规则以及AT的特殊比赛url字段。

安装方法

  1. 浏览器安装 Tampermonkey / Violentmonkey 油猴扩展。你也可以使用脚本猫。
  2. 新建用户脚本,粘贴代码,保存。记得打开插件的允许用户脚本。

使用说明

脚本会自动扫描页面文本,识别到合法题号就直接转为超链接,单击即可在新标签打开原题。

声明

本脚本由作者本人与 AI 合作完成。(不保证作者本人的贡献比 AI 大。)存在问题请私信。

SPOJ 的题号会在整个页面内搜索匹配的题目编号字段以更新url。如果没有找到那么跳转原站会失败(比如讨论区出现仅题号的位置)。

AT 的特殊比赛无法获取到原站链接。如AT1202Contest_a,其原站链接为https://atcoder.jp/contests/DEGwer2023/tasks/1202Contest_a。脚本技术限制无法获取到正确的url。你可以自行进入题目页点击题目来源跳转。

源码

INFO
JavaScript
// ==UserScript==
// @name         Luogu Problem Jumper
// @version      3.1
// @description  左键站内跳转,Ctrl+左键跳转原题站。
// @author       Queryme
// @match        https://www.luogu.com.cn/*
// @match        https://www.luogu.me/*
// @grant        none
// ==/UserScript==
(function() {
    'use strict';
    const pattern = /(?<!\w)(SP\d+\s+([A-Za-z0-9_]+)\s+-|CF\d+[A-Za-z][0-9]?|AT[\w]+|P\d{4,5}|SP\d+|[TU]\d+)/g;
    const spMap = new Map();
    let lastUrl = location.href;
    let timer = null;
    function refreshSpMapFromDom(){
        document.querySelectorAll('.row').forEach(row=>{
            const pidNode = row.querySelector('.pid');
            if(!pidNode) return;
            const spId = pidNode.textContent.trim();
            if(!/^SP\d+$/.test(spId)) return;
            if(spMap.has(spId)) return;
            const titleNode = row.querySelector('.title');
            if(!titleNode) return;
            const titleText = titleNode.textContent.trim();
            const dashPos = titleText.indexOf(' - ');
            if(dashPos === -1) return;
            const spojCode = titleText.substring(0, dashPos).trim();
            spMap.set(spId, spojCode);
        });
    }
    function parseInlineSp(text){
        const m = text.match(/^(SP(\d+))\s+([A-Za-z0-9_]+)\s+-/);
        if(!m) return null;
        const spId = m[1];
        const code = m[3];
        if(!spMap.has(spId)){
            spMap.set(spId, code);
        }
        return {
            innerId:spId,
            outerUrl:`https://www.spoj.com/problems/${code}/`
        };
    }
    function getOuterUrl(s){
        const spObj=parseInlineSp(s);
        if(spObj) return spObj.outerUrl;
        if(/^P\d{4,5}$/.test(s)){
            return `https://www.luogu.com.cn/problem/${s}`;
        }
        if(/^CF\d+[A-Za-z][0-9]?$/.test(s)){
            const numPart = s.match(/CF(\d+)([A-Za-z][0-9]?)/);
            return `https://codeforces.com/problemset/problem/${numPart[1]}/${numPart[2]}`;
        }
        if(/^AT/.test(s)){
            const low = s.toLowerCase();
            let raw = low.slice(2);
            raw = raw.replace(/^_/,'');
            let lastUnd = raw.lastIndexOf('_');
            const contestId = lastUnd===-1?raw:raw.substring(0,lastUnd).replaceAll('_','-');
            return `https://atcoder.jp/contests/${contestId}/tasks/${raw}`;
        }
        if(/^SP\d+$/.test(s)){
            if(spMap.has(s)){
                return `https://www.spoj.com/problems/${spMap.get(s)}/`;
            }else{
                return "";
            }
        }
        if(/^[TU]\d+$/.test(s)){
            return `https://www.luogu.com.cn/problem/${s}`;
        }
        return null;
    }
    function getInnerUrl(s){
        const m=s.match(/^(SP\d+)/);
        if(m) s=m[1];
        return `https://www.luogu.com.cn/problem/${s}`;
    }
    const skipTags = new Set(['SCRIPT','STYLE','A','BUTTON','TEXTAREA','INPUT','CODE','PRE']);
    function scanNode(node){
        if(node.nodeType === Node.TEXT_NODE){
            let txt = node.textContent;
            let parent = node.parentNode;
            if(skipTags.has(parent.tagName)) return;
            let matches = [];
            let res;
            pattern.lastIndex = 0;
            while((res = pattern.exec(txt)) !== null){
                const outerUrl = getOuterUrl(res[0]);
                if(outerUrl !== null){
                    matches.push({index:res.index, text:res[0], outer:outerUrl, inner:getInnerUrl(res[0])});
                }
            }
            matches.sort((a,b)=>a.index - b.index);
            let frag = document.createDocumentFragment();
            let last = 0;
            for(let m of matches){
                frag.appendChild(document.createTextNode(txt.slice(last, m.index)));
                let a = document.createElement('a');
                a.textContent = m.text;
                a.dataset.outer = m.outer;
                a.dataset.inner = m.inner;
                a.href = m.inner;
                a.target = "_blank";
                a.style.cssText = 'color:inherit;background:inherit;text-decoration:none;padding:0;border:none;';
                a.addEventListener('click',function(e){
                    if(e.ctrlKey||e.metaKey){
                        e.preventDefault();
                        window.open(this.dataset.outer,'_blank');
                    }
                });
                frag.appendChild(a);
                last = m.index + m.text.length;
            }
            frag.appendChild(document.createTextNode(txt.slice(last)));
            if(frag.childNodes.length > 1){
                parent.replaceChild(frag, node);
            }
            return;
        }
        for(let c of node.childNodes) scanNode(c);
    }
    function fullProcess(){
        refreshSpMapFromDom();
        scanNode(document.body);
    }
    function delayedRun(){
        clearTimeout(timer);
        timer = setTimeout(fullProcess,350);
    }
    const origPush = history.pushState;
    const origRepl = history.replaceState;
    history.pushState = function(){
        origPush.apply(this,arguments);
        delayedRun();
    };
    history.replaceState = function(){
        origRepl.apply(this,arguments);
        delayedRun();
    };
    const obs = new MutationObserver((mut)=>{
        let needRefresh = false;
        for(let m of mut){
            for(let n of m.addedNodes){
                if(n.nodeType === Node.ELEMENT_NODE && n.classList.contains('row')){
                    needRefresh = true;
                }
                scanNode(n);
            }
        }
        if(needRefresh){
            delayedRun();
        }
    });
    obs.observe(document.body, {childList:true, subtree:true});
    setInterval(()=>{
        if(location.href!==lastUrl){
            lastUrl = location.href;
            delayedRun();
        }
    },150);
    delayedRun();
})();
原文