|
| 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