How to steal focus from the omnibox in a Chrome extension on the new tab page?

后端 未结 1 375
醉酒成梦
醉酒成梦 2020-12-16 23:13

I\'m making a Chrome extension that involves putting a text input on the new tab page, and immediately giving focus to that input text when the new tab page loads. After bei

相关标签:
1条回答
  • 2020-12-16 23:33

    Hello from my question you linked!

    On your New Tab Page, include the following JavaScript:

    chrome.tabs.create({ url: chrome.extension.getURL("foo.html") });
    window.close();
    

    This will create a "normal" tab, and then close the New Tab Page. You can then focus() your input box on the normal tab.

    This is what I've done for my Fauxbar extension. It adds a slight delay when clicking Chrome's New Tab button (in terms of how long it takes for your input box to become focused), but I reckon it's better than having to press Tab.

    You could also implement chrome.commands to create keyboard shortcuts that users can modify themselves under chrome://extensions > Keyboard shortcuts.

    0 讨论(0)
提交回复
热议问题