Skip to content

Commit 3e43556

Browse files
committed
feat(github-mark-viewed): add userscript to toggle PR file "Viewed" with "v"
Mirrors the GitLab equivalent for GitHub PR "Files changed" pages, picking the file closest to the top of the viewport when many files are open.
1 parent dca72b4 commit 3e43556

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ the `==UserScript==` header and prompts to install.
1313
| Script | Install | Description |
1414
| ---------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
1515
| `gitlab-mark-viewed.user.js` | [install](https://raw.githubusercontent.com/solcik/userscripts/main/gitlab-mark-viewed.user.js) | Press `v` on a GitLab MR diff to toggle the focused file's "Viewed" checkbox. Matches `gitlab.com` and `git.vs-point.cz`. |
16+
| `github-mark-viewed.user.js` | [install](https://raw.githubusercontent.com/solcik/userscripts/main/github-mark-viewed.user.js) | Press `v` on a GitHub PR "Files changed" page to toggle the focused file's "Viewed" button. |
1617

1718
## Authoring
1819

github-mark-viewed.user.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// ==UserScript==
2+
// @name GitHub — mark file as Viewed with "v"
3+
// @namespace https://github.com/solcik/userscripts
4+
// @version 0.1.0
5+
// @description In a GitHub pull request "Files changed" view, press "v" to toggle the focused file's "Viewed" button.
6+
// @author David Solc
7+
// @match https://github.com/*/*/pull/*/files*
8+
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
9+
// @homepageURL https://github.com/solcik/userscripts
10+
// @supportURL https://github.com/solcik/userscripts/issues
11+
// @updateURL https://raw.githubusercontent.com/solcik/userscripts/main/github-mark-viewed.user.js
12+
// @downloadURL https://raw.githubusercontent.com/solcik/userscripts/main/github-mark-viewed.user.js
13+
// @require https://cdn.jsdelivr.net/npm/mousetrap@1.6.5/mousetrap.min.js
14+
// @grant none
15+
// @license MIT
16+
// ==/UserScript==
17+
18+
(function () {
19+
'use strict';
20+
21+
// Pick the "Mark as Viewed" button whose file is closest to the top of the viewport.
22+
function findFocusedViewedButton() {
23+
const buttons = document.querySelectorAll('button[class*="MarkAsViewedButton-module"]');
24+
if (!buttons.length) return null;
25+
26+
const focusLine = window.innerHeight * 0.25;
27+
let best = null;
28+
let bestDist = Infinity;
29+
30+
for (const btn of buttons) {
31+
const file =
32+
btn.closest('[data-testid="diff-file"]') ||
33+
btn.closest('copilot-diff-entry') ||
34+
btn.closest('.file') ||
35+
btn.closest('[id^="diff-"]') ||
36+
btn.parentElement;
37+
if (!file) continue;
38+
39+
const rect = file.getBoundingClientRect();
40+
if (rect.bottom < 0 || rect.top > window.innerHeight) continue;
41+
42+
const dist =
43+
rect.top <= focusLine && rect.bottom >= focusLine
44+
? 0
45+
: Math.min(Math.abs(rect.top - focusLine), Math.abs(rect.bottom - focusLine));
46+
47+
if (dist < bestDist) {
48+
bestDist = dist;
49+
best = btn;
50+
}
51+
}
52+
53+
return best;
54+
}
55+
56+
Mousetrap.bind('v', function () {
57+
const btn = findFocusedViewedButton();
58+
if (btn) btn.click();
59+
});
60+
})();

0 commit comments

Comments
 (0)