Skip to content

Commit

Permalink
🎬 New version of GitHub Commit dIFF
Browse files Browse the repository at this point in the history
  • Loading branch information
jerone committed Apr 27, 2016
1 parent e52982a commit 5c7a4e1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 72 deletions.
181 changes: 109 additions & 72 deletions Github_Commit_Diff/Github_Commit_Diff.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,84 +11,121 @@
// @updateURL https://github.com/jerone/UserScripts/raw/master/Github_Commit_Diff/Github_Commit_Diff.user.js
// @supportURL https://github.com/jerone/UserScripts/issues
// @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
// @icon https://github.com/fluidicon.png
// @include https://github.com/*
// @version 1.6.2
// @version 1.6.3
// @grant none
// ==/UserScript==
/* global unsafeWindow */

(function() {

function addButton() {
var e;
if (!(/\/commit\//.test(location.href) || /\/compare\//.test(location.href) || /\/pull\/\d*\/files/.test(location.href)) ||
!(e = document.getElementById("toc"))) { return; }

var r = e.querySelector(".GithubCommitDiffButton");
if (r) { r.parentElement.removeChild(r); }

function getPatchOrDiffHref(type) {
return (document.querySelector("link[type='text/plain+" + type + "']")
|| document.querySelector("link[type='text/x-" + type + "']")
|| { href: location.href + "." + type }).href;
}

var b = e.querySelector(".toc-diff-stats");

var s = document.createElement("span");
s.textContent = " ";
s.classList.add("octicon", "octicon-diff");
s.style.color = "#333"; // set color because of css selector `p.explain .octicon`;

var a = document.createElement("a");
a.classList.add("btn", "btn-sm", "tooltipped", "tooltipped-n");
a.setAttribute("href", getPatchOrDiffHref("diff"));
a.setAttribute("rel", "nofollow");
a.setAttribute("aria-label", "Show commit diff.\r\nHold Shift to open commit patch.");
a.appendChild(s);
a.appendChild(document.createTextNode(" Diff"));

var g = document.createElement("div");
g.classList.add("GithubCommitDiffButton", "right");
g.style.margin = "0 10px 0 0"; // give us some room;
g.appendChild(a);

b.parentNode.insertBefore(g, b);

a.addEventListener("mousedown", function(e) {
if (e.shiftKey) {
var patch = getPatchOrDiffHref("patch");
e.preventDefault();
a.setAttribute("href", patch);
if (e.which === 1) { // left click;
location.href = patch;
// To prevent Firefox default behavior (opening a new window)
// when pressing shift-click on a link, delete the link.
this.parentElement.removeChild(this);
} else if (e.which === 2) { // middle click;
window.open(patch, "GithubCommitDiff");
}
} else {
a.setAttribute("href", getPatchOrDiffHref("diff"));
}
}, false);
a.addEventListener("mouseout", function() {
a.setAttribute("href", getPatchOrDiffHref("diff"));
}, false);
}

// init;
addButton();

// on pjax;
unsafeWindow.$(document).on("pjax:end", addButton); // `pjax:end` also runs on history back;

// on PR files tab;
var f;
if ((f = document.querySelector(".js-pull-request-tab[data-container-id='files_bucket']"))) {
f.addEventListener("click", function() {
window.setTimeout(addButton, 13);
});
}
function addButton() {
var e;
if ((/\/commit\//.test(location.href) || /\/compare\//.test(location.href)) && (e = document.getElementById("toc"))) {

var r = e.querySelector(".GithubCommitDiffButton");
if (r) {
r.parentElement.removeChild(r);
}

var b = e.querySelector(".toc-diff-stats");

const s = document.createElementNS("http://www.w3.org/2000/svg", "svg");
s.classList.add("octicon", "octicon-diff");
s.setAttributeNS(null, "height", 16);
s.setAttributeNS(null, "width", 14);
s.setAttributeNS(null, "viewBox", "0 0 14 16");

const p = document.createElementNS("http://www.w3.org/2000/svg", "path");
p.setAttributeNS(null, "d", "M6 7h2v1H6v2h-1V8H3v-1h2V5h1v2zM3 13h5v-1H3v1z m4.5-11l3.5 3.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V3c0-0.55 0.45-1 1-1h6.5z m2.5 4L7 3H1v12h9V6zM8.5 0S3 0 3 0v1h5l4 4v8h1V4.5L8.5 0z");
s.appendChild(p);

var a = document.createElement("a");
a.classList.add("btn", "btn-sm", "tooltipped", "tooltipped-n");
a.setAttribute("href", getPatchOrDiffHref("diff"));
a.setAttribute("rel", "nofollow");
a.setAttribute("aria-label", "Show commit diff.\r\nHold Shift to open commit patch.");
a.appendChild(s);
a.appendChild(document.createTextNode(" Diff"));

var g = document.createElement("div");
g.classList.add("GithubCommitDiffButton", "right");
g.style.margin = "0 10px 0 0"; // give us some room;
g.appendChild(a);

b.parentNode.insertBefore(g, b);

a.addEventListener("mousedown", mousedownEvent, false);
a.addEventListener("mouseout", function() {
a.setAttribute("href", getPatchOrDiffHref("diff"));
}, false);
} else if (/\/pull\/\d*\/files/.test(location.href) && (e = document.querySelector("#files_bucket .pr-toolbar .diffbar > .right"))) {

var r = e.querySelector(".GithubCommitDiffButton");
if (r) {
r.parentElement.removeChild(r);
}

const s = document.createElementNS("http://www.w3.org/2000/svg", "svg");
s.classList.add("octicon", "octicon-diff");
s.setAttributeNS(null, "height", 16);
s.setAttributeNS(null, "width", 14);
s.setAttributeNS(null, "viewBox", "0 0 14 16");

const p = document.createElementNS("http://www.w3.org/2000/svg", "path");
p.setAttributeNS(null, "d", "M6 7h2v1H6v2h-1V8H3v-1h2V5h1v2zM3 13h5v-1H3v1z m4.5-11l3.5 3.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V3c0-0.55 0.45-1 1-1h6.5z m2.5 4L7 3H1v12h9V6zM8.5 0S3 0 3 0v1h5l4 4v8h1V4.5L8.5 0z");
s.appendChild(p);

var a = document.createElement("a");
a.classList.add("btn-link", "muted-link");
a.setAttribute("href", getPatchOrDiffHref("diff"));
a.setAttribute("rel", "nofollow");
a.setAttribute("aria-label", "Show commit diff.\r\nHold Shift to open commit patch.");
a.appendChild(s);
a.appendChild(document.createTextNode(" Diff"));

var g = document.createElement("div");
g.classList.add("GithubCommitDiffButton", "diffbar-item");
g.appendChild(a);

e.insertBefore(g, e.firstChild);

a.addEventListener("mousedown", mousedownEvent, false);
a.addEventListener("mouseout", function() {
a.setAttribute("href", getPatchOrDiffHref("diff"));
}, false);
}
}

function mousedownEvent(e) {
if (e.shiftKey) {
var patch = getPatchOrDiffHref("patch");
e.preventDefault();
a.setAttribute("href", patch);
if (e.which === 1) { // left click;
location.href = patch;
// To prevent Firefox default behavior (opening a new window)
// when pressing shift-click on a link, delete the link.
this.parentElement.removeChild(this);
} else if (e.which === 2) { // middle click;
window.open(patch, "GithubCommitDiff");
}
} else {
a.setAttribute("href", getPatchOrDiffHref("diff"));
}
}

function getPatchOrDiffHref(type) {
return (document.querySelector("link[type='text/plain+" + type + "']") || document.querySelector("link[type='text/x-" + type + "']") || {
href: location.href + "." + type
}).href;
}

// init;
addButton();

// on pjax;
document.addEventListener('pjax:end', addButton);

})();
2 changes: 2 additions & 0 deletions Github_Commit_Diff/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ This works on commits, pull requests and compare pages.

## Version History

* **1.6.3**
* Fixed issues after recent layout updates;
* **1.6.2**
* Fixed issues after recent layout updates;
* **1.6.1**
Expand Down

0 comments on commit 5c7a4e1

Please sign in to comment.