Github Better Diff

A userscript that adds a button to make the diff better

Verzia zo dňa 18.04.2019. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

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