GitLab:用 J/K 键在差异文件间跳转
添加 J 和 K 快捷键,在合并请求的变更文件之间快速滚动。
可复制的代码
// Use J and K keys to jump between diff files in a merge request
function files() {
return Array.prototype.slice.call(document.querySelectorAll('.diff-file'));
}
function jump(dir) {
const list = files();
if (!list.length) { return; }
const y = window.scrollY + 80;
let idx = list.findIndex(function (f) { return f.offsetTop > y; });
if (dir < 0) { idx = idx <= 0 ? 0 : idx - 1; }
else if (idx === -1) { idx = list.length - 1; }
const target = list[Math.max(0, Math.min(idx, list.length - 1))];
if (target) { window.scrollTo({ top: target.offsetTop - 60, behavior: 'smooth' }); }
}
document.addEventListener('keydown', function (e) {
const tag = (e.target.tagName || '').toLowerCase();
if (tag === 'input' || tag === 'textarea' || e.target.isContentEditable) { return; }
if (e.key === 'j') { jump(1); }
else if (e.key === 'k') { jump(-1); }
});
如何使用此示例
- 用上方按钮复制代码。
- 安装 JustZix(2 分钟),在目标页面打开扩展。
- 添加一条匹配该页面的新规则。
- 将代码粘贴到规则的 JavaScript 面板并保存 — 每次访问页面时都会运行。
为此示例评分
暂无评分 — 成为第一个。