您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Automatically sets Reddit's default sorting order for home and subreddits.
当前为
- // ==UserScript==
- // @name Reddit Default Sort
- // @version 1.1.1
- // @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";
- // This allows you to use the Reddit filters temporariliy, without it redirecting you to the default
- let lastUrl = "";
- // 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);
- // Function to redirect if necessary
- function redirectIfNeeded() {
- const currentUrl = window.location.href;
- // 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', 'top', 'new', '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', 'top', 'new', '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);
- // 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;
- GM_setValue("homePageSort", homePageSort);
- GM_setValue("subredditSort", subredditSort);
- alert('Settings Saved!');
- 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
- }
- const settingsButton = document.createElement('button');
- settingsButton.textContent = 'Sort Settings';
- settingsButton.style.marginLeft = '10px'; // Add spacing
- settingsButton.className = 'btn btn-primary'; // Reddit-style button class
- 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);
- })();