GitHub: Blame Previous Commit Button

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

当前为 2016-06-08 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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();

})();