Skip to content

Instantly share code, notes, and snippets.

@mRoca
Last active July 7, 2026 14:32
Show Gist options
  • Select an option

  • Save mRoca/00430cba0872a0d4d4ef0e0e99f8aa6c to your computer and use it in GitHub Desktop.

Select an option

Save mRoca/00430cba0872a0d4d4ef0e0e99f8aa6c to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name GitHub Mark File Viewed
// @version 0.1
// @description Mark file as "viewed" on GitHub PR UI when hovering and pressing 'Escape' key
// @match https://github.com/*
// ==/UserScript==
function markFileAsViewed() {
// Legacy Github PR review page
const legacyFileEl = document.querySelector(
'[data-details-container-group="file"]:hover'
);
if (legacyFileEl) {
const fileViewedCheckbox = legacyFileEl.querySelector(
'.js-reviewed-toggle'
);
fileViewedCheckbox?.click();
return;
}
// New page format
const newFileEl = document.querySelector('div[class^="PullRequestDiffsList"]:hover');
console.log(newFileEl);
if (newFileEl) {
const fileViewedCheckbox = Array.from(
newFileEl.querySelectorAll('button')
).find((el) => el.textContent === 'Viewed');
fileViewedCheckbox?.click();
return;
}
}
// event parameter was missing
function handleKeyDown(event) {
console.log(event);
if (event.key === 'Escape') {
markFileAsViewed();
}
}
function initGhListener() {
'use strict';
if (window.disposeMarkAsViewedByEscape) {
window.disposeMarkAsViewedByEscape();
}
window.disposeMarkAsViewedByEscape = registerEventListener();
function registerEventListener() {
window.addEventListener('keydown', handleKeyDown);
return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}
}
window.addEventListener('load', initGhListener);
if (window.navigation) {
// Chrome can detect url change, see https://developer.mozilla.org/en-US/docs/Web/API/Navigation/navigate_event
window.navigation.addEventListener('navigate', initGhListener);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment