Greasy Fork 还支持 简体中文。

Better search results in youtube and google

Ctrl+ArrowUp and Ctrl+ArrowDown to focus prev/next result

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Better search results in youtube and google
// @namespace    https://www.twitch.tv/simplevar
// @version      2025-05-15
// @description  Ctrl+ArrowUp and Ctrl+ArrowDown to focus prev/next result
// @author       SimpleVar
// @match        https://www.youtube.com/*
// @match        https://www.google.com/search?*
// @icon         https://icons.iconarchive.com/icons/icons8/windows-8/256/Very-Basic-Search-icon.png
// @grant        none
// @run-at       document-end
// @license      The Unlicense
// ==/UserScript==

(() => {
    const ATT_ACTIVE = 'simplevar-active-ef10f5b8bf220b327cb92f44516cc'
    const resultSelector = [
        // youtube
        'ytd-search:not([hidden]) h3>a:not([class*=-ad-], [class^=shorts])', // search results
        'ytd-watch-flexy:not([hidden]) #video-title:not([class*=-ad-], [class^=shorts])', // related vids
        'ytd-browse:not([hidden]) h3>a:not([class*=-ad-], [class^=shorts])', // feed
        // google results
        '.e9EfHf .Ww4FFb a>h3'
    ].join(',')
    document.body.addEventListener('keydown', e => {
        debugger
        if (!e.ctrlKey) return
        let goUp = false
        switch (e.keyCode) {
            case 38: // up
                goUp = true
                break
            case 40: // down
                break
            default:
                return
        }
        let active = document.activeElement
        switch (active.tagName) {
            case 'input':
            case 'textarea':
                //console.log('[bleep] ignoring cuz active element is ' + active.tagName)
                return
        }
        const results = document.querySelectorAll(resultSelector)
        let idx = -1
        active = undefined
        for (let i = 0; i < results.length; i++) {
            if (results[i].getAttribute(ATT_ACTIVE)) {
                idx = i
                active = results[i]
                break
            }
        }
        //console.log(`[bleep] idx=${idx} | len=${results.length}`)
        if (goUp) idx = idx <= 0 ? results.length - 1 : idx - 1
        else idx = idx + 1 < results.length ? idx + 1 : 0

        const next = results[idx]
        //console.log('[bleep] next idx=' + idx, next)
        if (!next) return
        if (active) {
            active.style.outline = 'none'
            active.setAttribute(ATT_ACTIVE, '')
        }
        next.style.outline = 'cyan solid 1px'
        next.style.outlineOffset = '-1px'
        next.setAttribute(ATT_ACTIVE, '1')
        next.scrollIntoView({block: 'center'})
        let a = next
        while (a.tagName !== 'A' && a.parentElement) a = a.parentElement
        a.focus()
        e.preventDefault()
        e.stopPropagation()
    })
})()