Github Better Diff

A userscript that adds a button to make the diff better

Stan na 18-04-2019. Zobacz najnowsza wersja.

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

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

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 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)
})()