Skip to content

Instantly share code, notes, and snippets.

@kawahara
Created December 11, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kawahara/ab8ca5ab11ff84840ffd to your computer and use it in GitHub Desktop.
Save kawahara/ab8ca5ab11ff84840ffd to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name GitHub Compare Button
// @namespace http://tampermonkey.net/
// @version 0.1
// @description This script can add compare button to GitHub commit log.
// @author Shogo Kawahara <kawahara@bucyou.net>
// @match https://github.com/*
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
var fromCommitId, toCommitId, repository, lastPathName;
var reset = function() {
fromCommitId = null;
toCommitId = null;
$('.from-btn').removeClass('selected');
$('.to-btn').removeClass('selected');
};
var open = function() {
if (fromCommitId && toCommitId) {
window.open('http://github.com/' + repository + '/compare/' + fromCommitId + '...' + toCommitId);
reset();
}
};
var updateRepositoryUrl = function() {
var match = window.location.pathname.match(/^(\/[^\/]+\/[^\/]+)/)
if (match) {
repository = match[1];
}
};
var addButton = function() {
$('.commit-links-cell').each(function(i, obj) {
var cell = $(obj);
var commitId =$(cell.parent()).attr('data-url').match(/\/commit\/([0-9a-f]+)/)[1];
var fromButton = $('<button>')
.append($('<span>').attr('class', 'octicon octicon-git-compare'))
.attr('class', 'btn btn-outline tooltipped tooltipped-s from-btn')
.attr('aria-label', 'compare from');
if (commitId == fromCommitId) {
fromButton.addClass('selected');
}
var toButton = $('<button>')
.append($('<span>').attr('class', 'octicon octicon-git-compare'))
.attr('class', 'btn btn-outline tooltipped tooltipped-s to-btn')
.attr('aria-label', 'compare to');
if (commitId == toCommitId) {
toButton.addClass('selected');
}
var group = $('<div>').attr('class', 'commit-links-group btn-group');
group.append(fromButton);
group.append(toButton);
fromButton.click(function() {
fromCommitId = commitId;
$('.from-btn').removeClass('selected');
fromButton.addClass('selected');
open();
});
toButton.click(function() {
toCommitId = commitId;
$('.to-btn').removeClass('selected');
toButton.addClass('selected');
open();
});
cell.css('width', '300px')
cell.prepend(group);
});
updateRepositoryUrl();
};
addButton();
lastPathName = window.location.pathname;
var dc = $('[data-pjax-container]')
var mo = new MutationObserver(function() {
addButton();
if (window.location.pathname != lastPathName) {
lastPathName = window.location.pathname;
reset();
}
});
mo.observe(dc[0], {childList: true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment