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?
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).
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With