GitHub code review helper - open/hide diff on click

Open/hide GitHub diff when clicking on diff header

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

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

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