Github PR review: mark moved files as viewed

Adds an option to the Github PR review page to mark moved files as viewed.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name     Github PR review: mark moved files as viewed
// @description  Adds an option to the Github PR review page to mark moved files as viewed.
// @include  https://github.com/*
// @license  GPLv2
// @version  1
// @grant    none
// @namespace https://greasyfork.org/users/13329
// ==/UserScript==

function should_mark(file) {
  if (file.dataset.fileDeleted === 'true') {
    return true;
  }
  
  const changes = file.getElementsByClassName('diffstat')[0].childNodes[0].textContent.trim();
  if (changes === '0' || changes === 'BIN') {
    return true;
  }
}

function mark_moved_files(event) {
  event.preventDefault();
  for (const file of document.getElementsByClassName('file')) {
    if (should_mark(file)) {
    	const checkbox = file.getElementsByClassName('js-reviewed-checkbox')[0];
    	if (!checkbox.checked) {
	      checkbox.click();
	    }
	  }
  }
  return false;
}


function add_checkbox(callback) {
  const submit_button = document.getElementById('whitespace-cb-lg')?.form?.elements?.[5];
  if (!submit_button) {
    return;
  }

  const button = document.createElement('button');
  button.addEventListener('click', callback);
  button.setAttribute('class', 'btn-sm btn');
  button.setAttribute('id', 'ignore-moved-files');

  button.append(document.createTextNode('Mark moved files as read'));

  submit_button.parentNode.insertBefore(button, submit_button);
}

function init() {
  add_checkbox(mark_moved_files);
}

window.addEventListener('DOMContentLoaded', init);