跳到正文

科技·工程

个人主页查看脚本

ds_SegmentTree约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年10月15日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 8 分钟

版本

  1. v1当前

众所周知洛谷最近更新了个人主页,个人介绍变成了国际服专属,但是国际服暂时上不去,所以洛谷保存站做了一个大好事:将个人主页加入了保存站。但是我认为每次访问主页的时候改链接有点过于麻烦,所以就有了这个脚本。

代码
JavaScript
// ==UserScript==
// @name         个人主页查看
// @namespace    https://github.com/LYkcul
// @description  添加跳转到luogu.me的按钮
// @author       Segment_Tree
// @license      MIT
// @version      1.0.0
// @match        https://www.luogu.com.cn/user/*
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  function injectStyle() {
    var style = document.createElement("style");
    style.textContent = `
    .lcolor-var-green-3 {
        --lcolor-rgb: 82, 196, 26;
    }
    .lcolor-var-blue-3 {
        --lcolor-rgb: 52, 152, 219;
    }
    .lcolor-var-orange-3 {
        --lcolor-rgb: 230, 126, 34;
    }
    .redirect-btn {
        --l-button--real-color: var(--lcolor-rgb, var(--lcolor--primary, var(--lcolor--primary, 52, 152, 219)));
        display: inline-block;
        outline: none;
        cursor: pointer;
        font-weight: inherit;
        line-height: 1.5;
        text-align: center;
        vertical-align: middle;
        border-radius: 3px;
        border: 1px solid rgb(var(--l-button--real-color));
        background: rgba(var(--l-button--real-color),0) none;
        color: rgb(var(--l-button--real-color));
        margin-right: .5em;
        padding: 4px 12px;
        font-size: 14px;
        transition: all 0.2s;
    }
    .redirect-btn:hover {
        background: rgba(var(--l-button--real-color), 0.1);
    }
    `;
    document.head.appendChild(style);
  }

  function getUserIdFromUrl() {
    const path = window.location.pathname;
    const match = path.match(/^\/user\/(\d+)$/);
    return match ? match[1] : null;
  }

  function createRedirectButton() {
    const userId = getUserIdFromUrl();
    if (!userId) return null;

    const button = document.createElement("button");
    button.textContent = "查看个人主页";
    button.className = "lfe-form-sz-middle lcolor-var-blue-3 redirect-btn"; // 改为蓝色
    button.type = "button";

    button.addEventListener("click", function() {
      const newUrl = `https://luogu.me/user/${userId}`;
      window.open(newUrl, '_blank');
    });

    return button;
  }

  function insertButton() {
    // 检查是否已经存在按钮
    if (document.querySelector("#luogu-redirect-btn")) return;

    const nav = document.querySelector(".user-nav");
    if (!nav) return;

    const button = createRedirectButton();
    if (button) {
      button.id = "luogu-redirect-btn";
      nav.insertBefore(button, nav.firstChild);
    }
  }

  function handleUrlChange() {
    injectStyle();

    // 检查当前页面是否为用户页面
    if (!getUserIdFromUrl()) return;

    const start = Date.now();
    const timer = setInterval(() => {
      if (insertButton()) {
        clearInterval(timer);
        return;
      }
      if (Date.now() - start > 3000) clearInterval(timer);
    }, 100);
  }

  // 监听URL变化
  const fireUrlChange = () =>
    window.dispatchEvent(new Event("userscript:urlchange"));

  const _ps = history.pushState,
    _rs = history.replaceState;

  history.pushState = function (...a) {
    const r = _ps.apply(this, a);
    fireUrlChange();
    return r;
  };

  history.replaceState = function (...a) {
    const r = _rs.apply(this, a);
    fireUrlChange();
    return r;
  };

  window.addEventListener("popstate", fireUrlChange);
  window.addEventListener("hashchange", fireUrlChange);
  window.addEventListener("userscript:urlchange", () => {
    document.querySelectorAll("#luogu-redirect-btn").forEach(el => el.remove());
    setTimeout(handleUrlChange, 0);
  });

  // 初始化
  const init = () => handleUrlChange();
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", init);
  } else {
    init();
  }
})();

进入个人主页时,会在右上角显示按钮,点击就能传送到保存站。

bug

只能在“主页”栏使用,其他地方暂时不能显示按钮。

AI 使用说明

此代码是 deepseek 写的。

原文