5

Is it possible to highlight the URL bar (set focus to the location bar) using Google Chrome extensions or Safari extensions?

Using Firefox extensions, one can highlight the URL bar like this:

var addressBar=document.getElementById('urlbar');
addressBar.focus();
addressBar.select();

I'm not sure if there's similar functionality in Chrome and Safari.

Thanks in advance for your help!

Kevin
  • 682
  • 1
  • 9
  • 18

1 Answers1

4

Firefox, in its plugins and extensions gives access to browser's "chrome" (the UI stuff around the page) which makes this possible, although I am not sure of the api that you have mentioned.

Google Chrome, however, is clear about its exposure to browser's chrome to the extension and gives access only through browser actions and page actions. Moreover you can use HTML5 Desktop Notifications, Context Menus and Omnibox to get more out of the browser.

But I am afraid you cannot access the address bar from extensions (or otherwise) which Chrome explicitly denies access to via javascript or Extensions.

At most what you can do is to point it out to user that pressing Ctrl+L will get them to browser's address bar or if you want to redirect the user you can use window.location.href = 'https://stackoverflow.com'.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Juzer Ali
  • 4,109
  • 3
  • 35
  • 62
  • Thank you for the reply! It's too bad that Google Chrome restricts the ability to set focus to the URL bar. Do you have any idea about Safari? I can't find anything in their [documentation](http://developer.apple.com/library/safari/#documentation/Tools/Conceptual/SafariExtensionGuide/Introduction/Introduction.html). – Kevin May 08 '12 at 20:29
  • Ditto Safari. It doesn't let extensions mess with the address bar. – chulster May 09 '12 at 01:02
  • @JuzerAli, Why does Google limit access to only browser actions and page actions? Is there some evil plan going on? – Pacerier Oct 13 '17 at 10:51
  • @Pacerier safety. Sandoxing protect users from malicious developers/extensions from, for example, taking over the address bar and redirect you to scam website. – jalone Jul 21 '20 at 10:52