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!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
})();