r/Title Fix for Reddit

Always show the subreddit (r/Name) at the start of the Reddit tab title and keep it updated on SPA navigation.

התקן את הסקריפט?
סקריפטים מומלצים של יוצר זה

אולי תאהב גם את Subreddit tab icons.

התקן את הסקריפט
// ==UserScript==
// @name         r/Title Fix for Reddit
// @description  Always show the subreddit (r/Name) at the start of the Reddit tab title and keep it updated on SPA navigation.
// @namespace https://greasyfork.org/en/scripts/540810-r-title-fix-for-reddit
// @version      1
// @license      MIT
// @author       TheSina
// @match        https://*.reddit.com/*
// @exclude      https://*.reddit.com/account/*
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  const SUBREDDIT_RX = /^\/r\/([^\/]+)/;
  const PREFIX_RX    = /^r\/[^\s]+\s*-\s*/;

  function getSubreddit() {
    const m = SUBREDDIT_RX.exec(location.pathname);
    return m ? m[1] : null;
  }

  function updateTitle() {
    const sr = getSubreddit();
    if (!sr) return;

    const display = 'r/' + sr.charAt(0).toUpperCase() + sr.slice(1);
    const suffix  = document.title.replace(PREFIX_RX, '');
    const newT    = `${display} - ${suffix}`;

    if (document.title !== newT) {
      document.title = newT;
    }
  }

  let timer;
  function debouncedUpdate() {
    clearTimeout(timer);
    timer = setTimeout(updateTitle, 100);
  }

  // Patch both pushState and replaceState
  ['pushState','replaceState'].forEach(fn => {
    const orig = history[fn];
    history[fn] = function() {
      orig.apply(this, arguments);
      debouncedUpdate();
    };
  });

  // Back/forward buttons
  window.addEventListener('popstate', debouncedUpdate);

  // Watch for programmatic title changes
  const titleEl = document.querySelector('title');
  if (titleEl) {
    new MutationObserver(debouncedUpdate)
      .observe(titleEl, { childList: true });
  }

  // Initial run
  updateTitle();
})();