Scroll to Top Button

Adds a customizable scroll-to-top button near the page bottom.

Tekijä
DXRK1E
Päivittäiset asennukset
0
Asennuskerrat
72
Arvostelut
2 0 0
Versio
3.1.0
Luotu
24.7.2024
Päivitetty
13.4.2025
Size
5,7 kt
Lisenssi
MIT
Käytössä
Kaikki sivustot

Scroll to Top Button UserScript: What's the Deal?

Hey! So you've got this JavaScript UserScript called "Scroll to Top Button". Let's dive into what it does, how it ticks, and whether you should trust it on your browser.

What is it for? Simple:

Ever been deep down a super long webpage (think infinite scroll feeds, massive articles, long forum threads) and just wished for a quick "zap me back to the top" button? That's exactly what this script does. It adds a little button, usually chilling near the bottom-right corner of your screen, that only shows up when you've scrolled down a bit. Click it, and whoosh, you're back at the top.

How Does it Work (The Gist)?

  1. It Waits: The script waits for the webpage to be ready (DOMContentLoaded). No point trying to add a button if there's nowhere to put it yet, right?
  2. Injects Style: It creates some CSS rules on the fly (_injS function) to define how the button looks (size, color, position, roundness, shadow, etc.) and how it animates (fade in/out, grow/shrink on hover). It gives these styles a unique ID (_sid) to avoid messing with the site's own styles.
  3. Builds the Button: It creates the actual button element (_crB function) using JavaScript, gives it a unique ID (_bid), sets its text label (for accessibility), adds a neat SVG up-arrow icon, and most importantly, tells it what to do when clicked (_scT function).
  4. Appends to Page: It sticks this newly created button onto the current webpage's <body>.
  5. Listens for Scrolls: The script keeps an ear out for when you scroll the page (window.addEventListener('scroll', ...)).
  6. Smart Showing/Hiding: It doesn't constantly check your scroll position (that'd be inefficient!). It uses a technique called "debouncing" (_deb function). This means it waits until you've stopped scrolling for a tiny moment (like 150 milliseconds, configurable!) before checking if you've scrolled far enough down (_cfg.bh.shThrPx, default 300 pixels) to show the button (_hSE function). If you scroll back up near the top, it hides the button again. It also re-checks if the page layout changes (MutationObserver) or if you resize the window.
  7. Smooth Scrolling Magic: When you click the button (_scT function):
    • It checks your settings (_cfg.bh.smScr).
    • If smooth scroll is off, it just jumps straight to the top (behavior: 'auto').
    • If smooth scroll is on, it first checks if you want to use the browser's native smooth scroll (_cfg.bh.natSmScr and checks browser support). If yes, it uses behavior: 'smooth'.
    • If native smooth scroll isn't preferred or available, it runs its own smooth scroll animation (_smS function). This uses requestAnimationFrame (which is efficient for animations) and fancy math functions ("easing functions" like easeInOutCubic stored in _eas) to make the scroll look nice and smooth over a set duration (_cfg.sc.durMs, default 800ms).
  8. Configurable Goodness: Almost everything about the button (look, feel, position, when it appears, scroll speed, scroll smoothness type) can be tweaked by changing the values in the _cfg object right at the top of the script.

Is it Safe to Use?

Yes, generally very safe. Here's why:

  1. @grant none: This is the most important line in the UserScript header for safety. It means the script tells your UserScript manager "Hey, I don't need any special permissions or access to browser APIs that could be risky." It runs in the standard web page environment.
  2. No External Requests: The script doesn't load external resources (except the icon from imgur, which is just an image) or send your data anywhere.
  3. DOM Manipulation Only: All it really does is add a button and some styles to the page, listen to scroll/resize events, and tell the browser window to scroll. It's not reading your passwords or doing anything sneaky.
  4. Scoped: It's wrapped in an IIFE (function () { ... })(); which helps prevent its variables and functions from accidentally interfering with the website's own JavaScript.
  5. @noframes: This prevents it from running inside embedded frames (iframes) on a page, which can sometimes be a vector for tricky behavior, though it's mostly just to ensure it behaves predictably on the main page.

In short: It's a well-behaved script focused solely on adding a visual element and interacting with standard browser scrolling. It's about as safe as a UserScript gets.

Who is this for?

Anyone who browses the web and gets tired of manually scrolling back to the top of long pages. It's a simple quality-of-life improvement. If you like tweaking things, the configuration options are a nice bonus!