Repository navigation
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR primarily aims to improve Firefox-family browser compatibility for keyboard shortcuts when the YouTube player is not focused, by adjusting how key events are captured and filtered. It also includes additional changes affecting Shorts autoplay/loop behavior and navigation-time initialization behavior.
Changes:
- Adjust shortcut listener initialization to also bind keyboard events at
documentlevel, and update keydown filtering logic to preferevent.target. - Modify Shorts behavior by disabling Shorts looping under broader conditions and auto-advancing Shorts on
endedbased onup_next_autoplay. - Add a Firefox manual test guide, plus a unit test file covering Shorts autoplay/loop logic.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/shorts-autoplay.test.js | Adds unit coverage for Shorts loop removal and Shorts “next” click-on-ended behavior. |
| TEST_FIREFOX_SHORTCUTS.md | Adds a manual test guide for reproducing/verifying the Firefox shortcuts focus issue. |
| js&css/web-accessible/www.youtube.com/shortcuts.js | Updates keyboard listener wiring and typing/ignore-element detection for shortcut handling. |
| js&css/web-accessible/init.js | Changes Shorts autoloop gating and comments out YouTubeExperiments() on SPA navigation finish. |
| js&css/web-accessible/functions.js | Expands Shorts loop prevention and adds Shorts auto-advance logic on playback end. |
Comments suppressed due to low confidence (3)
js&css/web-accessible/www.youtube.com/shortcuts.js:86
- The teardown mirrors the double-registration problem: keydown/keyup were installed on both window and document, so they must be removed consistently. If you switch to document-only keyboard listeners (recommended to avoid duplicate handler runs), update removal accordingly.
if (listeners[name]) {
delete listeners[name];
window.removeEventListener(name, handler, {passive: false, capture: true});
// Firefox compatibility: also remove from document
if (name === 'keydown' || name === 'keyup') {
document.removeEventListener(name, handler, {passive: false, capture: true});
}
js&css/web-accessible/init.js:284
- Same issue as earlier in this file:
up_next_autoplay !== falsetreats an unset value as enabled, changing default Shorts behavior during SPA navigations. Prefer checking for explicit opt-in (=== true) or a dedicated Shorts setting.
if (ImprovedTube.elements.shorts_player) {
ImprovedTube.redirectShortsToWatch();
if (ImprovedTube.storage.prevent_shorts_autoloop || ImprovedTube.storage.up_next_autoplay !== false) {
ImprovedTube.stop_shorts_autoloop();
}
js&css/web-accessible/functions.js:604
up_next_autoplay !== falsemakes Shorts auto-advance the default even when the user never configured the setting (because undefined !== false). Elsewhere (e.g. upNextAutoplay) this setting is only applied when explicitly set. Consider requiring=== truehere (or a dedicated Shorts setting) to avoid changing default behavior.
if (document.documentElement.dataset.pageType === 'shorts' && ImprovedTube.storage.up_next_autoplay !== false) {
const nextButton = document.querySelector('#navigation-button-down button') ||
document.querySelector('button[aria-label="Next video"]');
if (nextButton) {
nextButton.click();
}
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+47
to
+53
| if (!listeners[name]) { | ||
| listeners[name] = true; | ||
| window.addEventListener(name, handler, {passive: false, capture: true}); | ||
| // Firefox compatibility: also listen on document for keyboard events | ||
| if (name === 'keydown' || name === 'keyup') { | ||
| document.addEventListener(name, handler, {passive: false, capture: true}); | ||
| } |
Comment on lines
+126
to
+129
| // no shortcuts over 'ignoreElements' - check event target first (more reliable) | ||
| if (ImprovedTube.input.ignoreElements.includes(event.target.tagName) || event.target.isContentEditable) return; | ||
| // fallback check for activeElement (for nested elements) | ||
| if (document.activeElement && ImprovedTube.input.ignoreElements.includes(document.activeElement.tagName) && document.activeElement.isContentEditable) return; |
Comment on lines
+124
to
+129
| keydown: function (event) { | ||
| ImprovedTube.user_interacted = true; | ||
| // no shortcuts over 'ignoreElements' | ||
| if ((document.activeElement && ImprovedTube.input.ignoreElements.includes(document.activeElement.tagName)) || event.target.isContentEditable) return; | ||
| // no shortcuts over 'ignoreElements' - check event target first (more reliable) | ||
| if (ImprovedTube.input.ignoreElements.includes(event.target.tagName) || event.target.isContentEditable) return; | ||
| // fallback check for activeElement (for nested elements) | ||
| if (document.activeElement && ImprovedTube.input.ignoreElements.includes(document.activeElement.tagName) && document.activeElement.isContentEditable) return; |
Comment on lines
252
to
255
| ImprovedTube.pageType(); | ||
| ImprovedTube.YouTubeExperiments(); | ||
| // ImprovedTube.YouTubeExperiments(); | ||
| ImprovedTube.commentsSidebar(); | ||
| ImprovedTube.categoryRefreshButton(); |
Comment on lines
207
to
210
| if (ImprovedTube.elements.shorts_player) { | ||
| if (ImprovedTube.storage.prevent_shorts_autoloop) { | ||
| if (ImprovedTube.storage.prevent_shorts_autoloop || ImprovedTube.storage.up_next_autoplay !== false) { | ||
| ImprovedTube.stop_shorts_autoloop(); | ||
| } |
Comment on lines
+457
to
+461
| if (document.documentElement.dataset.pageType === 'shorts') { | ||
| if (ImprovedTube.storage.prevent_shorts_autoloop || ImprovedTube.storage.up_next_autoplay !== false) { | ||
| ImprovedTube.stop_shorts_autoloop(this); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Keyboard shortcuts not working when player isn't focused (Firefox)
The Problem
yo so, so keyboard shortcuts weren't working on Firefox-based browsers (Firefox, Floorp, Librewolf, etc.) when the video player wasn't in focus. For example, if you set Up/Down arrow keys to control volume, they would just scroll the page instead of changing the volume - unless you clicked on the player first.
it kinda worked fine in Chrome, but was broken in Firefox. Pretty annoying when you're reading comments and want to quickly adjust volume!
What Was Wrong
The shortcut handler was checking
document.activeElementto see if the user was typing in an input field. This check was too strict in Firefox - it would incorrectly think the player needed focus for shortcuts to work, when really we just need to make sure the user isn't typing in a text field.The Fix
Two changes to make it work properly:
Better event target checking - Now we check
event.targetfirst (the actual element that received the keypress) instead of relying ondocument.activeElement. This is more reliable across all browsers, especially Firefox.Added document-level listeners - Added keyboard event listeners to
documentin addition towindowfor better Firefox compatibility. This ensures we catch all keyboard events, even when YouTube's complex DOM structure is involved.Testing
Tested on:
To test:
The fix still correctly prevents shortcuts from working when you're actually typing in input fields or the search box.
Files Changed
js&css/web-accessible/www.youtube.com/shortcuts.js- Updated event handler and listener initializationFixes ##3639