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

时光毁灭记忆、已成空白 提交于 2019-11-29 05:06:44

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.

I think this way is better than @Chris 's way.

document.location.href = url;

Update: I tested it and succeed on my chrome. Although this way is slow, too.But it's faster than @Chris 's way.

This is manifest.json

{
    "name" : "my extension",
    "description" : "my extension",
    "version" : "0.1",
    "manifest_version" : 2,
    "chrome_url_overrides" : {
        "newtab" : "html/index.html#newTab"
    },
    "permissions" : [
        "tabs",
        "<all_urls>",
        "history",
        "bookmarks",
        "chrome://favicon/*",
        "unlimitedStorage",
        "management",
        "clipboardWrite",
        "clipboardRead",
        "contextMenus",
        "notifications",
        "storage"
    ],
    "browser_action" : {
        "default_icon" : "icon.png",
        "default_popup" : "html/popup.html",
        "default_title": "Click here!"
    },
    "options_page": "html/options.html"
}

This is index.html

<!DOCTYPE html>
<html>
  <head>        
    <script type="text/javascript" src="/js/newTabCheck.js"></script>
    <title></title>
  </head>
  <body></body>
</html>

This is newTabCheck.js

if (window.location.hash == '#newTab') {
    window.document.title = 'Loading...';
    chrome.storage.sync.get({
        file_url : 'http://www.google.com'
    }, function (items) {
        document.location.href = items.file_url;
    });
}

This is options.html

<html>
  <head>
    <title>My Test Extension Options</title>
  </head>
  <body>file path:
  <form>
  <input type="text" id="file_url" />
  <br />
  <button id="save">save</button> 
  <label id="status"></label> 
  <script type="text/javascript" src="/js/options.js"></script></form>
  </body>
</html>

This is options.js

// Saves options to chrome.storage
function save_options() {
    var file_url = document.getElementById("file_url").value;
    chrome.storage.sync.set({
        file_url : file_url
    }, function () {
        // Update status to let user know options were saved.
        var status = document.getElementById('status');
        status.textContent = 'save success!';
        setTimeout(function () {
            status.textContent = '';
        }, 750);
    });
}

// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
    chrome.storage.sync.get({
        file_url : 'http://www.google.com'
    }, function (items) {
        document.getElementById("file_url").value = items.file_url;
    });
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);

Just set the file_url as focus.html

<html>
<head><title></title></head>     
<body>

save path:
<input type="text" id="file_url"></input>

<script type="text/javascript">
    document.getElementById('file_url').focus();
</script>
</body>
</html>

That's all.

The reason of write this extension is for the extention Vimium. Then when i type "t" to open a newtab,I can use the vimiun without mouse.For this reason, you don't have to write javascript code document.getElementById('file_url').focus();.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!