// ==UserScript== // @name GitHub 增强 // @name:zh-CN GitHub 增强 // @name:en GitHub Enhancer // @namespace https://github.com/yilecoding/helper-scripts // @version 2025.08.07.9 // @description GitHub 页面增强:仓库页显示仓库大小、默认分支、创建时间(按仓库缓存 1 小时,API 限流时显示恢复时间;可设置 token 提高限额、查看私有仓库);文件列表每行一键下载单个文件,目录一键复制 git sparse-checkout 命令;文件页复制 raw 链接、复制带行号的永久链接(分支名换成提交 SHA);「3 天前」这类相对时间旁边显示本地时区的绝对时间;各功能可在菜单里单独开关;分支名带 / 也能正确识别,适配 Turbo 换页。 // @description:zh-CN GitHub 页面增强:仓库页显示仓库大小、默认分支、创建时间(按仓库缓存 1 小时,API 限流时显示恢复时间;可设置 token 提高限额、查看私有仓库);文件列表每行一键下载单个文件,目录一键复制 git sparse-checkout 命令;文件页复制 raw 链接、复制带行号的永久链接(分支名换成提交 SHA);「3 天前」这类相对时间旁边显示本地时区的绝对时间;各功能可在菜单里单独开关;分支名带 / 也能正确识别,适配 Turbo 换页。 // @description:en GitHub enhancements: shows repository size, default branch and creation date on repository pages (cached per repository for an hour; shows when the API rate limit resets; an optional token raises the limit and unlocks private repositories); adds a one-click download button to every file in the file list and a "copy git sparse-checkout command" button to every directory; on file pages copies the raw URL or a permalink with line numbers (branch replaced by the commit SHA); shows the absolute local time next to every relative time ("3 days ago"); every feature can be toggled from the menu; handles branch names containing slashes and follows Turbo navigation. // @author 壹樂(V:ita1125) // @match https://github.com/* // @noframes // @grant GM_xmlhttpRequest // @grant GM_getValue // @grant GM_setValue // @grant GM_deleteValue // @grant GM_listValues // @grant GM_addStyle // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant GM_download // @grant GM_setClipboard // @grant unsafeWindow // @connect api.github.com // @connect raw.githubusercontent.com // @license MIT // @run-at document-end // @homepageURL https://github.com/yile2020/helper-scripts // @supportURL https://github.com/yile2020/helper-scripts/issues // @updateURL https://raw.githubusercontent.com/yile2020/helper-scripts/main/github-enhancer/github-enhancer.user.js // @downloadURL https://raw.githubusercontent.com/yile2020/helper-scripts/main/github-enhancer/github-enhancer.user.js // ==/UserScript== // 脚本持续更新维护,欢迎 star / watch 仓库获取最新版本: // https://github.com/yile2020/helper-scripts (function () { 'use strict'; /** 日志前缀。控制台过滤框输入「GitHub增强」只看本脚本。 */ const TAG = '[GitHub增强]'; // ========================================================================= // 配置 // ========================================================================= const CONFIG = { applyDebounce: 150, // 导航事件 / DOM 变化后等这么久再统一注入一次 cacheTtl: 60 * 60 * 1000, // 仓库信息按仓库缓存 1 小时 requestTimeout: 15000, // API 请求超时 apiBase: 'https://api.github.com', rawBase: 'https://raw.githubusercontent.com', toastMs: 1800, // 「已复制」提示停留时间 }; /** * github.com 下第一段路径是这些时不是仓库(/settings/profile、/orgs/x/teams……)。 * 用户名不能取这些名字,所以命中就可以断定不是 owner。 */ const RESERVED_OWNERS = new Set([ 'about', 'account', 'apps', 'codespaces', 'collections', 'contact', 'copilot', 'customer-stories', 'dashboard', 'enterprise', 'events', 'explore', 'features', 'github-copilot', 'issues', 'join', 'login', 'logout', 'marketplace', 'new', 'notifications', 'organizations', 'orgs', 'pricing', 'pulls', 'readme', 'search', 'security', 'sessions', 'settings', 'signup', 'site', 'sponsors', 'stars', 'team', 'topics', 'trending', 'users', 'watching', ]); /** 路径第三段是这些时,后面跟的是「ref + 文件路径」。 */ const REF_KINDS = new Set(['tree', 'blob', 'blame', 'edit', 'raw', 'commits']); const FULL_SHA = /^[0-9a-f]{40}$/i; // ========================================================================= // 页面上下文:owner / repo / 页面类型 / ref / 路径 // ========================================================================= const Context = (() => { function decode(seg) { try { return decodeURIComponent(seg); } catch (e) { return seg; } } /** * 只从 URL 解析,不碰 DOM。 * * /owner/repo/tree/feature/x/src 这种地址本身是有歧义的:分支名可以带 `/`, * 光看 URL 分不清是「feature 分支下的 x/src」还是「feature/x 分支下的 src」。 * 这里先按「第一段是 ref」猜,`exact: false` 标出来,交给 resolve 消歧。 * 只有一段(/tree/main)、整段是 40 位 SHA、或者没有 ref 时才算确定。 */ function parse(pathname) { const segs = pathname.split('/').filter(Boolean).map(decode); if (segs.length < 2) return null; const [owner, repo, kind, ...rest] = segs; if (RESERVED_OWNERS.has(owner.toLowerCase())) return null; const ctx = { owner, repo, type: kind || 'root', ref: null, path: '', refPath: '', sha: null, exact: true }; if (!kind) return ctx; if (REF_KINDS.has(kind)) { if (!rest.length) return ctx; // /owner/repo/tree:等同仓库首页 ctx.refPath = rest.join('/'); ctx.ref = rest[0]; ctx.path = rest.slice(1).join('/'); if (FULL_SHA.test(rest[0])) ctx.sha = rest[0].toLowerCase(); else ctx.exact = rest.length === 1; return ctx; } if (kind === 'commit' && rest[0]) { // /commit/.diff、.patch 也是同一个提交 const sha = rest[0].replace(/\.(diff|patch)$/, ''); ctx.ref = sha; if (FULL_SHA.test(sha)) ctx.sha = sha.toLowerCase(); return ctx; } if (kind === 'pull' && rest[1] === 'commits' && rest[2] && FULL_SHA.test(rest[2])) { ctx.ref = ctx.sha = rest[2].toLowerCase(); return ctx; } if (kind === 'releases' && rest[0] === 'tag' && rest.length > 1) { // tag 名也能带 `/`,但这里 tag 后面没有文件路径,剩下的整段就是 tag 名 ctx.type = 'release'; ctx.ref = rest.slice(1).join('/'); return ctx; } return ctx; } function key(ctx) { return ctx ? `${ctx.owner}/${ctx.repo}|${ctx.type}|${ctx.ref || ''}|${ctx.path}` : ''; } // --------------------------------------------------------------------- // ref 消歧 // --------------------------------------------------------------------- /** refPath 是否以 name 开头(按 `/` 分段对齐,feature/xy 不算 feature/x 开头)。 */ function startsWithRef(refPath, name) { return !!name && (refPath === name || refPath.startsWith(name + '/')); } function decodePath(p) { return p.split('/').map(decode).join('/'); } /** * React 代码视图的嵌入数据( 里的 script[data-target="react-app.embeddedData"]), * 里面的 refInfo 有确切的分支名、类型和当前提交 SHA。 * * 坑:它只描述**首次加载**的那一页。之后 React 自己路由换目录,这段 JSON 原样留在 DOM 里, * 内容还是老页面的。所以只有 和当前地址一致时才信它。 */ function fromEmbedded() { const here = decodePath(location.pathname).replace(/\/$/, ''); for (const app of document.querySelectorAll('react-app[initial-path]')) { if (decodePath(app.getAttribute('initial-path')).replace(/\/$/, '') !== here) continue; const script = app.querySelector('script[data-target="react-app.embeddedData"]'); const info = script && embeddedRefInfo(script); if (info) { return { name: info.name, refType: info.refType || null, sha: FULL_SHA.test(info.currentOid || '') ? info.currentOid.toLowerCase() : null, source: 'embedded', }; } } return null; } /** * 按