Last active
April 16, 2026 01:33
-
-
Save YCF/eb039c326a2cb4abc97a28b61008a96c to your computer and use it in GitHub Desktop.
Tampermonkey - V2EX站内页面双击ESC回到顶部并聚焦到搜索框
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name V2EX站内页面双击ESC回到顶部并聚焦到搜索框 | |
| // @namespace http://tampermonkey.net/ | |
| // @version 1.0 | |
| // @description 双击ESC键,页面滚动到顶部并自动聚焦 #search 输入框 | |
| // @author GitHub: @YCF + AI | |
| // @match *://*.v2ex.com/* | |
| // @match *://v2ex.com/* | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| // 配置:双击间隔时间(毫秒),默认 250ms 内按两次 ESC 算双击 | |
| const DOUBLE_PRESS_INTERVAL = 250; | |
| let lastEscPressTime = 0; | |
| // 监听键盘按下事件 | |
| document.addEventListener('keydown', function(e) { | |
| // 判断按下的键是否是 ESC | |
| if (e.key === 'Escape' || e.key === 'Esc') { | |
| const now = Date.now(); | |
| // 判断是否为【双击】 | |
| if (now - lastEscPressTime < DOUBLE_PRESS_INTERVAL) { | |
| // ====================== | |
| // 执行:滚动到顶部 | |
| // ====================== | |
| window.scrollTo({ | |
| top: 0, | |
| behavior: 'smooth' // 平滑滚动 | |
| }); | |
| // ====================== | |
| // 执行:聚焦搜索框 #search | |
| // ====================== | |
| const searchInput = document.querySelector('#search'); | |
| if (searchInput) { | |
| searchInput.focus(); // 光标定位 | |
| searchInput.select(); // 可选:自动全选已有文字 | |
| } | |
| // 重置计时,防止连续触发 | |
| lastEscPressTime = 0; | |
| } else { | |
| // 第一次按下,记录时间 | |
| lastEscPressTime = now; | |
| } | |
| } | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment