GitHub: Blame Previous Commit Button

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

Fra og med 28.06.2015. Se den nyeste version.

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

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 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.

(I already have a user script manager, let me install it!)

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: Blame Previous Commit Button
// @description     Add a button to visit 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         1
// @include         https://github.com*
// ==/UserScript==

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

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

    var main = function() {
        var m = blameUrlPattern.exec(document.location.href);
        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();
    $(document).on('pjax:end', main);

})(typeof unsafeWindow === 'undefined' ? window : unsafeWindow);