Reddit Force Hot Sort (Global)

Force Reddit to always use "hot" sort globally, including r/popular, r/all, all subreddits, and dynamic navigation (SPA). Works on both mobile and the Desktop!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reddit Force Hot Sort (Global)
// @namespace    https://reddit.com/
// @version      1.2
// @description  Force Reddit to always use "hot" sort globally, including r/popular, r/all, all subreddits, and dynamic navigation (SPA). Works on both mobile and the Desktop! 
// @author       Ryan Noles
// @match        *://www.reddit.com/*
// @run-at       document-start
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const enforceHotSort = () => {
        const url = new URL(window.location.href);
        const pathname = url.pathname;

        // Skip the comment threads and user profiles
        if (pathname.match(/^\/r\/[^\/]+\/comments\//) || pathname.startsWith('/user/')) return;

        // Already sorted by hot? 
        if (pathname.includes('/hot')) return;

        let newPath;

        // Main homepage when site is accessed....
        if (pathname === '/' || pathname === '') {
            newPath = '/hot';
        }
        // r/popular or r/all subs
        else if (pathname === '/r/popular' || pathname === '/r/popular/') {
            newPath = '/r/popular/hot';
        }
        else if (pathname === '/r/all' || pathname === '/r/all/') {
            newPath = '/r/all/hot';
        }
        // Any other subreddit will also be applied 
        else {
            const match = pathname.match(/^\/r\/[^\/]+\/?$/);
            if (match) {
                newPath = pathname.replace(/\/$/, '') + '/hot';
            }
        }

        if (newPath) {
            const target = newPath + url.search;
            history.replaceState(null, '', target);
            location.reload(); // force page reload due to Reddit SPA
        }
    };

    const interceptSPA = () => {
        const _pushState = history.pushState;
        const _replaceState = history.replaceState;

        history.pushState = function () {
            _pushState.apply(this, arguments);
            setTimeout(enforceHotSort, 50);
        };

        history.replaceState = function () {
            _replaceState.apply(this, arguments);
            setTimeout(enforceHotSort, 50);
        };

        window.addEventListener('popstate', enforceHotSort);
    };

    interceptSPA();
    enforceHotSort();
})();