Reddit Configurable Comment Sorter

Redirects Reddit posts to sort comments with user-configurable sort order (default: top).

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Reddit Configurable Comment Sorter
// @namespace    https://example.com/
// @version      2.2
// @description  Redirects Reddit posts to sort comments with user-configurable sort order (default: top).
// @author       diggerman987654321
// @match        https://www.reddit.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @grant        GM.getValue
// @grant        GM.setValue
// @grant        GM.registerMenuCommand
// @license      MIT
// ==/UserScript==

/* jshint esversion: 10 */
/* jshint -W014 */

const redditPostRegex = /^https:\/\/www\.reddit\.com\/r\/[A-Za-z0-9][A-Za-z0-9_]{2,20}\/comments\/[a-zA-Z0-9]+/;
const isSortedRegex = /[?&]sort=/;
let currentUrl = null;

const sortOptions = {
    none: 'none',
    confidence: 'confidence',
    best: 'confidence',
    hot: 'hot',
    top: 'top',
    new: 'new',
    controversial: 'controversial',
    old: 'old',
    random: 'random',
    qa: 'qa'
};

const availableOptions = Object.keys(sortOptions).join(', ');

const defaultSortOrder = 'top';

async function getSortOrder() {
    try {
        return await GM.getValue('reddit_sort_order', defaultSortOrder);
    }
    catch (error) {
        console.error('Error getting sort order:', error);
        return defaultSortOrder;
    }
}

async function setSortOrder(sortOrder) {
    try {
        await GM.setValue('reddit_sort_order', sortOrder);
    }
    catch (error) {
        console.error('Error setting sort order:', error);
    }
}

function registerSortMenuCommands() {
    GM.registerMenuCommand('Change comment sort order', async () => {
        let userInput = prompt('Enter sort option:');

        if (userInput === null) {
            // User cancelled the prompt
            return;
        }

        userInput = userInput.trim().toLowerCase();

        if (userInput !== '' && sortOptions.hasOwnProperty(userInput)) {
            const sortValue = sortOptions[userInput];
            await setSortOrder(sortValue);
            alert(`Comment sort order changed to: ${userInput}`);
        }
        else {
            alert(`Invalid sort option. Available options: ${availableOptions}`);
        }
    });

    GM.registerMenuCommand('Disable manual sorting (uses Reddit\'s default)', async () => {
        await setSortOrder('none');
        alert('Comment sorting disabled');
    });

    GM.registerMenuCommand('Show current sort order', async () => {
        const currentSort = await getSortOrder();
        alert(`Current comment sort order: ${currentSort}`);
    });
}

function isCommentPermalink(url) {
    try {
        const urlObj = new URL(url);
        const pathSegments = urlObj.pathname.split('/').filter(segment => segment !== '');
        return pathSegments.length > 5;
    }
    catch {
        return false;
    }
}

function isRedditPostNotSorted(redditUrl) {
    return !isSortedRegex.test(redditUrl) && redditPostRegex.test(redditUrl);
}

async function redirectIfRedditPostNotSorted() {
    if (window.location.href === currentUrl) {
        return;
    }

    currentUrl = window.location.href;

    if (isRedditPostNotSorted(currentUrl)) {
        const sortOrder = await getSortOrder();

        // Don't sort if user has selected 'none'
        if (sortOrder === 'none') {
            return;
        }

        const appendChar = currentUrl.includes('?') ? '&' : '?';
        const sortParams = isCommentPermalink(currentUrl)
            ? `force-legacy-sct=1&sort=${sortOrder}`
            : `sort=${sortOrder}`;
        window.location.replace(`${currentUrl}${appendChar}${sortParams}`);
    }
}

(function() {
    'use strict';

    registerSortMenuCommands();

    redirectIfRedditPostNotSorted();

    const observer = new MutationObserver(redirectIfRedditPostNotSorted);
    observer.observe(document.head, {
        childList: true,
        subtree: true
    });

    window.addEventListener('popstate', redirectIfRedditPostNotSorted);

    const originalPushState = history.pushState;
    history.pushState = function(...args) {
        originalPushState.apply(this, args);
        redirectIfRedditPostNotSorted();
    };

    const originalReplaceState = history.replaceState;
    history.replaceState = function(...args) {
        originalReplaceState.apply(this, args);
        redirectIfRedditPostNotSorted();
    };
})();