GitHub code review helper - open/hide diff on click

Open/hide GitHub diff when clicking on diff header

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 code review helper - open/hide diff on click
// @namespace    http://think.js/
// @version      0.3.4
// @description  Open/hide GitHub diff when clicking on diff header
// @include      http*://github.com/*/*/commit/*
// @include      http*://github.com/*/*/pull/*
// @include      http*://github.com/*/*/compare/*
// @grant none
// @copyright    2013+, Victor Homyakov
// ==/UserScript==

function hasClass(element, className) {
    return element && element.classList && element.classList.contains(className);
}

function isDiffHeader(element) {
    return hasClass(element, 'file-header');
}

function isDiffContent(element) {
    return hasClass(element, 'image') || hasClass(element, 'data') || hasClass(element, 'render-wrapper');
}

function toggle(element) {
    element.hidden = !element.hidden;
    element.style.display = element.hidden ? 'none' : '';
}

document.body.addEventListener('click', function(event) {
    var target = event.target;
    while (target) {
        if (hasClass(target, 'file-actions')) {
            break;
        }
        if (isDiffHeader(target)) {
            var next = target;

            next = next.nextElementSibling;
            if (isDiffContent(next)) {
                toggle(next);
            }

            next = next.nextElementSibling;
            if (isDiffContent(next)) {
                toggle(next);
            }

            break;
        }
        target = target.parentElement;
    }
});