Skip to content

Fix chapters shortcut not closing the chapters panel - #4469

Open
MeeTrannn wants to merge 2 commits into
code-charity:masterfrom
MeeTrannn:fix/4283-chapters-shortcut-toggle
Open

MeeTrannn wants to merge 2 commits into
code-charity:masterfrom
MeeTrannn:fix/4283-chapters-shortcut-toggle

Conversation

@MeeTrannn

Copy link
Copy Markdown

Fixes #4283

What this changes and why

The "Chapters (Sidebar) On/Off" shortcut could open the chapters panel but never close it, in normal view and in full screen. After the first press, YouTube keeps a hidden copy of the chapters panel next to the open one in ytd-watch-flexy #panels. ImprovedTube.shortcutChapters chose "open" whenever any hidden chapters panel existed, so every press after the first re-opened instead of closing.

This PR changes only ImprovedTube.shortcutChapters in js&css/web-accessible/www.youtube.com/shortcuts.js:

  • It decides by whether a chapters panel is expanded: if one is, it clicks that panel's own #visibility-button button to close it; otherwise it opens the panel as before, through [modern-chapters] #navigation-button, keeping the old fallback and console message.
  • It returns early on pages with no chapters panel. Before, line 486 read .clientHeight of null there and threw a TypeError.
  • It removes the ReferenceError: modernChapters is not defined that every press logged. Line 494 read two consts outside the blocks they were declared in.

It adds tests/unit/shortcut-chapters-toggle.test.js, which loads the real shortcuts.js the way shortcuts-stuck-key-recovery.test.js does, with a stub page that keeps a hidden copy of the panel after opening.

Not changed: the key binding, the menu entry and translations, the keyboard handler, the chapters code in appearance.js, and shortcutTranscript, which has the same shape and might need the same fix in a separate PR.

One note on the issue as reported: on today's YouTube in Chromium, the first press already opens chapters in full screen, so I couldn't reproduce "does nothing in full screen". I tested Chromium only; if it still happens in another browser, that may need a follow-up.

Before and after

Chromium 141.0.7390.37, extension loaded unpacked, shortcut bound to H, https://www.youtube.com/watch?v=rfscVS0vtbw. H twice in normal view, then full screen, then H twice. Each line shows every [target-id*=chapters] panel's visibility.

Before (master, 737bbfe6):

B normal, after H:          ["EXPANDED h=465","HIDDEN h=0"]
C normal, after 2nd H:      ["EXPANDED h=465","HIDDEN h=0"]
E1 fullscreen, after H:     ["EXPANDED h=766","HIDDEN h=0"]
E2 fullscreen, after 2nd H: ["EXPANDED h=766","HIDDEN h=0"]
errors: ["modernChapters is not defined" x4]

After (this branch):

B normal, after H:          ["EXPANDED h=465","HIDDEN h=0"]
C normal, after 2nd H:      ["HIDDEN h=0","HIDDEN h=0"]
E1 fullscreen, after H:     ["EXPANDED h=766","HIDDEN h=0"]
E2 fullscreen, after 2nd H: ["HIDDEN h=0","HIDDEN h=0"]
errors: []

New test, against master's shortcuts.js and then on this branch:

master:  Tests: 4 failed, 4 total
branch:  Tests: 4 passed, 4 total

Checks

$ npm ci --force
added 364 packages, and audited 365 packages in 6s

$ npx jest
Test Suites: 38 passed, 38 total
Tests:       233 passed, 233 total

$ npm test
Test Suites: 38 passed, 38 total
Tests:       233 passed, 233 total

ESLint (npx eslint --config=tests/eslint_rules.config.mjs) fails on master before linting anything, because it can't parse the config file, and CI runs it with continue-on-error. I didn't touch it here.

AI use

I used Claude (Claude Code) to read the shortcut code with me, write the Playwright script that drove the browser reproduction, draft the fix and the test, and draft this description. I ran every step myself, checked the test fails on master and passes here, and can explain each changed line.

MeeTrannn and others added 2 commits November 9, 2026 11:50
shortcutChapters() opened the panel whenever any hidden chapters panel
existed. YouTube keeps a hidden copy next to the open panel, so a second
press re-opened instead of closing, in normal view and in full screen.
Decide by whether a chapters panel is expanded, and close it with that
panel's own visibility button.

Also stop the two errors the function threw: line 494 read two
block-scoped consts outside their blocks (ReferenceError on every
press), and pages with no chapters panel read .clientHeight of null.

Refs code-charity#4283

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Loads the real shortcuts.js with a stub page that keeps a hidden copy
of the chapters panel after opening, as YouTube does, and checks that
a second press closes the panel and that no press throws.

Refs code-charity#4283

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MeeTrannn
MeeTrannn force-pushed the fix/4283-chapters-shortcut-toggle branch from 3393afc to 7478d36 Compare November 9, 2026 19:51
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.

🐞"Chapters (Sidebar) On/Off" not working in full screen

1 participant