Skip to content

Refactor: var → const/let throughout the script#106

Open
w6a8w8 wants to merge 13 commits intoZren:masterfrom
w6a8w8:replace_var
Open

Refactor: var → const/let throughout the script#106
w6a8w8 wants to merge 13 commits intoZren:masterfrom
w6a8w8:replace_var

Conversation

@w6a8w8
Copy link
Copy Markdown

@w6a8w8 w6a8w8 commented Mar 21, 2026

Changes

Replaced every var declaration with const or let according to actual reassignment behaviour. No logic was changed.

Rules applied

  • const — value is never reassigned after the initial declaration.
  • let — value is reassigned at least once after declaration.

Classification

Module-level const (17):
playerHeight, enableOnLoad, scriptToggleKey, uw, JSStyleSheet, scriptShortName, scriptStyleId, scriptBodyClassId, viewingVideoClassId, topOfPageClassId, scriptHtmlSelector, videoContainerId, videoContainerPlacemarkerId, transitionProperties, transformProperties, ytwp, INIT_RETRY_MAX

Module-level let (2):
scriptBodySelector, scriptSelector — both are mutated inside the uw.spf block in initStyle() to duplicate the stylesheet for SPF pages.

Function-scoped locals — notable decisions:

  • const d in buildVenderPropertyDict, _styleSidebar, _styleMasthead: Object is mutated via property assignment, but the binding itself is never reassigned. const is correct here.
  • let d in _stylePlayer, _stylePlaylistBar: The binding is reassigned between two buildVenderPropertyDict() calls.
  • const tree in hasYoutubeChanged: Array is mutated via tree.reverse() in-place. The binding is never reassigned, so const is correct. The stale tree = tree.reverse() assignment is also removed since Array.prototype.reverse() mutates and returns the same array; the assignment was redundant.
  • let watchElement in setTheaterMode: Reassigned in the else-if branch via the assignment-in-condition pattern (else if (watchElement = document.querySelector(...))).
  • const temp in initStyle SPF block: Assigned once, never read back. The original value of scriptBodySelector is not restored after the if block. This is a pre-existing quirk, not introduced here; using const makes the write-only nature visible.
  • let debounceTimer in observePlayer: Reassigned on every mutation via clearTimeout/setTimeout.
  • let parent in childOf: Reassigned in the while loop body.

What did not change

The only remaining var in the file is inside a CSS string value:
var(--ytd-masthead-height, var(--ytd-toolbar-height)) — untouched.

w6a8w8 added 13 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