如何在Chrome中配置搜索词,自动填充GitHub仓库的Go To File搜索框?
实现Chrome中自动填充GitHub仓库的Go To File输入框
完全可以实现,分两步操作:
1. 配置Chrome自定义搜索引擎
- 打开Chrome设置,进入「搜索引擎」→「管理搜索引擎和网站搜索」
- 点击「添加」,填写:
- 搜索框:
ghfile(你可以自定义这个触发词) - 关键字:
github.com/你的仓库完整路径(比如github.com/vuejs/vue) - URL:
https://github.com/你的仓库完整路径?searchTerm=%s
- 搜索框:
- 保存后,在地址栏输入
ghfile 你要搜索的文件名,就能直接打开目标仓库并带上搜索参数
2. 用Tampermonkey脚本自动填充输入框
因为GitHub的「Go To File」输入框是动态生成的(只有按t键或点击按钮才会出现),直接修改HTML标签没用,需要脚本模拟触发并填充:
- 安装Tampermonkey插件(Chrome应用商店可找到)
- 点击Tampermonkey图标→「创建新脚本」,替换默认代码为以下内容:
// ==UserScript== // @name GitHub Auto Fill Go To File // @namespace http://tampermonkey.net/ // @version 0.1 // @description Auto fill Go To File input with URL search term // @match https://github.com/* // @grant none // ==/UserScript== (function() { 'use strict'; const urlParams = new URLSearchParams(window.location.search); const searchTerm = urlParams.get('searchTerm'); if (searchTerm) { setTimeout(() => { // 模拟按t键触发Go To File功能 document.dispatchEvent(new KeyboardEvent('keydown', { key: 't' })); // 定位输入框并填充内容 const input = document.querySelector('input[aria-label="Go to file"]'); if (input) { input.value = searchTerm; input.dispatchEvent(new Event('input', { bubbles: true })); } }, 500); } })();
- 保存脚本后,再用刚才的
ghfile 搜索词打开仓库,页面会自动触发「Go To File」并填充你的搜索词
补充说明
- 延迟时间(500ms)可以根据你的网络情况调整,如果填充失败,适当延长时间
- 若GitHub更新页面结构导致选择器失效,打开仓库后按
t调出输入框,右键检查元素,替换脚本中querySelector里的选择器即可
内容的提问来源于stack exchange,提问作者Bobby T.
相关产品推荐
相关产品推荐

