-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathGithub_Commit_Diff.user.js
More file actions
185 lines (166 loc) · 5.58 KB
/
Github_Commit_Diff.user.js
File metadata and controls
185 lines (166 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// ==UserScript==
// @name Github Commit Diff
// @namespace https://github.com/jerone/UserScripts
// @description Adds button to show diff (or patch) file for commit
// @author jerone
// @copyright 2014+, jerone (https://github.com/jerone)
// @license CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
// @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @homepage https://github.com/jerone/UserScripts/tree/master/Github_Commit_Diff
// @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Commit_Diff
// @downloadURL https://github.com/jerone/UserScripts/raw/master/Github_Commit_Diff/Github_Commit_Diff.user.js
// @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.githubassets.com/pinned-octocat.svg
// @include https://github.com/*
// @exclude https://github.com/*/*.diff
// @exclude https://github.com/*/*.patch
// @version 1.6.7
// @grant none
// ==/UserScript==
// cSpell:ignore tooltipped, diffbar
(function () {
function addButton() {
var e;
if (
(/\/commit\//.test(location.href) ||
/\/compare\//.test(location.href)) &&
(e = document.getElementById("toc"))
) {
let r = e.querySelector(".GithubCommitDiffButton");
if (r) {
r.parentElement.removeChild(r);
}
let 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);
let 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"));
let g = document.createElement("div");
g.classList.add("GithubCommitDiffButton", "float-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|commits)/.test(location.href) &&
(e = document.querySelector(
"#files_bucket .pr-toolbar .diffbar > .float-right",
))
) {
let 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);
let a = document.createElement("a");
a.classList.add(
"btn",
"btn-sm",
"btn-outline",
"tooltipped",
"tooltipped-s",
);
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"));
let 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();
this.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 {
this.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();
// Pjax
document.addEventListener("pjax:end", addButton);
})();