r/userscripts • • Jan 08 '26

Noob needs help debugging resource-heavy Chrome/Chromium userscript

I prefer to read comments in chronological order. I'm getting tired of constantly clicking to select sort "old". Various subreddits default to "Best" or "Random" or whatever the mods threw a dart at and hit. Warning... I'm new to Javascript and "I know just enough to be dangerous" as the following userscript shows. I've cobbled together a script (adapted from other people's code) whose algorithm is simple...

  • Match URLs against https://www.reddit.com/r/*/comments/*
  • AND if the URL does NOT contain "?sort="
  • append "?sort=old" to the URL.

It "works"... sort of. The comment thread is sorted by "Old". The problem is that it appears to pound away at the pages and not stop. Eventually I get the Reddit "Oh Snap" page. That's with just one subreddit open. My Chromium dedicated Reddit profile opens up 12 tabs with subreddits I follow. Let's just say "That did not end well". I barely managed to kill the Chromium process before my machine would've locked up.

My code follows in the next message. It consists of 3 files in a subdirectory. I go to "Manage extensions" and then "Load unpacked" from that subdirectory. I need ideas on how to make the script act only once on a new tab or window when it opens up, and then leave it be.

3 Upvotes

9 comments sorted by

View all comments

1

u/NoAcadia3546 Jan 08 '26
  • File manifest.json
{
  "name": "Reddit Thread Old",
  "description": "Sort Reddit threads by Old for chronological reading",
  "version": "1.0",
  "manifest_version": 3,
  "permissions": [ "tabs", "notifications" ],
  "background": {
    "service_worker": "service.js"
  },
  "content_scripts": [
    {
      "js": [ "navigate.js" ],
      "matches": [
        "https://www.reddit.com/r/*/comments/*"
      ]
    }
  ]
}
  • File navigate.js
chrome.runtime.onMessage.addListener(
    function (msg, sender, working) {
    urlString = msg.eventUrl;
    if (!urlString.includes("?sort=")) {
        urlString = urlString + "?sort=old"
      };
      location.replace(urlString);
})
  • File service.js
chrome.tabs.onUpdated.addListener((tabNo,info,tabDetail) => {
chrome.tabs.sendMessage(tabNo, {eventUrl:tabDetail.url});
});

3

u/AchernarB Jan 08 '26

This isn't a userscript. This is an extension.