Reddit Text-Only Mode

Switches to text-only mode by reading the current Sort/Time settings directly from the UI.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Reddit Text-Only Mode
// @namespace    http://tampermonkey.net/
// @version      4.4
// @description  Switches to text-only mode by reading the current Sort/Time settings directly from the UI.
// @author       qualia
// @match        https://www.reddit.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function getScreenSettings() {
        let sort = 'hot';
        let time = '';

        const sortEl = document.querySelector('shreddit-sort-dropdown[sort-event="feed-sort-change"] div[slot="selected-item"]');
        if (sortEl) {
            const text = sortEl.innerText.trim().toLowerCase();
            if (text === 'top') sort = 'top';
            else if (text === 'new') sort = 'new';
            else if (text === 'hot') sort = 'hot';
            else if (text === 'best') sort = 'relevance'; 
            else if (text === 'rising') sort = 'new';
        }

        const timeEl = document.querySelector('shreddit-sort-dropdown[sort-event="feed-time-sort-change"] div[slot="selected-item"]');
        if (timeEl) {
            const text = timeEl.innerText.trim().toLowerCase();
            if (text.includes('now') || text.includes('hour')) time = 'hour';
            else if (text.includes('today') || text.includes('day')) time = 'day';
            else if (text.includes('week')) time = 'week';
            else if (text.includes('month')) time = 'month';
            else if (text.includes('year')) time = 'year';
            else if (text.includes('all')) time = 'all';
        }

        return { sort, time };
    }

    function getPageState() {
        const url = new URL(window.location.href);
        const path = url.pathname;
        const params = url.searchParams;
        const srMatch = path.match(/\/r\/([^/]+)/);
        const subreddit = srMatch ? srMatch[1] : null;
        const isSearch = path.includes('/search');
        const query = decodeURIComponent(params.get('q') || '');
        const isTextMode = isSearch && query.includes('self:yes');

        return { url, path, params, subreddit, isTextMode };
    }

    function toggleMode() {
        const state = getPageState();

        if (!state.subreddit) {
            alert("Please go to a specific subreddit (like r/manga) to use this filter.");
            return;
        }

        if (state.isTextMode) {
            const sort = state.params.get('sort');
            const time = state.params.get('t');

            let targetUrl = `https://www.reddit.com/r/${state.subreddit}/`;

            if (sort === 'top') targetUrl += 'top/';
            else if (sort === 'new') targetUrl += 'new/';
            else if (sort === 'hot') targetUrl += 'hot/';
            else if (sort === 'rising') targetUrl += 'rising/';

            if (time) targetUrl += `?t=${time}`;

            window.location.href = targetUrl;

        } else {

            let settings = getScreenSettings();

            if (!settings.sort || settings.sort === 'hot') {
                if (state.path.includes('/top/')) settings.sort = 'top';
                else if (state.path.includes('/new/')) settings.sort = 'new';
            }
            if (!settings.time) {
                if (state.params.get('t')) settings.time = state.params.get('t');
            }

            const baseUrl = `https://www.reddit.com/r/${state.subreddit}/search/`;
            const searchParams = new URLSearchParams();

            searchParams.set('q', 'self:yes');
            searchParams.set('restrict_sr', '1');
            searchParams.set('type', 'posts');
            searchParams.set('sort', settings.sort);

            if (settings.time) {
                searchParams.set('t', settings.time);
            }

            window.location.href = `${baseUrl}?${searchParams.toString()}`;
        }
    }

    function createButton() {
        if (document.getElementById('reddit-text-native-btn')) return;
        const state = getPageState();
        if (!state.subreddit) return;

        const btn = document.createElement('button');
        btn.id = 'reddit-text-native-btn';

        if (state.isTextMode) {
            btn.innerText = 'Text Mode: ON';
            btn.style.backgroundColor = '#228B22';
        } else {
            btn.innerText = 'Text Mode: OFF';
            btn.style.backgroundColor = '#FF4500';
        }

        Object.assign(btn.style, {
            position: 'fixed', bottom: '20px', right: '20px', zIndex: '999999',
            padding: '10px 15px', color: 'white', border: 'none', borderRadius: '20px',
            fontWeight: 'bold', cursor: 'pointer', boxShadow: '0 4px 6px rgba(0,0,0,0.3)',
            fontFamily: 'Verdana, sans-serif', fontSize: '14px', transition: 'background-color 0.2s',
            // --- Alignment Fixes ---
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            lineHeight: '1',
            boxSizing: 'border-box'
        });

        btn.onclick = toggleMode;
        document.body.appendChild(btn);
    }

    function init() {
        createButton();
        let lastUrl = location.href;
        setInterval(() => {
            if (location.href !== lastUrl) {
                lastUrl = location.href;
                const oldBtn = document.getElementById('reddit-text-native-btn');
                if (oldBtn) oldBtn.remove();
                createButton();
            }
        }, 1000);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

})();