跳到正文

科技·工程

.com.cn 快速跳转 .me

Crunch_Time约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月16日
篇幅
约 9 分钟

版本

  1. v1当前

打基础赛的时候发现用手把 .com.cn 改成 .me 太慢了,于是有了这个。

大概功能就是剪贴板和文章直接给你跳转到保存站去,然后下面靠按钮搞了个双向边。

不会判全站推荐 / 管理员 / 用户自己,就这样吧。

全部由 DeepSeek 完成的油猴插件(

JavaScript
// ==UserScript==
// @name         洛谷域名智能切换(稳定无循环版)
// @namespace    http://tampermonkey.net/
// @version      3.0
// @description  彻底解决跳转循环问题,支持paste/article路径
// @author       You
// @match        https://www.luogu.com.cn/paste*
// @match        https://www.luogu.me/paste*
// @match        https://www.luogu.com.cn/article*
// @match        https://www.luogu.me/article*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    // 配置参数
    const CONFIG = {
        COOLDOWN_TIME: 5000,    // 跳转冷却时间(毫秒)
        MARKER_EXPIRE: 15000,   // 跳转标记有效期
        CHECK_INTERVAL: 3000    // 状态检查间隔
    };

    const currentUrl = window.location.href;
    const currentPath = window.location.pathname;
    const isCN = currentUrl.includes('luogu.com.cn');
    const isME = currentUrl.includes('luogu.me');
    const pathType = currentPath.split('/')[1] || ''; // 获取路径类型

    // 不处理的情况(不跳转也不显示按钮)
    const shouldSkip = () => {
        const skipPaths = [
            `/${pathType}`,                     // 无斜杠根路径
            `/${pathType}/mine`,                // 特别排除的mine路径
            new RegExp(`\\/${pathType}\\/[^\\/]+\\/edit`)  // 编辑页面
        ];
        return skipPaths.some(pattern =>
            pattern instanceof RegExp ? pattern.test(currentPath) : currentPath === pattern
        );
    };

    // 创建切换按钮
    const createSwitchButton = (targetUrl, buttonText, isMirror) => {
        const button = document.createElement('button');
        button.textContent = buttonText;
        Object.assign(button.style, {
            position: 'fixed',
            bottom: '20px',
            right: '20px',
            zIndex: '9999',
            padding: '8px 16px',
            backgroundColor: isMirror ? '#2196F3' : '#4CAF50',
            color: 'white',
            border: 'none',
            borderRadius: '4px',
            cursor: 'pointer',
            fontSize: '14px',
            fontWeight: 'bold',
            boxShadow: '0 2px 5px rgba(0,0,0,0.2)'
        });

        button.addEventListener('click', () => {
            GM_setValue(`last_${pathType}_switch`, Date.now());
            window.location.href = targetUrl;
        });

        document.body.appendChild(button);
        return button;
    };

    // 跳转状态检测
    const canAutoRedirect = () => {
        const lastSwitch = GM_getValue(`last_${pathType}_switch`, 0);
        const currentDomain = isCN ? 'cn' : 'me';
        const lastDomain = GM_getValue(`last_${pathType}_domain`, '');

        // 满足所有条件才允许跳转:
        // 1. 已过冷却时间
        // 2. 不是刚从目标域名过来
        // 3. 当前不是镜像站
        return (
            (Date.now() - lastSwitch > CONFIG.COOLDOWN_TIME) &&
            lastDomain !== 'me' &&
            isCN
        );
    };

    // 主逻辑
    if (shouldSkip()) return;

    if (isCN && currentPath.startsWith(`/${pathType}/`)) {
        const mirrorUrl = currentUrl.replace('luogu.com.cn', 'luogu.me');

        // 先记录当前域名状态
        GM_setValue(`last_${pathType}_domain`, 'cn');

        createSwitchButton(mirrorUrl, `切换到镜像站`, true);

        if (canAutoRedirect()) {
            GM_setValue(`last_${pathType}_domain`, 'me');
            window.location.href = mirrorUrl;
        }
    }
    else if (isME && currentPath.startsWith(`/${pathType}`)) {
        const officialUrl = currentUrl.replace('luogu.me', 'luogu.com.cn');
        GM_setValue(`last_${pathType}_domain`, 'me');
        createSwitchButton(officialUrl, `返回官方站`, false);
    }

    // 定期清理过期标记
    const cleanup = () => {
        const now = Date.now();
        ['paste', 'article'].forEach(type => {
            const lastSwitch = GM_getValue(`last_${type}_switch`, 0);
            if (lastSwitch && (now - lastSwitch > CONFIG.MARKER_EXPIRE)) {
                GM_setValue(`last_${type}_switch`, 0);
                GM_setValue(`last_${type}_domain`, '');
            }
        });
    };

    // 初始化定时器
    setInterval(cleanup, CONFIG.CHECK_INTERVAL);
    cleanup(); // 立即执行一次清理
})();

// 棒棒糖真好吃 /bangbangt
// 关注 /user/754021 谢谢喵!

嗯,喵喵喜欢偷懒,就这样。

哦对了快给喵喵点赞,为了写这个基础赛没有 AK,快用小红心抚慰喵喵受伤的等级分 /kel

有锅来骂我,我马上就去骂 DeepSeek。

不知道以后会不会加东西。

原文