From 66f6ee228ed02ad6c1b13a0f7e4e3059d8b6afa4 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 7 Apr 2021 19:07:42 -0400 Subject: Fix autofocus sometimes not working on the search page (#1597) --- ext/js/display/search-main.js | 2 +- ext/js/dom/document-focus-controller.js | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ext/js/display/search-main.js b/ext/js/display/search-main.js index 5ad50500..056d4788 100644 --- a/ext/js/display/search-main.js +++ b/ext/js/display/search-main.js @@ -26,7 +26,7 @@ (async () => { try { - const documentFocusController = new DocumentFocusController(); + const documentFocusController = new DocumentFocusController('#search-textbox'); documentFocusController.prepare(); await yomichan.prepare(); diff --git a/ext/js/dom/document-focus-controller.js b/ext/js/dom/document-focus-controller.js index 649b5abe..b2d40db8 100644 --- a/ext/js/dom/document-focus-controller.js +++ b/ext/js/dom/document-focus-controller.js @@ -22,13 +22,17 @@ * focus a dummy element inside the main content, which gives keyboard scroll focus to that element. */ class DocumentFocusController { - constructor() { + constructor(autofocusElementSelector=null) { + this._autofocusElement = (autofocusElementSelector !== null ? document.querySelector(autofocusElementSelector) : null); this._contentScrollFocusElement = document.querySelector('#content-scroll-focus'); } prepare() { window.addEventListener('focus', this._onWindowFocus.bind(this), false); this._updateFocusedElement(false); + if (this._autofocusElement !== null && document.activeElement !== this._autofocusElement) { + this._autofocusElement.focus({preventScroll: true}); + } } blurElement(element) { -- cgit v1.2.3