HjklNavigation

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

31.10.2021 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         HjklNavigation
// @namespace    com.gmail.fujifruity.greasemonkey
// @version      1.5.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 items = [...document.querySelectorAll('#rso .g')].filter(
        e => e.querySelectorAll(':scope > div').length == 1
    )
    const open = inBackground => {
        const url = findCurrentItem().querySelector('a').href
        GM.openInTab(url, inBackground)
    }
    const tag = "hjklNavigationCurrentItem"
    const findCurrentItem = () => items.find(e => e.hasAttribute(tag))

    const moveCursor = step => {
        const currentItem = findCurrentItem()
        const r = currentItem.getBoundingClientRect();
        const isVisible = 0 < r.top && r.top < window.innerHeight || 0 < r.bottom && r.bottom < window.innerHeight
        if (!isVisible) {
            const dist = e => {
                const r = e.getBoundingClientRect()
                return Math.abs(window.innerHeight - (r.top + r.bottom))
            }
            const nearest = items.reduce((acc,e) => dist(acc) < dist(e) ? acc : e )
            select(nearest)
            return
        }
        const nextIdx = (items.indexOf(currentItem) + step + items.length) % items.length
        select(items[nextIdx])
    }
    const highlight = e => {
        e.style.backgroundColor = 'WhiteSmoke'
        e.setAttribute(tag, '')
    }
    const select = item => {
        // deselect current item
        const currentItem = findCurrentItem()
        currentItem?.setAttribute('style', "background-color: null;")
        currentItem?.removeAttribute(tag)
        highlight(item)
        item.scrollIntoView({ behavior: "smooth", block: "center" })
    }
    highlight(items[0])

    window.addEventListener('keydown', event => {
        if (event.target.tagName == "INPUT" || event.ctrlKey || event.altKey) return
        switch (event.key) {
            case 'j': {
                moveCursor(+1)
                break
            }
            case 'k': {
                moveCursor(-1)
                break
            }
            case 'l': {
                open(false)
                break
            }
            case 'h': {
                open(true)
                break
            }
        }
    })

}