Skip to content

Refactor: split 303-line buildStylesheet() into named sub-functions#105

Open
w6a8w8 wants to merge 12 commits intoZren:masterfrom
w6a8w8:buildStylesheet
Open

Refactor: split 303-line buildStylesheet() into named sub-functions#105
w6a8w8 wants to merge 12 commits intoZren:masterfrom
w6a8w8:buildStylesheet

Conversation

@w6a8w8
Copy link
Copy Markdown

@w6a8w8 w6a8w8 commented Mar 21, 2026

Problem

buildStylesheet() was a single 303-line function containing rules for every visual concern in the script—scrollbars, player sizing, sidebar, masthead, guide, miniplayer, playlist bar, and Material/Flexy UI. There was no way to locate, read, or modify one concern without scrolling past all the others.

Solution

Extracted 8 named sub-functions as sibling properties on ytwp.event:

Sub-function Purpose
_styleScrollbar() Webkit + Firefox scrollbar hiding
_stylePlayer() Sizing, positioning, overlays, cropping
_styleSidebar() Sidebar transition and padding
_styleMasthead() Masthead, guide, autocomplete popup
_styleMiniplayer() Miniplayer fixed dimensions
_styleMisc() Scrollbar hiding, placeholders, whitespace
_stylePlaylistBar() Playlist bar dimensions and media queries
_styleMaterialUI() Material UI and flexy UI rules

buildStylesheet() now delegates to all eight in order and remains the single call site, so the SPF duplication path in initStyle() is unchanged.

What did not change

Zero CSS rules were added, removed, or reordered. The appendRule and appendRaw call count is identical (64) in both versions. Each sub-function that used the shared d variable now declares its own local d—there was no shared state between sections.

w6a8w8 added 12 commits March 21, 2026 12:35
Removed version 140 entry from changelog.
Removed HistoryEvent class (unused; native yt-navigate-finish handles SPA routing).

Removed ytwp.html5PlayerFix (unreachable code block due to early return).

Removed window 'keypress' listener (deprecated API; 'keydown' handles the logic).
Added early return inside the else block to accurately report when the DOM has changed.

Prevents the loop from cascading fake errors against a stuck parent node.

Enhanced the error log to output the actual mismatched parent element.
Replaced 60fps rAF polling with targeted, debounced MutationObservers to reduce redundant DOM queries.

Added lifecycle management to disconnect existing observers before re-attaching, preventing memory leaks.

Deferred observer attachment until ytwp.initialized is true to fix silent failures on homepage cold starts.

Slowed the fallback setInterval safety net from 2500ms to 5000ms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant