Skip to content

firefox bug scroll instead of volume - #3663

Closed
MoriMomo wants to merge 0 commit into
code-charity:masterfrom
MoriMomo:master
Closed

MoriMomo wants to merge 0 commit into
code-charity:masterfrom
MoriMomo:master

Conversation

@MoriMomo

@MoriMomo MoriMomo commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

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.activeElement to 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:

  1. Better event target checking - Now we check event.target first (the actual element that received the keypress) instead of relying on document.activeElement. This is more reliable across all browsers, especially Firefox.

  2. Added document-level listeners - Added keyboard event listeners to document in addition to window for better Firefox compatibility. This ensures we catch all keyboard events, even when YouTube's complex DOM structure is involved.

Testing

Tested on:

  • Firefox 124

To test:

  1. Set any shortcut (like Up/Down for volume)
  2. Play a video
  3. Click anywhere EXCEPT the player (comments, sidebar, etc.)
  4. Press your shortcut key
  5. Should work now! 🎉

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 initialization

Fixes ##3639

@MoriMomo
MoriMomo marked this pull request as ready for review June 28, 2026 17:08
Copilot AI review requested due to automatic review settings July 24, 2026 01:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 document level, and update keydown filtering logic to prefer event.target.
  • Modify Shorts behavior by disabling Shorts looping under broader conditions and auto-advancing Shorts on ended based on up_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 !== false treats 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 !== false makes 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 === true here (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 thread js&css/web-accessible/functions.js Outdated
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);
}
}
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.

2 participants