Reddit - Back To Top

Adds a small bottom corner button on (New) Reddit to go back to the top.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Reddit - Back To Top
// @version      1.0
// @description  Adds a small bottom corner button on (New) Reddit to go back to the top.
// @author       Mane
// @license      CCO-1.0
// @match        https://www.reddit.com/*
// @grant        none
// @run-at       document-end
// @namespace https://greasyfork.org/users/1491313
// ==/UserScript==

(function() {
  'use strict';

  const BTN_ID = 'scrollToTopBtn';

  function addButton() {
    if (document.getElementById(BTN_ID)) return;

    var btn = document.createElement('div');
    btn.id = BTN_ID;
    btn.title = 'Scroll to Top';
    btn.innerHTML = '⬆';

    // Basic styling
    btn.style.position        = 'fixed';
    btn.style.bottom          = '20px';
    btn.style.right           = '20px';
    btn.style.width           = '40px';
    btn.style.height          = '40px';
    btn.style.backgroundColor = 'rgba(0,0,0,0.7)';
    btn.style.color           = '#fff';
    btn.style.fontSize        = '22px';
    btn.style.textAlign       = 'center';
    btn.style.lineHeight      = '40px';
    btn.style.borderRadius    = '4px';
    btn.style.cursor          = 'pointer';
    btn.style.zIndex          = '9999';

    // Scroll to top on click
    btn.onclick = function() {
      window.scrollTo(0, 0);
    };

    document.body.appendChild(btn);
  }

  // Initial injection
  addButton();

  // Re-inject after Reddit’s client-side navigation
  var origPush = history.pushState;
  history.pushState = function() {
    origPush.apply(this, arguments);
    addButton();
  };
  window.addEventListener('popstate', addButton);

})();