Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome new tab autofocus

I am making a Chrome extension and I am using the following:

manifest.json

{
    "name": "Test extension",
    "version": "1.1",
    "description": "Test extension.",
    "icons": {
        "128": "icon_128.png"
    },
    "chrome_url_overrides": {
        "newtab": "cc.html"
    },
    "manifest_version": 2
}

cc.html

<style>body,html{padding:0;margin:0}</style>
<iframe src="theiframe.html" frameborder="0" height="200px" width="200px">
</iframe>

theiframe.html

<style>body,html{padding:0;margin:0}</style>
<form action="http://www.example.com/search">
    <input autofocus="autofocus" tabindex="1" type="text" />
    <input tabindex="2" value="search" type="submit"/>
</form>

When the user opens new tab the autofocus will be in the address bar. I want the users to be able to change that. Is there any code that will automatically autofocus the search input?

like image 865
Enve Avatar asked Feb 01 '13 19:02

Enve


3 Answers

Maybe it can't work using just an iframe. But you can make a sample page which will redirect to the page you want when a new tab is opened, so the input which has autofocus will be automatically focused.

Here is a way to do it:


manifest.json

"chrome_url_overrides": {
    "newtab": "r.html"
},


r.html

<html>
  <head>
    <title>Loading...</title> <!-- user friendly -->
    <noscript>
      <meta http-equiv="refresh" content="0; url=https://www.google.com"> <!-- in case javascript is disabled -->
    </noscript>
    <script src="s.js"></script>
  </head>
  <body></body>
</html>


s.js

window.location="https://www.google.com";


This is the best way to do it (and probably the only way).

like image 69
Enve Avatar answered Sep 27 '22 19:09

Enve


As Erik Kay (the director of Chrome engineering) pointed out in this Chromium bug report, the intended behaviour is for the Omnibox to have focus by default and it is not possible to change this.

As such, the bug was marked as WontFix.

like image 45
Whymarrh Avatar answered Sep 27 '22 20:09

Whymarrh


A tip from the API docs:

Don't rely on the page having the keyboard focus. The address bar always gets the focus first when the user creates a new tab.

like image 24
Chris Wesseling Avatar answered Sep 27 '22 18:09

Chris Wesseling