10

With Chrome 27, it seems that extensions that override Chrome's New Tab Page can't take focus away from Chrome's Omnibox like they used to in previous versions of Chrome.

Is there a new way to focus an input box in a New Tab Page, or has this functionality been disabled completely? :(

To test this, create an extension folder with three files:

1. manifest.json:

{
    "name": "Focus Test",
    "version": "0",
    "minimum_chrome_version": "27",
    "chrome_url_overrides": {
        "newtab": "newTab.html"
    },
    "manifest_version": 2
}

2. focus.js:

document.getElementById('foo').focus();

3. newTab.html:

<html>
    <body>
        <input id="foo" type="text" />
        <script type="text/javascript" src="focus.js"></script>
    </body>
</html>

Then, when you load the extension and open a new tab, the input field does not get focused on the new tab page.

I have also tried adding the autofocus attribute to the input field, but no luck either. The extension's new tab page can't take focus away from Chrome's Omnibox.

Any ideas? Is this a bug or a new "feature"?

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Chris McFarland
  • 6,059
  • 5
  • 43
  • 63
  • 1
    The Chrome documentation claims that "New Tab" pages should not be able to override the address bar/omnibox getting the focus, however whether it is actually possible I am not sure about. http://developer.chrome.com/extensions/override.html#tips – Qantas 94 Heavy May 22 '13 at 07:31
  • There are similar issues reported from other extensions but it looks like the opposite behavior occurs: https://code.google.com/p/chromium/issues/detail?id=232148 – Gearoid Murphy Jun 06 '13 at 19:41
  • I've actually filed an issue with Chromium. Waiting for confirmation to learn if this is a bug or not. https://code.google.com/p/chromium/issues/detail?id=243102 – Chris McFarland Jun 07 '13 at 01:38
  • Chromium team has concluded that this is a feature, not a bug. – Chris McFarland Jun 12 '13 at 19:31

4 Answers4

8

ManifestV3 update

This answer is adapted from https://stackoverflow.com/a/11348302/1754517.
This has been tested with both Manifest V2 and V3.
Tested in Google Chrome 99.0.4844.51 64-bit (Windows 10).

  1. Replace the content of focus.js with:
  if (location.search !== "?x") {
    location.search = "?x";
    throw new Error;  // load everything on the next page;
    // stop execution on this page
  }
  1. Add the autofocus attribute to the <input>.
  2. Go to the Extensions page in Chrome and click the Load unpacked button. Choose the folder of your extension.
  3. Open your new tab page. You might see a modal dialogue reading Change back to Google?. Click Keep it to keep your custom new tab page.
Inline Javascript - Manifest V2 only

If you're inlining the Javascript in the HTML file, then you'll need to take some extra steps:

  1. After adding your inline Javascript to your HTML file, open DevTools (F12 key) and observe the error output in the Console. Example output you should see:
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem:". 
Either the 'unsafe-inline' keyword, a hash ('sha256-MK0Gypb4mkZTI11eCOtWT+mGYcJNpN5zccvhfeaRb6E='), or a nonce ('nonce-...') is required to enable inline execution.
  1. Select & copy this hash.
  2. Add a line to manifest.json to allow the JS to run, pasting in the hash you just copied between the single-quotes. E.g.:
"content_security_policy": "script-src 'self' 'sha256-MK0Gypb4mkZTI11eCOtWT+mGYcJNpN5zccvhfeaRb6E='"
  1. Go to the Extensions page again. Remove the extension, then re-add it using the Load unpacked button.
  2. Open your new tab page. Your extension should now autofocus on the <input>.

Note inlining only works with Manifest V2; Manifest V3 returns a failure message when attempting to load the extension (even with a properly formed "content_security_policy" object in manifest.json, to replace the Manifest V2 "content_security_policy" string):

Failed to load extension
File C:\path\to\extension
Error 'content_security_policy.extension_pages': Insecure CSP value "'sha256-...'" in directive 'script-src'.
Could not load manifest.
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Jimadine
  • 998
  • 13
  • 26
  • Will this work with Manifest V3? Or if not, can you please provide an example for V3? – Chris McFarland Mar 10 '22 at 20:14
  • I have just tested and V3 does work - just need to update `manifest.json` with `"manifest_version": 3,`. I've also realised that steps 5-8 are only relevant if you're inlining your JS, which the OP isn't; I will adjust my answer. Thanks. – Jimadine Mar 11 '22 at 12:02
  • 1
    Thanks very much. Upon further trialling, for a shortened, condensed version to keep the `search` property as small as possible, this seems to do the trick: `let q = "?"; if (location.search !== q && !location.href.includes(q)) location.search = q;` For what it's worth, also tried mucking around with `history.pushState` and changing `location.href`, but no luck. Just setting `location.search` allows the New Tab Page to steal focus with `autocomplete` as an attribute, or calling `.focus()`. – Chris McFarland Mar 12 '22 at 01:25
  • 1
    @ChrisMcFarland This is useful, thanks! – Jimadine Mar 15 '22 at 16:34
4

As per the Chrome Extension Documentation,

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.

See reference here: Override Pages

John Woodruff
  • 1,608
  • 16
  • 29
1

Here's the solution for Manifest v3

chrome.tabs.onCreated.addListener((tab) => {
  if (tab.pendingUrl === 'chrome://newtab/') {
    chrome.tabs.remove(tab.id)
    chrome.tabs.create({
      url: '/index.html',
    })
  }
})

I saw a pretty old blog which updates the new tab conditionally. However, simply updating the tab does not steal the focus. I had to close the pending tab and open a new one.

Cons: An ugly chrome-extension://akfdobdepdedlohhjdalbeadhkbelajj/index.html in the URL bar.

0

I have a cheap work around that allows stealing focus from address bar focus. It's not for everyone. I do actually do use this because I want to control a new tab focus just that bad in my own custom new tab solution:

<script>
  alert('Use enter key to cancel this alert and then I will control your focus');

  document.getElementById('...AckerAppleIsCrafty...').focus()
</script>

USE CASE: I built my own HTML chrome custom tab that has a search input that custom searches my history and bookmarks the way I like it too.

Cash me focusing outside how bout dat?

Acker Apple
  • 395
  • 2
  • 3