Github Better Diff

A userscript that adds a button to make the diff better

Från och med 2019-04-18. Se den senaste versionen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Github Better Diff
// @description A userscript that adds a button to make the diff better
// @license     MIT
// @author      Marco Pelegrini
// @namespace   https://github.com/marcopelegrini
// @version     1.0.0
// @include     https://github.com/*
// @exclude     https://github.com/*/*.diff
// @exclude     https://github.com/*/*.patch
// @run-at      document-idle
// @grant       GM.addStyle
// @grant       GM_addStyle
// @icon        https://github.githubassets.com/pinned-octocat.svg
// ==/UserScript==

(function() {

    function addButton() {
        var e
        if (/\/pull\/\d*\/(files|commits)/.test(location.href) && (e = document.querySelector('#files_bucket .pr-toolbar .diffbar > .float-right'))) {

            var r = e.querySelector('.GithubFixDiffButton')
            if (r) {
                r.parentElement.removeChild(r)
            }

            var btn = document.createElement('summary')
            btn.classList.add('btn', 'btn-sm')
            btn.appendChild(document.createTextNode('Better Diff'))

            var g = document.createElement('div')
            g.classList.add('GithubFixDiffButton', 'diffbar-item')
            g.appendChild(btn)

            e.insertBefore(g, e.firstChild)

            btn.addEventListener('click', betterDiffEvent, false)
        }
    }

    function betterDiffEvent(e) {
        // Fix deleted
        Array.from(document.querySelectorAll('.js-diff-load-button-container'))
            .filter(e => {
            var diffReason = e.querySelector('.hidden-diff-reason');
            return diffReason != null && diffReason.innerHTML.includes('This file was deleted');
        }).forEach(e => {
            var content = e.parentElement.parentElement;
            content.style.display = "none";
            content.parentElement.parentElement.querySelector('.file-header').querySelector('.diffstat.tooltipped.tooltipped-e').innerHTML = 'DELETED';
        });

        // Fix renamed
        Array.from(document.querySelectorAll('.data.highlight.empty')).forEach(e => {
            var content = e.parentElement;
            content.style.display = "none";
            content.parentElement.querySelector('.file-header').querySelector('.diffstat.tooltipped.tooltipped-e').innerHTML = 'RENAMED';
        });

        // Expand large files
        Array.from(document.querySelectorAll('.js-diff-load-button-container'))
            .forEach(container => container.querySelector('.load-diff-button').click());
    }

    // Init
    addButton()

    // Pjax
    document.addEventListener('pjax:end', addButton)
})()