我创建了一个Chrome扩展程序,该扩展程序将重新添加主要的键盘功能(至少我曾经使用过)。如果搜索框未对准焦点,则按任意键将自动使其聚焦。此外,箭头键和Tab / Shift + Tab键可让您在结果之间导航。希望这可以帮助我们保持生产力,直到Google(希望)重新添加该功能。
https://chrome.google.com/webstore/detail/google-search-result-keyb/iobmefdldoplhmonnnkchglfdeepnfhd?hl=zh-CN&gl=US
这是扩展名的代码,以备您编辑时使用:
(function() {
'use strict';
var isResultsPage = document.querySelector('html[itemtype="http://schema.org/SearchResultsPage"]');
if (!isResultsPage) {
return;
}
var searchbox = document.querySelector('form[role="search"] input[type="text"]:nth-of-type(1)'),
results = document.querySelectorAll('h3 a'),
KEY_UP = 38,
KEY_DOWN = 40,
KEY_TAB = 9;
function focusResult(offset) {
var focused = document.querySelector('h3 a:focus');
// No result is currently focused. Focus the first one
if (focused == null) {
results[0].focus();
}
else {
for (var i = 0; i < results.length; i++) {
var result = results[i];
if (result === focused) {
var focusIndex = i + offset;
if (focusIndex < 0) focusIndex = 0;
if (focusIndex >= results.length) focusIndex = results.length - 1;
results[focusIndex].focus();
}
}
}
}
window.addEventListener('keydown', function(e) {
e = e || window.event;
var isSearchActive = searchbox === document.activeElement,
keycode = e.keyCode,
// From https://stackoverflow.com/questions/12467240/determine-if-javascript-e-keycode-is-a-printable-non-control-character
isPrintable = (keycode > 47 && keycode < 58) || // number keys
(keycode > 64 && keycode < 91) || // letter keys
(keycode > 95 && keycode < 112) || // numpad keys
(keycode > 185 && keycode < 193) || // ;=,-./` (in order)
(keycode > 218 && keycode < 223); // [\]' (in order)
if ((!isSearchActive && e.keyCode == KEY_DOWN) || (e.keyCode == KEY_TAB && !e.shiftKey)) {
e.preventDefault();
e.stopPropagation();
focusResult(1); // Focus next
}
else if ((!isSearchActive && e.keyCode == KEY_UP) || (e.keyCode == KEY_TAB && e.shiftKey)) {
e.preventDefault();
e.stopPropagation();
focusResult(-1); // Focus previous
}
else if (!isSearchActive && isPrintable) {
// Otherwise, force caret to end of text and focus the search box
searchbox.value = searchbox.value + " ";
searchbox.focus();
}
});
})();