Reddit Default Sort

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

目前为 2025-02-10 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit Default Sort
  3. // @version 1.0.0
  4. // @description Automatically sets Reddit's default sorting order for home and subreddits.
  5. // @author yodaluca23
  6. // @license MIT
  7. // @match *://*.reddit.com/*
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @namespace https://greasyfork.org/users/1315976
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.     'use strict';
  15.  
  16.     // Default values
  17.     const defaultHomePageSort = "new";
  18.     const defaultSubredditSort = "new";
  19.  
  20.     // Function to get the stored sort option, or the default if none is stored.
  21.     function getSortOption(key, defaultValue) {
  22.         let value = GM_getValue(key);
  23.         return value === undefined ? defaultValue : value;
  24.     }
  25.  
  26.     // Get the stored sort options
  27.     let homePageSort = getSortOption("homePageSort", defaultHomePageSort);
  28.     let subredditSort = getSortOption("subredditSort", defaultSubredditSort);
  29.  
  30.     // Function to redirect if necessary
  31.     function redirectIfNeeded() {
  32.         const currentUrl = window.location.href;
  33.  
  34.         // Check for home page URLs
  35.         const homeUrls = [
  36.             "https://www.reddit.com/",
  37.             "https://www.reddit.com/?feed=home",
  38.             "https://www.reddit.com/best/?feed=home",
  39.             "https://www.reddit.com/hot/?feed=home",
  40.             "https://www.reddit.com/top/?feed=home",
  41.             "https://www.reddit.com/new/?feed=home",
  42.             "https://www.reddit.com/rising/?feed=home"
  43.         ];
  44.  
  45.         const homeTargetUrl = `https://www.reddit.com/${homePageSort}/?feed=home`;
  46.  
  47.         if (homeUrls.includes(currentUrl) && currentUrl !== homeTargetUrl) {
  48.             window.location.replace(homeTargetUrl);
  49.             return;
  50.         }
  51.  
  52.         // Check for subreddit URLs
  53.         const subredditPattern = /https:\/\/www\.reddit\.com\/r\/([^/]+)(\/)?(\?.*)?$/;
  54.  
  55.         if (subredditPattern.test(currentUrl)) {
  56.             const match = currentUrl.match(subredditPattern);
  57.             const subredditName = match[1];
  58.             const targetUrl = `https://www.reddit.com/r/${subredditName}/${subredditSort}`;
  59.  
  60.             if (currentUrl !== targetUrl) {
  61.                 window.location.replace(targetUrl);
  62.             }
  63.         }
  64.     }
  65.  
  66.     // Function to create the settings UI
  67.     function createSettingsUI() {
  68.         // Create the settings container
  69.         const settingsContainer = document.createElement('div');
  70.         settingsContainer.style.position = 'fixed';
  71. settingsContainer.style.top = '50%';
  72. settingsContainer.style.left = '50%';
  73. settingsContainer.style.transform = 'translate(-50%, -50%)';
  74. settingsContainer.style.backgroundColor = 'white';
  75. settingsContainer.style.border = '1px solid #ccc';
  76. settingsContainer.style.padding = '15px';
  77. settingsContainer.style.borderRadius = '10px';
  78. settingsContainer.style.boxShadow = '0px 4px 10px rgba(0, 0, 0, 0.2)';
  79. settingsContainer.style.zIndex = '1000';
  80. settingsContainer.style.minWidth = '250px';
  81. settingsContainer.style.textAlign = 'center';
  82.  
  83.         // Home page sort select
  84.         const homePageLabel = document.createElement('label');
  85.         homePageLabel.textContent = 'Home Page Sort:';
  86.         const homePageSelect = document.createElement('select');
  87.         homePageSelect.id = 'homePageSortSelect';
  88.         ['best', 'hot', 'top', 'new', 'rising'].forEach(option => {
  89.             const opt = document.createElement('option');
  90.             opt.value = option;
  91.             opt.textContent = option.charAt(0).toUpperCase() + option.slice(1);
  92.             homePageSelect.appendChild(opt);
  93.         });
  94.         homePageSelect.value = homePageSort;
  95.  
  96.         // Subreddit sort select
  97.         const subredditLabel = document.createElement('label');
  98.         subredditLabel.textContent = 'Subreddit Sort:';
  99.         const subredditSelect = document.createElement('select');
  100.         subredditSelect.id = 'subredditSortSelect';
  101.         ['best', 'hot', 'top', 'new', 'rising'].forEach(option => {
  102.             const opt = document.createElement('option');
  103.             opt.value = option;
  104.             opt.textContent = option.charAt(0).toUpperCase() + option.slice(1);
  105.             subredditSelect.appendChild(opt);
  106.         });
  107.         subredditSelect.value = subredditSort;
  108.  
  109.         // Save button
  110.         const saveButton = document.createElement('button');
  111.         saveButton.textContent = 'Save Settings';
  112.         saveButton.addEventListener('click', () => {
  113.             homePageSort = document.getElementById('homePageSortSelect').value;
  114.             subredditSort = document.getElementById('subredditSortSelect').value;
  115.  
  116.             GM_setValue("homePageSort", homePageSort);
  117.             GM_setValue("subredditSort", subredditSort);
  118.  
  119.             alert('Settings Saved!');
  120.             settingsContainer.remove(); // Remove the settings UI after saving
  121.             redirectIfNeeded(); // Redirect immediately after saving
  122.         });
  123.  
  124.         // Close button
  125.         const closeButton = document.createElement('button');
  126.         closeButton.textContent = 'Close';
  127.         closeButton.addEventListener('click', () => {
  128.             settingsContainer.remove();
  129.         });
  130.  
  131.         // Append elements
  132.         settingsContainer.appendChild(homePageLabel);
  133.         settingsContainer.appendChild(homePageSelect);
  134.         settingsContainer.appendChild(subredditLabel);
  135.         settingsContainer.appendChild(subredditSelect);
  136.         settingsContainer.appendChild(saveButton);
  137.         settingsContainer.appendChild(closeButton);
  138.  
  139.         document.body.appendChild(settingsContainer);
  140.     }
  141.  
  142.     // Add a button to the page to open the settings
  143. function addSettingsButton() {
  144. const container = document.querySelector('.pl-lg.gap-xs.flex.items-center.justify-end');
  145.  
  146. if (!container) {
  147. console.warn("Sort Settings button: Target container not found.");
  148. return; // Exit if container is missing
  149. }
  150.  
  151. const settingsButton = document.createElement('button');
  152. settingsButton.textContent = 'Sort Settings';
  153. settingsButton.style.marginLeft = '10px'; // Add spacing
  154. settingsButton.className = 'btn btn-primary'; // Reddit-style button class
  155. settingsButton.addEventListener('click', createSettingsUI);
  156.  
  157. container.appendChild(settingsButton);
  158. }
  159.  
  160.  
  161.     // Initialize
  162.     addSettingsButton();
  163.     redirectIfNeeded();
  164.  
  165. // Function to monitor URL changes in single-page app
  166. function observeUrlChanges(callback) {
  167. let lastUrl = location.href;
  168.  
  169. new MutationObserver(() => {
  170. if (location.href !== lastUrl) {
  171. lastUrl = location.href;
  172. callback();
  173. }
  174. }).observe(document, { subtree: true, childList: true });
  175.  
  176. // Also intercept history changes
  177. const pushState = history.pushState;
  178. const replaceState = history.replaceState;
  179.  
  180. history.pushState = function() {
  181. pushState.apply(this, arguments);
  182. callback();
  183. };
  184. history.replaceState = function() {
  185. replaceState.apply(this, arguments);
  186. callback();
  187. };
  188. }
  189.  
  190. // Run `redirectIfNeeded` whenever Reddit changes the page
  191. observeUrlChanges(redirectIfNeeded);
  192.  
  193. })();