Github Better Diff

A userscript that adds a button to make the diff better

Tính đến 18-04-2019. Xem phiên bản mới nhất.

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

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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