Greasy Fork

来自缓存

洛谷专栏保存站跳转

在洛谷专栏页面添加保存站跳转按钮

// ==UserScript==
// @name         洛谷专栏保存站跳转
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  在洛谷专栏页面添加保存站跳转按钮
// @author       Federico2903
// @match        https://www.luogu.com.cn/article/*
// @match        https://www.luogu.com/article/*
// @icon         https://www.luogu.com.cn/favicon.ico
// @grant        GM_addStyle
// @license      GPL-3.0-or-later
// ==/UserScript==

(function() {
    'use strict';
    let loc = 0;
    if (window.location.href.startsWith('https://www.luogu.com.cn')) loc = 1;
    if (!loc) {
        const checkContainer = setInterval(() => {
            const metasContainer = document.querySelector('.meta .metas');
            if (metasContainer) {
                clearInterval(checkContainer);
                const articleId = window.location.pathname.split('/')[2];
                const mirrorContainer = document.createElement('div');
                mirrorContainer.setAttribute('data-v-076e399a', '');
                mirrorContainer.setAttribute('style', 'margin-left: 1.5em');
                mirrorContainer.innerHTML = `
                <div data-v-076e399a class="label">保存站</div>
                <a class="mirror-link"
                   href="https://www.luogu.me/article/${articleId}"
                   target="_blank"
                   style="color: var(--lfe-color--blue-3);
                          text-decoration: none;
                          cursor: pointer;
                          transition: color 0.2s;">
                    前往专栏保存站
                </a>
            `;
                const link = mirrorContainer.querySelector('.mirror-link');
                link.addEventListener('mouseover', () => {
                    link.style.color = 'var(--lfe-color--blue-2)';
                });
                link.addEventListener('mouseout', () => {
                    link.style.color = 'var(--lfe-color--blue-3)';
                });

                metasContainer.appendChild(mirrorContainer);
            }
        }, 10);
    }
    else {
        const checkUrl = setInterval(() => {
            const cont = document.querySelector('#url');
            if (!cont) return;
            clearInterval(checkUrl);
            cont.innerHTML = cont.innerHTML.replaceAll('.com', '.me');
            const articleId = window.location.pathname.split('/')[2];
            const link = document.querySelector('a[href^="https://www.luogu.com/article/"]');
            link.setAttribute('href', link.href.replaceAll('.com', '.me'))
        }, 10);
    }
})();