油猴脚本:快速聚焦bilibili搜索框

发布于: 2026-10-22 23:42 · 更新于: 2026-11-24 23:10

bilibili油猴脚本,按 / 快速聚焦搜索框,按 Esc 取消聚焦,另外 https://www.bilibili.com/zh-Hans/ 这个路径访问 bilibili 似乎是专门给简中定制的界面,非常清新没有广告。

  • 页面任意位置(非输入状态)按 / → 光标跳入顶部搜索框
  • 搜索框聚焦时按 Esc → 失焦,面板收起
  • 在评论框、弹幕框等输入区域按 / → 正常输入斜杠,不会跳走

补充 url 清洗功能。

// ==UserScript==
// @name         Bilibili 按 / 聚焦搜索框,Esc 取消 + 清追踪参数
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  在B站按 / 聚焦顶部搜索框,按 Esc 取消聚焦,并自动清洗 URL(只保留纯路径)
// @author       111
// @match        https://www.bilibili.com/*
// @match        https://search.bilibili.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    /* ================= URL 清洗 ================= */

    function cleanUrl(rawUrl) {
        try {
            const u = new URL(rawUrl, location.href);

            // 只处理 bilibili 同源链接
            if (!/(^|\.)bilibili\.com$/.test(u.hostname)) return null;

            // 有 query 或 hash 才需要清
            if (!u.search && !u.hash) return null;

            // 只返回路径,query 和 hash 全部丢弃
            return u.pathname;
        } catch (_) {
            return null;
        }
    }

    function applyClean() {
        const next = cleanUrl(location.href);
        const cur = location.pathname + location.search + location.hash;
        if (next && next !== cur) {
            history.replaceState(history.state, '', next);
        }
    }

    // SPA 路由:劫持 replaceState / pushState,切页后继续清
    function patchHistory() {
        const origReplace = history.replaceState;
        const origPush = history.pushState;

        history.replaceState = function (state, title, url) {
            if (url) {
                const c = cleanUrl(String(url));
                if (c) url = c;
            }
            return origReplace.apply(this, [state, title, url]);
        };

        history.pushState = function (state, title, url) {
            if (url) {
                const c = cleanUrl(String(url));
                if (c) url = c;
            }
            return origPush.apply(this, [state, title, url]);
        };

        window.addEventListener('popstate', applyClean);
    }

    applyClean();
    patchHistory();
    /* ================= 新增结束 ================= */

    // 查找搜索框(兼容不同页面结构)
    function getSearchInput() {
        return document.querySelector('.nav-search-input')
            || document.querySelector('.center-search__bar input.nav-search-input')
            || document.querySelector('#nav-searchform input.nav-search-input')
            || document.querySelector('.search-input-wrap input.search-input-el')
            || document.querySelector('input.search-input-el');
    }

    function isTypingInInput() {
        const el = document.activeElement;
        if (!el) return false;
        const tag = el.tagName.toLowerCase();
        if (tag === 'input' || tag === 'textarea' || tag === 'select') return true;
        if (el.isContentEditable) return true;
        return false;
    }

    document.addEventListener('keydown', function (e) {
        if (e.key === 'Escape') {
            const input = getSearchInput();
            if (input && document.activeElement === input) {
                e.preventDefault();
                input.blur();
                const panel = document.querySelector('.nav-search-panel');
                if (panel) panel.style.display = 'none';
            }
            return;
        }

        if (e.key !== '/' || e.ctrlKey || e.altKey || e.metaKey) return;
        if (isTypingInInput()) return;

        const input = getSearchInput();
        if (!input) return;

        e.preventDefault();
        input.focus();
        const len = input.value.length;
        try { input.setSelectionRange(len, len); } catch (_) {}
    }, true);
})();