GitHub: Blame Previous Commit Button

Add a button to `git blame` the `sha^` (parent commit) on each commit on a `git blame` page.

Stan na 08-06-2016. Zobacz najnowsza wersja.

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

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

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: Blame Previous Commit Button
// @description     Add a button to `git blame` the `sha^` (parent commit) on each commit on a `git blame` page.
// @author          Chris H (Zren / Shade)
// @icon            https://github.com/favicon.ico
// @namespace       http://xshade.ca
// @version         3
// @include         https://github.com*
// ==/UserScript==

// Works with GitHub Enterprise as well, just add your own @include rule for your domain.

(function(){

    var createElement = function(html) {
        var e = document.createElement('div');
        e.innerHTML = html;
        return e.firstChild;
    };

    var blameUrlPattern = /^(\/[^\/]+\/[^\/]+\/blame\/)([^\/]+)(.*)$/;

    var main = function() {
        var m = blameUrlPattern.exec(document.location.pathname);
        if (!m)
            return;

        Array.prototype.forEach.call(document.querySelectorAll('.blame-commit-info'), function(e) {
            var url = e.firstElementChild.href;
            var sha = url.substr(url.lastIndexOf('/') + 1);
            var blameUrlForCurrentFile = m[1] + sha + encodeURI('^') + m[3];
            var html = '<a class="blame-sha blame-previous-commit" href="' + blameUrlForCurrentFile + '">Before</a>';
            var a = createElement(html);
            e.lastElementChild.insertBefore(a, e.lastElementChild.firstChild);
        });
    };

    main();

})();