Skip to content

Commit e52982a

Browse files
committed
🎬 New version of GitHub Commit Whitespace
1 parent a154ed3 commit e52982a

2 files changed

Lines changed: 72 additions & 57 deletions

File tree

Github_Commit_Whitespace/Github_Commit_Whitespace.user.js

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,69 +11,82 @@
1111
// @updateURL https://github.com/jerone/UserScripts/raw/master/Github_Commit_Whitespace/Github_Commit_Whitespace.user.js
1212
// @supportURL https://github.com/jerone/UserScripts/issues
1313
// @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
14+
// @icon https://github.com/fluidicon.png
1415
// @include https://github.com/*
15-
// @version 1.4.2
16+
// @version 1.4.3
1617
// @grant none
1718
// ==/UserScript==
1819
/* global unsafeWindow */
1920

2021
(function() {
2122

22-
function addButton() {
23-
var e;
24-
if (!(/\/commit\//.test(location.href) || /\/compare\//.test(location.href) || /\/pull\/\d*\/files/.test(location.href)) ||
25-
!(e = document.getElementById("toc"))) { return; }
26-
27-
var r = e.querySelector(".GithubCommitWhitespaceButton");
28-
if (r) { r.parentElement.removeChild(r); }
29-
30-
var on = /w=/.test(location.search); // any occurense results in enabling;
31-
32-
var b = e.querySelector(".toc-diff-stats");
33-
34-
var s = document.createElement("span");
35-
s.textContent = " \u2423";
36-
s.style.color = "#333"; // set color because of css selector `p.explain .octicon`;
37-
38-
var a = document.createElement("a");
39-
a.classList.add("btn", "btn-sm", "tooltipped", "tooltipped-n");
40-
if (on) { a.classList.add("selected"); }
41-
a.setAttribute("href", url(on));
42-
a.setAttribute("rel", "nofollow");
43-
a.setAttribute("aria-label", on ? "Show commit whitespace" : "Hide commit whitespaces");
44-
a.appendChild(s);
45-
46-
var g = document.createElement("div");
47-
g.classList.add("GithubCommitWhitespaceButton", "right");
48-
g.style.margin = "0 10px 0 0"; // give us some room;
49-
g.appendChild(a);
50-
51-
b.parentNode.insertBefore(g, b);
52-
}
53-
54-
function url(on) {
55-
var searches = location.search.replace(/^\?/, "").split("&").filter(function(item) {
56-
return item && !/w=.*/.test(item);
57-
});
58-
if (!on) {
59-
searches.push("w=1");
60-
}
61-
return location.href.replace(location.search, "")
62-
+ (searches.length > 0 ? "?" + searches.join("&") : "");
63-
}
64-
65-
// init;
66-
addButton();
67-
68-
// on pjax;
69-
unsafeWindow.$(document).on("pjax:end", addButton); // `pjax:end` also runs on history back;
70-
71-
// on PR files tab;
72-
var f;
73-
if ((f = document.querySelector(".js-pull-request-tab[data-container-id='files_bucket']"))) {
74-
f.addEventListener("click", function() {
75-
window.setTimeout(addButton, 13);
76-
});
77-
}
23+
function addButton() {
24+
var e;
25+
if ((/\/commit\//.test(location.href) || /\/compare\//.test(location.href)) && (e = document.getElementById("toc"))) {
26+
27+
var r = e.querySelector(".GithubCommitWhitespaceButton");
28+
if (r) {
29+
r.parentElement.removeChild(r);
30+
}
31+
32+
var on = /w=/.test(location.search); // any occurense results in enabling;
33+
34+
var b = e.querySelector(".toc-diff-stats");
35+
36+
var a = document.createElement("a");
37+
a.classList.add("btn", "btn-sm", "tooltipped", "tooltipped-n");
38+
if (on) {
39+
a.classList.add("selected");
40+
}
41+
a.setAttribute("href", url(on));
42+
a.setAttribute("rel", "nofollow");
43+
a.setAttribute("aria-label", on ? "Show commit whitespace" : "Hide commit whitespaces");
44+
a.appendChild(document.createTextNode(" \u2423"));
45+
46+
var g = document.createElement("div");
47+
g.classList.add("GithubCommitWhitespaceButton", "right");
48+
g.style.margin = "0 10px 0 0"; // give us some room;
49+
g.appendChild(a);
50+
51+
b.parentNode.insertBefore(g, b);
52+
} else if (/\/pull\/\d*\/files/.test(location.href) && (e = document.querySelector("#files_bucket .pr-toolbar .diffbar > .right"))) {
53+
54+
var r = e.querySelector(".GithubCommitWhitespaceButton");
55+
if (r) {
56+
r.parentElement.removeChild(r);
57+
}
58+
59+
var on = /w=/.test(location.search); // any occurense results in enabling;
60+
61+
var a = document.createElement("a");
62+
a.classList.add("btn-link", "muted-link");
63+
a.setAttribute("href", url(on));
64+
a.setAttribute("rel", "nofollow");
65+
a.setAttribute("aria-label", on ? "Show commit whitespace" : "Hide commit whitespaces");
66+
a.appendChild(document.createTextNode(on ? "Show whitespace" : "Hide whitespace"));
67+
68+
var g = document.createElement("div");
69+
g.classList.add("GithubCommitWhitespaceButton", "diffbar-item");
70+
g.appendChild(a);
71+
72+
e.insertBefore(g, e.firstChild);
73+
}
74+
}
75+
76+
function url(on) {
77+
var searches = location.search.replace(/^\?/, "").split("&").filter(function(item) {
78+
return item && !/w=.*/.test(item);
79+
});
80+
if (!on) {
81+
searches.push("w=1");
82+
}
83+
return location.href.replace(location.search, "") + (searches.length > 0 ? "?" + searches.join("&") : "");
84+
}
85+
86+
// init;
87+
addButton();
88+
89+
// on pjax;
90+
document.addEventListener('pjax:end', addButton);
7891

7992
})();

Github_Commit_Whitespace/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ This works on commits, pull requests and compare pages.
2828

2929
## Version History
3030

31+
* **1.4.3**
32+
* Fixed issues after recent layout updates;
3133
* **1.4.2**
3234
* Fixed issues after recent layout updates;
3335
* **1.4.1**

0 commit comments

Comments
 (0)