洛谷链接自动跳转器

· · 个人记录

创作背景:洛谷关闭讨论区,文章和云剪贴板只能在国际站访问。

实现原理:
打开 https://www.luogu.com.cn/discuss/xxxxxx 时自动跳转到 https://lglg.top/xxxxxx
打开 https://www.luogu.com.cn/article/yyyyyy 时自动跳转到 https://www.luogu.me/article/yyyyyy
打开 https://www.luogu.com.cn/paste/zzzzzz 时自动跳转到 https://www.luogu.me/paste/zzzzzz

解决了用户看帖子时需要手动跳转的问题。

限制:只能打开帖子,文章,云剪贴板保存站保存了的内容。

油猴脚本:

// ==UserScript==
// @name         洛谷链接自动跳转器
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  自动将特定洛谷链接重定向到指定域名
// @author       我
// @match        *://www.luogu.com.cn/discuss/*
// @match        *://www.luogu.com.cn/article/*
// @match        *://www.luogu.com.cn/paste/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 获取当前完整路径
    const currentUrl = window.location.href;

    // 处理 discuss 跳转逻辑(数字ID)
    if (/^https:\/\/www\.luogu\.com\.cn\/discuss\/\d+$/.test(currentUrl)) {
        const discussId = currentUrl.split('/').pop();
        window.location.replace(`https://lglg.top/${discussId}`);
    }
    // 处理 article 跳转逻辑(字符串ID)
    else if (/^https:\/\/www\.luogu\.com\.cn\/article\/[^\/]+$/.test(currentUrl)) {
        const pathSegment = currentUrl.split('/article/')[1];
        window.location.replace(`https://www.luogu.me/article/${pathSegment}`);
    }
    // 处理 paste 跳转逻辑(字符串ID)
    else if (/^https:\/\/www\.luogu\.com\.cn\/paste\/[^\/]+$/.test(currentUrl)) {
        const pathSegment = currentUrl.split('/paste/')[1];
        window.location.replace(`https://www.luogu.me/paste/${pathSegment}`);
    }
})();

创作人员:DeepSeek