diff --git a/devtools/client/framework/toolbox.js b/devtools/client/framework/toolbox.js index e2414a6a674c..37c1e0f97d82 100644 --- a/devtools/client/framework/toolbox.js +++ b/devtools/client/framework/toolbox.js @@ -763,6 +763,15 @@ Toolbox.prototype = { await promise.all([splitConsolePromise, framesPromise]); + // Request the actor to restore the focus to the content page once the + // target is detached. This typically happens when the console closes. + // We restore the focus as it may have been stolen by the console input. + await this.target.reconfigure({ + options: { + restoreFocus: true, + }, + }); + // Lazily connect to the profiler here and don't wait for it to complete, // used to intercept console.profile calls before the performance tools are open. const performanceFrontConnection = this.initPerformance(); diff --git a/devtools/client/webconsole/test/mochitest/browser_webconsole_document_focus.js b/devtools/client/webconsole/test/mochitest/browser_webconsole_document_focus.js index 7a2d0a607897..5c9a1ef690bd 100644 --- a/devtools/client/webconsole/test/mochitest/browser_webconsole_document_focus.js +++ b/devtools/client/webconsole/test/mochitest/browser_webconsole_document_focus.js @@ -13,12 +13,20 @@ add_task(async function() { info("Focus after console is opened"); ok(isInputFocused(hud), "input node is focused after console is opened"); + await ContentTask.spawn(gBrowser.selectedBrowser, {}, function() { + this.onFocus = new Promise(resolve => { + content.addEventListener("focus", resolve, { once: true }); + }); + }); + info("Closing console"); await closeConsole(); + const isFocused = await ContentTask.spawn( gBrowser.selectedBrowser, {}, - function() { + async function() { + await this.onFocus; return Services.focus.focusedWindow == content; } ); diff --git a/devtools/client/webconsole/webconsole.js b/devtools/client/webconsole/webconsole.js index 4910d9310c2c..6612bfd67f13 100644 --- a/devtools/client/webconsole/webconsole.js +++ b/devtools/client/webconsole/webconsole.js @@ -386,14 +386,6 @@ class WebConsole { this.ui.destroy(); } - if (!this.isBrowserConsole) { - try { - await this.target.focus(); - } catch (ex) { - // Tab focus can fail if the tab or target is closed. - } - } - if (this._parserService) { this._parserService.stop(); this._parserService = null; diff --git a/devtools/server/actors/targets/browsing-context.js b/devtools/server/actors/targets/browsing-context.js index efa9a943c771..783af6e20037 100644 --- a/devtools/server/actors/targets/browsing-context.js +++ b/devtools/server/actors/targets/browsing-context.js @@ -1135,6 +1135,9 @@ const browsingContextTargetPrototype = { options.serviceWorkersTestingEnabled ); } + if (typeof options.restoreFocus == "boolean") { + this._restoreFocus = options.restoreFocus; + } // Reload if: // - there's an explicit `performReload` flag and it's true @@ -1157,6 +1160,9 @@ const browsingContextTargetPrototype = { this._setCacheDisabled(false); this._setServiceWorkersTestingEnabled(false); this._setPaintFlashingEnabled(false); + if (this._restoreFocus) { + this.window.focus(); + } }, /**