Greasy Fork

Greasy Fork is available in English.

自动跳转到 Reddit 官方中文翻译的页面

Automatically jump to the official Chinese translation page of Reddit

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         自动跳转到 Reddit 官方中文翻译的页面
// @namespace    reddit.com
// @version      0.8
// @description  Automatically jump to the official Chinese translation page of Reddit
// @author       xxnuo
// @match        *://*.reddit.com/*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const paramToAdd = 'tl=zh-hans';
    const redditPostPattern = /^(https?:\/\/)?([a-zA-Z0-9-]+\.)?reddit\.com\/r\/[a-zA-Z0-9_]+\/comments\/[a-zA-Z0-9_]+(\/[a-zA-Z0-9_]*\/?)?($|\?.*)/;

    let lastHandledUrl = '';
    let redirectCheckTimer = null;

    function performRedirectChecks() {
        const currentBrowserUrl = window.location.href;
        if (currentBrowserUrl === lastHandledUrl) {
            return;
        }
        if (!redditPostPattern.test(currentBrowserUrl)) {
            lastHandledUrl = currentBrowserUrl;
            return;
        }

        const currentUrlObj = new URL(currentBrowserUrl);

        if (currentUrlObj.searchParams.get('tl') !== 'zh-hans') {
            const oldTlParam = currentUrlObj.searchParams.get('tl');
            currentUrlObj.searchParams.set('tl', 'zh-hans');
            const newRedirectUrl = currentUrlObj.href;
            if (newRedirectUrl !== currentBrowserUrl) {
                lastHandledUrl = newRedirectUrl;
                window.location.replace(newRedirectUrl);
                return;
            }
        }
        lastHandledUrl = currentBrowserUrl;
    }

    function scheduleRedirectCheck() {
        clearTimeout(redirectCheckTimer);
        redirectCheckTimer = setTimeout(performRedirectChecks, 100);
    }
    performRedirectChecks();
    const observer = new MutationObserver((mutationsList, obs) => {
        scheduleRedirectCheck();
    });
    const targetNode = document.head || document.documentElement;
    if (targetNode) {
        observer.observe(targetNode, {
            childList: true,
            subtree: true,
            characterData: true
        });
    } else {
        const fallbackObserver = new MutationObserver((mutationsList, obs) => {
            scheduleRedirectCheck();
        });
        if (document.documentElement) {
            fallbackObserver.observe(document.documentElement, {
                childList: true,
                subtree: true
            });
        } else {
            window.addEventListener('DOMContentLoaded', () => {
                if (document.documentElement) {
                    fallbackObserver.observe(document.documentElement, {
                        childList: true,
                        subtree: true
                    });
                }
            });
        }
    }
})();