Focus page instead of address bar on new tab

I have overrided new tab in my extension and want to listen to keystroke events. But when new tab is open, focus is always on address bar and event listeners hears nothing. Due to this user has to click anywhere on page before keystroke handlers be able to recognize anything. Not so comfortable.

Is there ways to set focus to page automatically? window.focus() and document.body.focus() in load handler didn’t work.

It appears one way of achieving this is to simply immediately close the newtab page and re-open the custom landing page: https://github.com/cadeyrn/newtaboverride/blob/cfacb44040a6c594b1345decf6706122cd48f6be/src/js/core/newtab.js#L94

I need to think about it for my own situation. But actually I want to make option to focus page or address bar. Your answer can be useful but what about contrary - to set focus to address bar?

Just don’t do anything and let the focus rest in the address bar?

If new tab is opened with browser.tabs.create(), focus is on the page, so it goes. In my ext both scenarios are possible.

Also be aware that in Firefox 61 (release date tomorrow) if you set new tab page as homepage then opening new window will focus the page, not the addressbar.


I agree there should be a way for extensions to control focus even if it would require new permission - I’m not sure if there is a bug for that already…

Thanks, I’ll keep it on mind. Is there anywhere info about dev API updates? I would like to know when some experimental features be released.

Yes there is:
https://blog.mozilla.org/addons/

Specifically the articles that starts with “Extensions in XX”, for example:
https://blog.mozilla.org/addons/2018/05/17/extensions-in-firefox-61/
https://blog.mozilla.org/addons/2018/04/02/extensions-firefox-60/
https://blog.mozilla.org/addons/2018/01/26/extensions-firefox-59/

They are being released shortly after new Beta version is released so there should be new article about new features in upcoming Beta 62 soon.

EDIT:
New article for 62:
https://blog.mozilla.org/addons/2018/07/05/extensions-in-firefox-62/

1 Like