Imgur links rewriting on Ptt

Rewrite imgur links to bypass referrer check.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Imgur links rewriting on Ptt
// @namespace   https://github.com/gslin/imgur-links-rewriting-on-ptt
// @match       https://www.ptt.cc/bbs/*
// @match       https://www.ptt.cc/man/*
// @grant       GM_xmlhttpRequest
// @version     0.20240912.0
// @author      Gea-Suan Lin <[email protected]>
// @description Rewrite imgur links to bypass referrer check.
// @license     MIT
// ==/UserScript==

(() => {
    'use strict';

    const get_imgur_id_suffix = url => {
        const a = url.match(/^https?:\/\/(i\.|m\.)?imgur\.com\/(\w+)\.?(\w+)?/);
        return [a[2], a[3]];
    };
    const re_imgur_album = /^https?:\/\/imgur\.com\/(a|gallery)\//;

    document.querySelectorAll('a[href^="https://imgur.com/"], a[href^="https://i.imgur.com/"], a[href^="https://m.imgur.com/"]').forEach(async el => {
        // Remove ".richcontent" if existing.
        const next = el.nextElementSibling;
        if (next && next.classList.contains('richcontent')) {
            next.remove();
        }

        // Remove ".richcontent" if existing.
        const next2 = el.parentElement.nextElementSibling;
        if (next2 && next2.classList.contains('richcontent')) {
            next2.remove();
        }

        // Remove ".richcontent" for ".push" case.
        const el_p2 = el.parentElement.parentElement;
        if (el_p2 && el_p2.classList.contains('push')) {
            const el_p2_next = el_p2.nextElementSibling;
            if (el_p2_next && el_p2_next.classList.contains('richcontent')) {
                el_p2_next.remove();
            }
        }

        const href = el.getAttribute('href');
        let imgur_id = '';
        let imgur_suffix = '';

        if (href.match(re_imgur_album)) {
            // album case.
            const res = await new Promise(resolve => {
                GM_xmlhttpRequest({
                    'anonymous': true,
                    'headers': {
                        'Referer': 'https://imgur.com/',
                    },
                    'method': 'GET',
                    'onload': res => {
                        resolve(res.responseText);
                    },
                    'url': href,
                });
            });

            const parser = new DOMParser();
            const doc = parser.parseFromString(res, 'text/html');

            const og_image = doc.querySelector('meta[property="og:image"]');
            const img_url = og_image.getAttribute('content');

            [imgur_id, imgur_suffix] = get_imgur_id_suffix(img_url);
        } else {
            // image case.
            [imgur_id, imgur_suffix] = get_imgur_id_suffix(href);
        }

        // Change to webp only if it's not gif.
        if (imgur_suffix !== 'gif') {
            imgur_suffix = 'webp';
        }

        const container = document.createElement('div');
        container.setAttribute('style', 'margin-top:0.5em;text-align:center;');

        const img = document.createElement('img');
        img.setAttribute('referrerpolicy', 'no-referrer');
        img.setAttribute('src', 'https://i.imgur.com/' + imgur_id + '.' + imgur_suffix);
        container.appendChild(img);

        el.insertAdjacentElement('afterend', container);
    });
})();