Reddit Sort by Old

Automatically sorts Reddit comments by "old" without refreshing the feed unnecessarily.

目前為 2025-01-11 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Reddit Sort by Old
// @namespace    https://github.com/rayman1972
// @version      1.2
// @description  Automatically sorts Reddit comments by "old" without refreshing the feed unnecessarily.
// @author       Rayman30
// @license      GPL-3.0-or-later
// @match        https://www.reddit.com/*
// @match        https://sh.reddit.com/*
// @grant        none
// @run-at       document-start
// @noframes
// ==/UserScript==
'use strict';

(function () {
    function enforceOldSort() {
        try {
            if (location.pathname.includes('/comments/')) {
                const url = new URL(location.href);
                if (url.searchParams.get('sort') !== 'old') {
                    url.searchParams.set('sort', 'old');
                    // Update the URL without reloading the page
                    window.history.replaceState(null, '', url.toString());
                    console.log('Sorting by old applied without reloading.');
                }
            }
        } catch (error) {
            console.error('Error enforcing sort by old:', error);
        }
    }

    function init() {
        // Apply sorting immediately
        enforceOldSort();

        // Observe changes for dynamic content
        const observer = new MutationObserver(() => {
            enforceOldSort();
        });

        observer.observe(document, {
            childList: true,
            subtree: true,
        });

        console.log('Observer initialized for dynamic content.');
    }

    init();
})();