From 5afc10dfcdf8fc5407a47651233aba7a6003c1eb Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Mon, 12 Oct 2026 20:23:54 -0700 Subject: [PATCH] Add TopCommentSort.os.js: sort Reddit post comments by Top by default Co-Authored-By: Claude Opus 5.5 --- README.md | 19 ++++++++++++++ TopCommentSort.os.js | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 TopCommentSort.os.js diff --git a/README.md b/README.md index 9076b1a..53a65f7 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,22 @@ Embeds relevant Reddit discussion threads and comments directly above the native 3. Copy and paste the contents of [`YouTube.os.js`](YouTube.os.js) into the editor. 4. Save the script and ensure it is enabled. 5. Open any YouTube video with Reddit discussions (e.g. trailers, music videos, podcasts). + +### 2. Top Comment Sort (`TopCommentSort.os.js`) + +Opens every Reddit post with its comments sorted by **Top** instead of Best or a subreddit's suggested sort. + +#### Features +- **Top by Default:** Any post opened without an explicit sort loads with `?sort=top`, including links from feeds, search, profiles, and comment threads. +- **No Double Loads:** Post links are pointed at the sorted URL before they are followed, so Reddit's in-page navigation loads Top directly instead of loading twice. +- **Direct Visits Too:** Posts opened from the address bar, a new tab, or an external site are switched to Top before the page renders. +- **Your Choice Wins:** Picking another sort from the post's sort menu (including Best) is respected and never overridden. +- **Explicit Links Respected:** Links that already carry a sort, such as `?sort=new`, are left alone. +- **Selector Free:** Works through URLs and the browser Navigation API rather than Reddit's markup, so redesigns are unlikely to break it. + +#### Installation +1. Install [OpenScript](https://chromewebstore.google.com/detail/openscript/dkelmgdchndagjemmodhkphdikhpnfol). +2. Open the OpenScript popup and click **New Script**. +3. Copy and paste the contents of [`TopCommentSort.os.js`](TopCommentSort.os.js) into the editor. +4. Save the script and ensure it is enabled. +5. Open any post on [reddit.com](https://www.reddit.com). diff --git a/TopCommentSort.os.js b/TopCommentSort.os.js new file mode 100644 index 0000000..9eb3e0a --- /dev/null +++ b/TopCommentSort.os.js @@ -0,0 +1,61 @@ +// ==UserScript== +// @name Top Comment Sort +// @version 1.0.0 +// @description Opens every Reddit post with its comments sorted by Top, while still letting you pick another sort. +// @match https://www.reddit.com/* +// @run-at document_start +// ==/UserScript== + +const POST = /^\/(?:(?:r|u|user)\/[^/]+\/)?comments\/[a-z0-9]+/i; +const KEY = 'os-top-comment-sort'; + +// Sorted URL for a post page without an explicit sort, otherwise null +const sorted = raw => { + const url = new URL(raw, location.href); + if (url.origin !== location.origin || !POST.test(url.pathname) || url.searchParams.has('sort')) return null; + url.searchParams.set('sort', 'top'); + return url.href; +}; + +// One-shot pass for a path, so a page we already handled (or a sort you picked) is left alone on load +const mark = path => { try { sessionStorage.setItem(KEY, path); } catch {} }; +const passed = () => { + try { + const hit = sessionStorage.getItem(KEY) === location.pathname; + sessionStorage.removeItem(KEY); + return hit; + } catch { return false; } +}; + +// Direct visits: address bar, new tabs, external links +const start = sorted(location.href); +if (!passed() && start) { + mark(location.pathname); + return location.replace(start); +} + +// In-page navigation to another post: go to the sorted URL instead +navigation.addEventListener('navigate', e => { + const url = new URL(e.destination.url); + // Same post with a different sort (e.g. the sort dropdown picking Best): respect it + if (url.pathname === location.pathname) { + if (!url.searchParams.has('sort')) mark(url.pathname); + return; + } + const target = e.navigationType === 'push' && e.cancelable && !e.hashChange + && !e.formData && !e.downloadRequest && sorted(url); + if (!target) return; + e.preventDefault(); + e.stopImmediatePropagation(); + const nav = navigation.navigate(target, { state: e.destination.getState(), info: e.info }); + nav.committed.catch(() => {}); + nav.finished.catch(() => {}); +}); + +// Point post links at the sorted URL before they are followed, so the router loads Top directly +const rewrite = e => { + const a = e.composedPath().find(n => n instanceof HTMLAnchorElement && n.href); + const target = a && new URL(a.href).pathname !== location.pathname && sorted(a.href); + if (target) a.href = target; +}; +['mousedown', 'click', 'auxclick', 'focusin'].forEach(type => addEventListener(type, rewrite, true));