Reddit Default Sort

Automatically sets Reddit's default sorting order for home and subreddits.

当前为 2025-02-11 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reddit Default Sort
// @version      1.2.0
// @description  Automatically sets Reddit's default sorting order for home and subreddits.
// @author       yodaluca23
// @license      MIT
// @match        *://*.reddit.com/*
// @grant        GM_getValue
// @grant        GM_setValue
// @namespace https://greasyfork.org/users/1315976
// ==/UserScript==

(function() {
    'use strict';

    // Default values
    const defaultHomePageSort = "new";
    const defaultSubredditSort = "new";
    const defaultCommentSort = "new";

    // This allows you to use the Reddit filters temporariliy, without it redirecting you to the default
    // This is a domain it should never actually be so it sorts to the default the first time, but doesn't fail being passed to URL()
    let lastUrl = "https://www.example.com";

    // Function to get the stored sort option, or the default if none is stored.
    function getSortOption(key, defaultValue) {
        let value = GM_getValue(key);
        return value === undefined ? defaultValue : value;
    }

    // Get the stored sort options
    let homePageSort = getSortOption("homePageSort", defaultHomePageSort);
    let subredditSort = getSortOption("subredditSort", defaultSubredditSort);
    let commentSort = getSortOption("commentSort", defaultCommentSort);

    // Function to redirect if necessary
    function redirectIfNeeded() {
        const currentUrl = window.location.href;

        // Comment page sorting
        const commentPattern = /https:\/\/www\.reddit\.com\/r\/[^/]+\/comments\/[^/]+\/[^/]+\/?/;
        if (commentPattern.test(currentUrl) && !currentUrl.includes(`?sort=${commentSort}`)) {
            const urlObj = new URL(currentUrl);
            const justRoot = urlObj.origin + urlObj.pathname;
            const urlObjLast = new URL(lastUrl);
            const justRootLast = urlObjLast.origin + urlObjLast.pathname;
            if (justRootLast != justRoot) {
              const newUrl = `${justRoot}?sort=${commentSort}`;
              window.location.replace(newUrl);
            }
        }

        // Check for home page URLs
        const homeUrls = [
            "https://www.reddit.com/",
            "https://www.reddit.com/?feed=home",
            "https://www.reddit.com/best/?feed=home",
            "https://www.reddit.com/hot/?feed=home",
            "https://www.reddit.com/top/?feed=home",
            "https://www.reddit.com/new/?feed=home",
            "https://www.reddit.com/rising/?feed=home"
        ];

        const homeTargetUrl = `https://www.reddit.com/${homePageSort}/?feed=home`;

        if (homeUrls.includes(currentUrl) && currentUrl !== homeTargetUrl && !homeUrls.includes(lastUrl)) {
            window.location.replace(homeTargetUrl);
            return;
        }

        // Check for subreddit URLs
        const subredditPattern = /https:\/\/www\.reddit\.com\/r\/([^/]+)(\/(hot|new|top|best|rising))?(\/)?(\?.*)?$/;

        if (subredditPattern.test(currentUrl)) {
            const match = currentUrl.match(subredditPattern);
            const subredditName = match[1];

            // Check if were still on the same sub indicates the user only changed their sort we should not set it back
            if (subredditPattern.test(lastUrl)) {
              const matchLast = lastUrl.match(subredditPattern);
              if (subredditName == matchLast[1]) {
                return;
              }
            }

            const targetUrl = `https://www.reddit.com/r/${subredditName}/${subredditSort}`;
            const altTargetUrl = `https://www.reddit.com/r/${subredditName}/${subredditSort}/`;

            if (currentUrl !== targetUrl && currentUrl !== altTargetUrl) {
                window.location.replace(targetUrl);
            }
        }
    }

    // Function to create the settings UI
    function createSettingsUI() {
      // Create the settings container
      const settingsContainer = document.createElement('div');
      settingsContainer.style.position = 'fixed';
      settingsContainer.style.top = '50%';
      settingsContainer.style.left = '50%';
      settingsContainer.style.transform = 'translate(-50%, -50%)';
      settingsContainer.style.padding = '20px';
      settingsContainer.style.borderRadius = '12px';
      settingsContainer.style.boxShadow = '0px 8px 20px rgba(0, 0, 0, 0.1)';
      settingsContainer.style.zIndex = '1000';
      settingsContainer.style.minWidth = '280px';
      settingsContainer.style.textAlign = 'center';
      settingsContainer.style.fontFamily = '"Arial", sans-serif';

      // Dark/Light Mode detection based on system preference
      const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
      if (isDarkMode) {
          settingsContainer.style.backgroundColor = '#111111'; // AMOLED dark mode
          settingsContainer.style.color = '#fff';
          settingsContainer.style.border = '1px solid #444'; // Softer border for dark mode
      } else {
          settingsContainer.style.backgroundColor = '#f9f9f9';
          settingsContainer.style.color = '#000';
          settingsContainer.style.border = '1px solid #ccc'; // Softer border for light mode
      }

      // Title
      const title = document.createElement('h2');
      title.textContent = 'Settings';
      title.style.marginBottom = '20px';
      settingsContainer.appendChild(title);

      // Home page sort select
      const homePageLabel = document.createElement('label');
      homePageLabel.textContent = 'Home Page Sort:';
      homePageLabel.style.display = 'block';
      homePageLabel.style.marginBottom = '10px';

      const homePageSelect = document.createElement('select');
      homePageSelect.id = 'homePageSortSelect';
      homePageSelect.style.width = '100%';
      homePageSelect.style.padding = '8px';
      homePageSelect.style.borderRadius = '5px';
      homePageSelect.style.backgroundColor = isDarkMode ? '#333' : '#fff'; // Darker dropdown in dark mode
      homePageSelect.style.color = isDarkMode ? '#fff' : '#000'; // Ensure text contrast is high
      homePageSelect.style.border = isDarkMode ? '1px solid #444' : '1px solid #ccc'; // Softer borders

      ['best', 'hot', 'new', 'top', 'rising'].forEach(option => {
          const opt = document.createElement('option');
          opt.value = option;
          opt.textContent = option.charAt(0).toUpperCase() + option.slice(1);
          homePageSelect.appendChild(opt);
      });
      homePageSelect.value = homePageSort;

      settingsContainer.appendChild(homePageLabel);
      settingsContainer.appendChild(homePageSelect);

      // Subreddit sort select
      const subredditLabel = document.createElement('label');
      subredditLabel.textContent = 'Subreddit Sort:';
      subredditLabel.style.display = 'block';
      subredditLabel.style.marginBottom = '10px';

      const subredditSelect = document.createElement('select');
      subredditSelect.id = 'subredditSortSelect';
      subredditSelect.style.width = '100%';
      subredditSelect.style.padding = '8px';
      subredditSelect.style.borderRadius = '5px';
      subredditSelect.style.backgroundColor = isDarkMode ? '#333' : '#fff'; // Darker dropdown in dark mode
      subredditSelect.style.color = isDarkMode ? '#fff' : '#000'; // Ensure text contrast is high
      subredditSelect.style.border = isDarkMode ? '1px solid #444' : '1px solid #ccc'; // Softer borders

      ['best', 'hot', 'new', 'top', 'rising'].forEach(option => {
          const opt = document.createElement('option');
          opt.value = option;
          opt.textContent = option.charAt(0).toUpperCase() + option.slice(1);
          subredditSelect.appendChild(opt);
      });
      subredditSelect.value = subredditSort;

      settingsContainer.appendChild(subredditLabel);
      settingsContainer.appendChild(subredditSelect);

      // Comments sort select
      const commentsLabel = document.createElement('label');
      commentsLabel.textContent = 'Comments Sort:';
      commentsLabel.style.display = 'block';
      commentsLabel.style.marginBottom = '10px';

      const commentSelect = document.createElement('select');
      commentSelect.id = 'commentSortSelect';
      commentSelect.style.width = '100%';
      commentSelect.style.padding = '8px';
      commentSelect.style.borderRadius = '5px';
      commentSelect.style.backgroundColor = isDarkMode ? '#333' : '#fff'; // Darker dropdown in dark mode
      commentSelect.style.color = isDarkMode ? '#fff' : '#000'; // Ensure text contrast is high
      commentSelect.style.border = isDarkMode ? '1px solid #444' : '1px solid #ccc'; // Softer borders

      const optionMapping = {
        best: 'confidence',
        top: 'top',
        new: 'new',
        controversial: 'controversial',
        old: 'old',
        'Q&A': 'qa'
      };

      Object.entries(optionMapping).forEach(([key, value]) => {
        const opt = document.createElement('option');
        opt.value = value;
        opt.textContent = key.charAt(0).toUpperCase() + key.slice(1);
        commentSelect.appendChild(opt);
      });

      commentSelect.value = commentSort;

      settingsContainer.appendChild(commentsLabel);
      settingsContainer.appendChild(commentSelect);

      // Buttons container
      const buttonsContainer = document.createElement('div');
      buttonsContainer.style.marginTop = '20px';
      buttonsContainer.style.display = 'flex';
      buttonsContainer.style.justifyContent = 'space-between';

      // Save button
      const saveButton = document.createElement('button');
      saveButton.textContent = 'Save Settings';
      saveButton.style.padding = '10px 20px';
      saveButton.style.borderRadius = '5px';
      saveButton.style.backgroundColor = '#4CAF50';
      saveButton.style.color = 'white';
      saveButton.style.border = 'none';
      saveButton.style.cursor = 'pointer';
      saveButton.style.transition = 'background-color 0.3s ease';
      saveButton.style.textAlign = 'center'; // Center text
      saveButton.style.display = 'flex';
      saveButton.style.alignItems = 'center';
      saveButton.style.justifyContent = 'center'; // Ensure text is centered
      saveButton.addEventListener('click', () => {
          homePageSort = document.getElementById('homePageSortSelect').value;
          subredditSort = document.getElementById('subredditSortSelect').value;
          commentSort = document.getElementById('commentSortSelect').value;

          GM_setValue("homePageSort", homePageSort);
          GM_setValue("subredditSort", subredditSort);
          GM_setValue("commentSort", commentSort);

          if (confirm('Settings Saved!\nWant to refresh now?')) {
            // User clicked "Yes" / "Ok"
            location.reload();
          }

          settingsContainer.remove(); // Remove the settings UI after saving
      });
      saveButton.addEventListener('mouseover', () => {
          saveButton.style.backgroundColor = '#45a049'; // Darker green on hover
      });
      saveButton.addEventListener('mouseout', () => {
          saveButton.style.backgroundColor = '#4CAF50'; // Reset to original green
      });

      // Close button
      const closeButton = document.createElement('button');
      closeButton.textContent = 'Close';
      closeButton.style.padding = '10px 20px';
      closeButton.style.borderRadius = '5px';
      closeButton.style.backgroundColor = '#f44336';
      closeButton.style.color = 'white';
      closeButton.style.border = 'none';
      closeButton.style.cursor = 'pointer';
      closeButton.style.transition = 'background-color 0.3s ease';
      closeButton.style.textAlign = 'center'; // Center text
      closeButton.style.display = 'flex';
      closeButton.style.alignItems = 'center';
      closeButton.style.justifyContent = 'center'; // Ensure text is centered
      closeButton.addEventListener('click', () => {
          settingsContainer.remove();
      });
      closeButton.addEventListener('mouseover', () => {
          closeButton.style.backgroundColor = '#e53935'; // Darker red on hover
      });
      closeButton.addEventListener('mouseout', () => {
          closeButton.style.backgroundColor = '#f44336'; // Reset to original red
      });

      // Append buttons
      buttonsContainer.appendChild(saveButton);
      buttonsContainer.appendChild(closeButton);
      settingsContainer.appendChild(buttonsContainer);

      // Append the settings container to the body
      document.body.appendChild(settingsContainer);
    }

    // Add a button to the page to open the settings
    function addSettingsButton() {
      const container = document.querySelector('.pl-lg.gap-xs.flex.items-center.justify-end');

      if (!container) {
          console.warn("Sort Settings button: Target container not found.");
          return; // Exit if container is missing
      }

      // Styling
      const settingsButton = document.createElement('button');
      settingsButton.textContent = 'Sort Settings';
      settingsButton.style.marginLeft = '5px';
      settingsButton.style.paddingLeft = '10px';
      settingsButton.style.paddingRight = '10px';
      settingsButton.style.backgroundColor = 'transparent';
      settingsButton.style.border = '1px solid white';
      settingsButton.className = 'btn btn-primary';
      settingsButton.addEventListener('click', createSettingsUI);

      container.appendChild(settingsButton);
    }



    // Initialize
    addSettingsButton();
    redirectIfNeeded();

      // Function to monitor URL changes in single-page app
    function observeUrlChanges(callback) {
        lastUrl = location.href;

        new MutationObserver(() => {
            if (location.href !== lastUrl) {
                lastUrl = location.href;
                callback();
            }
        }).observe(document, { subtree: true, childList: true });

        // Also intercept history changes
        const pushState = history.pushState;
        const replaceState = history.replaceState;

        history.pushState = function() {
            pushState.apply(this, arguments);
            callback();
        };
        history.replaceState = function() {
            replaceState.apply(this, arguments);
            callback();
        };
    }

    // Run `redirectIfNeeded` whenever Reddit changes the page
    observeUrlChanges(redirectIfNeeded);

})();