Enhanced Medium Redirect (Compressed)

Add buttons to open Medium articles in Freedium and Archive.is, hide recommended articles and more from author, adjust story width and image sizes while preserving zoom, then scroll to top

当前为 2024-10-10 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Enhanced Medium Redirect (Compressed)
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Add buttons to open Medium articles in Freedium and Archive.is, hide recommended articles and more from author, adjust story width and image sizes while preserving zoom, then scroll to top
// @author       573dave (improved by Claude)
// @match        https://*.medium.com/*
// @grant        GM_addStyle
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    const debug = m => console.log(`[Medium Redirect Debug]: ${m}`);
    const scrollTop = () => window.scrollTo({top: 0, behavior: 'smooth'});
    const hideElement = selector => { const el = document.querySelector(selector); if (el) { el.style.display = 'none'; debug(`Hidden: ${selector}`); return true; } return false; };
    const hideRecommended = () => hideElement('#root > div > div.l.c > div:nth-child(2) > div.tj.tk.tl.tm.tn.l.bx > div:nth-child(4)');
    const hideMoreFromAuthor = () => hideElement('#root > div > div.l.c > div:nth-child(2) > div.tj.tk.tl.tm.tn.l.bx > div:nth-child(2)');
    const adjustStoryWidth = () => {
        GM_addStyle(`
            article, 
            .story-body, 
            .story-content,
            div.ci.bh.fz.ga.gb.gc {
                width: 100% !important;
                max-width: 1192px !important;
                margin-left: auto !important;
                margin-right: auto !important;
                padding-left: 20px !important;
                padding-right: 20px !important;
            }
            .story-body {
                box-sizing: border-box !important;
            }
            figure.paragraph-image {
                max-width: 500px !important;
                margin-left: auto !important;
                margin-right: auto !important;
            }
            figure.paragraph-image > div[role="button"] {
                max-width: 100% !important;
            }
            figure.paragraph-image > div[role="button"] > div {
                max-width: 100% !important;
            }
        `);
        debug('Story width and image size styles added');
    };
    const clickClose = () => ['button[data-testid="close-button"]', 'button[aria-label="close"]', '#root > div > div.l.c > div:nth-child(2) > div.m.uy.uz.va > div > div > div > div.am.l.fs.vp.vq > div.h.k > div > button'].some(s => { const b = document.querySelector(s); if (b) { b.click(); debug('Close button clicked'); return true; } return false; }) || (debug('No close button found'), false);
    const addButtons = () => {
        if (document.querySelector('.medium-redirect-buttons')) return;
        const c = document.createElement('div');
        c.className = 'medium-redirect-buttons';
        c.style.cssText = 'position:fixed;top:10px;left:50%;transform:translateX(-50%);display:flex;align-items:center;gap:10px;z-index:10000;background:white;padding:5px;border-radius:5px;box-shadow:0 2px 5px rgba(0,0,0,0.2);';
        const bStyle = 'font-size:14px;padding:5px 10px;background-color:#FFC017;color:black;border:none;border-radius:5px;cursor:pointer;';
        c.innerHTML = '<span style="font-size:14px;font-weight:bold;">Load with:</span>';
        ['Freedium', 'Archive.is'].forEach(t => {
            const b = document.createElement('button');
            b.textContent = t;
            b.style.cssText = bStyle;
            b.onclick = () => { window.open(`https://${t.toLowerCase() === 'freedium' ? 'freedium.cfd/' : 'archive.is/'}${window.location.href}`, '_blank'); };
            c.appendChild(b);
        });
        document.body.appendChild(c);
        debug('Redirect buttons added');
    };
    const handle = () => { 
        debug('Checking for paywall...'); 
        if (clickClose()) {
            setTimeout(() => {
                if (!clickClose()) {
                    addButtons();
                    hideRecommended();
                    hideMoreFromAuthor();
                    adjustStoryWidth();
                    scrollTop();
                }
            }, 1000);
        } else {
            addButtons();
            hideRecommended();
            hideMoreFromAuthor();
            adjustStoryWidth();
            scrollTop();
        }
    };
    new MutationObserver(ms => ms.some(m => m.addedNodes.length && (debug('DOM mutation detected, rechecking...'), handle(), true))).observe(document.body, { childList: true, subtree: true });
    debug('Script started');
    handle();
})();