HjklNavigation

Shortcuts for Google Search result. j/k to move focus, l/h to open in new/background tab.

当前为 2021-03-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         HjklNavigation
// @namespace    com.gmail.fujifruity.greasemonkey
// @version      1.4
// @description  Shortcuts for Google Search result. j/k to move focus, l/h to open in new/background tab.
// @author       fujifruity
// @include      https://www.google.com/search*
// @grant        GM.openInTab
// @license      MIT
// ==/UserScript==

{
    const googleUrl = "www.google.com"
    // Add another URL variable and @include above.
    // This script may fits most list-like elements.

    let focusIdx = null

    const results = (() => {
        switch (location.hostname) {
            case googleUrl: return [...document.querySelectorAll('#rso .g')].filter(e => e.className == 'g')
            // add another case here
            default: return null
        }
    })()

    const open = (result, isBackground) => {
        switch (location.hostname) {
            case googleUrl: {
                const url = result.getElementsByTagName('a')[0].href
                GM.openInTab(url, isBackground)
                break
            }
            // add another case here
        }
    }

    const refocus = nextIdx => {
        if (focusIdx == null) {
            focusIdx = 0
        } else {
            results[focusIdx].style.backgroundColor = null
            focusIdx = nextIdx
        }
        results[focusIdx].style.backgroundColor = 'lightyellow'
        results[focusIdx].scrollIntoView({ behavior: "smooth", block: "center" })
    }

    window.addEventListener('keydown', event => {
        if (event.target.tagName == "INPUT" || event.ctrlKey || event.altKey) return
        const result = results[focusIdx]
        switch (event.key) {
            case 'j': {
                refocus((focusIdx + 1) % results.length)
                break
            }
            case 'k': {
                refocus((focusIdx - 1 + results.length) % results.length)
                break
            }
            case 'l': {
                open(result, false)
                break
            }
            case 'h': {
                open(result, true)
                break
            }
            case 'g': {
                refocus(0)
                break
            }
            case 'G': {
                refocus(results.length - 1)
                break
            }
        }
    })

}