Github Better Diff

A userscript that adds a button to make the diff better

Verze ze dne 18. 04. 2019. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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