-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.user.js
More file actions
424 lines (343 loc) · 17.1 KB
/
main.user.js
File metadata and controls
424 lines (343 loc) · 17.1 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// ==UserScript==
// @name GitHub Network Ninja
// @version 2.0
// @namespace https://github.com/maliayas
// @author Ali Ayas <mali@maliayas.com>
// @description Full-viewport graph with searchable commit list on "GitHub repo network" pages.
// @license MIT
// @include https://github.com/*/*/network
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAMAUExURQAAAB8hJR8iJiAiJiAjJyIkKCMlKSMmKSQmKiUnKyUoKyYoLCcqLSgqLSgrLiksLyosMCsuMCsuMSwuMCwuMS4wMy8xNC8xNTEyNjI1NzM1ODU4Oj0%2FQj5AQz9AREFCRUJDR0JER0dITEhJTElLTk9QU09RVFBRVFFSVVJUVlJUV1RWWFtcXltcX1xdYF1eYF5eYV5fYmFiZWNkZmRlZ2dpa2prbWprbmtsbmtsb2xucG5vcW9wc3JydXt7fnx8fnx9f3x%2Bf35%2BgH5%2FgYCAg4GChIKDhYOEhoaGiIaHiYeIioiJi4mKi4qLjY6OkI6PkZCQkpGRk5GSk5GSlJWVl5aWmJqanJycnp6dn5%2Ben56foKGhoqOjpKOjpaOkpaeoqaurrK2sra2trq2ur66vsLCwsba0tbW0tra2t7i4uby7vLy8vcHBwsTCw8TDxMXExcXFxsbGx8rKysvKy83MzM7Mzc3NztTS0tXV1dXV1tfW19jX19zb293c3N7d3t%2Fe39%2Ff4ODe3uLg4OLh4ePi4uTi4uXj4%2BXk5Obl5efm5ujl5ejm5unn5%2Bro6Ovp6evq6uzq6uzr6%2B3s7O7t7e%2Fu7u%2Fu7%2FDu7vHv7%2FHw8PLx8fLy8vPz8%2FTz8%2FX09PX19fb29vf39%2Fj39%2Fj4%2BPn5%2Bfr6%2Bvv7%2B%2Fz8%2FP39%2Ff7%2B%2Fv%2F%2F%2FwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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==/UserScript==
var $canvasContainer = $(".js-network-graph-container");
if ($canvasContainer.length) {
// If there is a scrollbar, we should hide it in order to get the correct window width and height.
$("body").css({"overflow": "hidden"});
// Make the container fixed to 0x0 point.
$canvasContainer.removeClass("position-relative").css({
"position": "fixed",
"left": 0,
"right": 0,
"bottom": "-16px",
"top": 0,
"z-index": "1000",
"background-color": "white"
});
var $canvas = $canvasContainer.find("canvas");
var $win = $(window);
var winWidth = $win.width();
var winHeight = $win.height();
$canvas.attr("width", winWidth);
$canvas.attr("height", winHeight);
}
(function initCommitFilterBox() {
var config = {
/**
* Number of commits to fetch in each API request.
*/
"number_of_commits_per_request": 500,
/**
* Number of milliseconds to wait between API requests.
*/
"api_request_interval": 1000,
/**
* Number of maximum commits to fetch from the API.
*/
"number_of_max_commits": 10000
};
insertCss(
".commit-filter-box-mask {"
+ "width: 100%;"
+ "height: 100%;"
+ "position: fixed;"
+ "top: 0;"
+ "left: 0;"
+ "z-index: 1001;"
+ "background-color: #000000;"
+ "opacity: 0.5;"
+ "cursor: pointer;"
+ "}"
+ ".commit-filter-button {"
+ "position: fixed;"
+ "top: 5px;"
+ "right: 5px;"
+ "z-index: 1000;"
+ "}"
+ ".commit-filter-box {"
+ "padding: 20px;"
+ "border-radius: 10px;"
+ "position: fixed;"
+ "left: 10%;"
+ "right: 10%;"
+ "top: 10%;"
+ "bottom: 10%;"
+ "overflow: hidden;"
+ "z-index: 1002;"
+ "background-color: #eaf5ff;"
+ "}"
+ ".commit-filter-box input {"
+ "margin-bottom: 10px;"
+ "}"
+ ".commit-filter-box .table-container {"
+ "overflow: auto;"
+ "}"
+ ".commit-filter-box .loading {"
+ "position: relative;"
+ "top: 25%;"
+ "color: #96a3ae;"
+ "text-align: center;"
+ "}"
+ ".commit-filter-box .loading .flash {"
+ "margin-top: 20px;"
+ "display: inline-block;"
+ "}"
+ ".commit-filter-box table {"
+ "width: 100%;"
+ "display: none;"
+ "}"
+ ".commit-filter-box .author {"
+ "white-space: nowrap;"
+ "}"
+ ".commit-filter-box .message {"
+ "padding-left: 10px;"
+ "}"
+ ".commit-filter-box table tr.non-merged .message {"
+ "font-weight: bold;"
+ "}"
+ ".commit-filter-box .date {"
+ "padding-right: 10px;"
+ "white-space: nowrap;"
+ "text-align: right;"
+ "}"
);
var $commitFilterBoxMask, $commitFilterButton, $commitFilterBox, $commitFilterInput, $commitFilterLoading, $commitFilterTable;
var numberOfTotalCommits = 0;
var repo = getCurrentRepo();
/**
* This is a hash string that's specific to the repo. It's required in order to
* make API request for list of commits. However, -to my knowledge- it's not
* possible to get it directly from somewhere. GitHub somehow creates/calculates
* it. So we'll simply steal it from GitHub's runtime. See below for more
* details.
*/
var nethash;
/*
* In order to find "nethash" value, we need to hook into GitHub's API requests.
* So we're creating a proxy "fetch()" method below. Whenever we find a request
* that includes the "nethash" in its URL we'll save it for future usage.
*/
var originalFetch = fetch;
fetch = function (input, init) {
var match;
if (typeof nethash === "undefined" && (match = /[?&]nethash=([^&]+)/.exec(input.url))) {
nethash = match[1];
}
// Just do what regular fetch() does.
return originalFetch(input, init);
};
/**
* Meta data related to axises of the network graph. See `fetchNetworkMetaData()`.
*/
var networkMetaData;
$commitFilterButton = $(
"<button class='commit-filter-button btn btn-sm btn-primary'>"
+ "List of commits"
+ "</button>").appendTo("body");
$commitFilterButton.click(showCommitFilterBox);
function showCommitFilterBox() {
if (typeof $commitFilterBox !== "undefined") {
// It's already created before.
$commitFilterBoxMask.show();
$commitFilterBox.show();
return;
}
$commitFilterBoxMask = $("<div class='commit-filter-box-mask'></div>").appendTo("body");
$commitFilterBoxMask.click(hideCommitFilterBox);
$commitFilterBox = $(
"<div class='commit-filter-box'>"
+ "<input type='text' class='form-control input-block' />"
+ "<div class='table-container'>"
+ "<div class='loading'>"
+ "<p class='h1'><span class='number'>0</span> commits fetched...</p>"
+ "<p class='h3'>Please wait until all the commits are fetched.</p>"
+ "<p class='h5'>(Limit is " + config.number_of_max_commits + " commits. You can configure it in the source.)</p>"
+ "</div>"
+ "<table></table>"
+ "</div>"
+ "</div>").appendTo("body");
// Adjust height dynamically based on the viewport height. 44 is the total
// height of the input box.
$commitFilterBox.find(".table-container").height($commitFilterBox.height() - 44);
$commitFilterInput = $commitFilterBox.find("input");
$commitFilterLoading = $commitFilterBox.find(".loading");
$commitFilterTable = $commitFilterBox.find("table");
bindFilterFunctionOnInput();
fetchNetworkMetaData();
// Close commit filter box on escape.
$(document).bind("keyup", function (e) {
if (e.which == 27) {
hideCommitFilterBox();
return false;
}
return true;
});
}
function hideCommitFilterBox() {
$commitFilterBox.hide();
$commitFilterBoxMask.hide();
$commitFilterInput.val("").trigger("input");
}
/**
* Returns "user/repo".
*/
function getCurrentRepo() {
return document.location.href.replace(/^https:\/\/(www\.)?github\.com\/([^/]+\/[^/]+)(\/.*)?$/, "$2");
}
function insertCss(css) {
var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = css;
document.head.appendChild(style);
}
var timeoutId;
function bindFilterFunctionOnInput() {
$commitFilterInput.on("input", function () {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(function () {
var query = $commitFilterInput.val().toLowerCase();
$commitFilterTable.find("tr").each(function () {
if (query === "" || this.getAttribute("search-text").includes(query)) {
this.removeAttribute("hidden");
} else {
this.setAttribute("hidden", "");
}
});
}, 1000);
});
}
function fetchNetworkMetaData(retry) {
if (typeof retry === "undefined") {
// Number of retries for failed requests.
retry = 3;
}
$.getJSON("https://github.com/" + repo + "/network/meta")
.done(function (response) {
networkMetaData = response;
fetchCommits();
}).fail(function () {
retry--;
if (! retry) {
$commitFilterLoading.append("<div class='flash flash-error'>Network meta data could not be fetched. You can refresh the page to retry.</div>");
return;
}
// Retry.
window.setTimeout(function () {
fetchNetworkMetaData(retry);
}, config.api_request_interval);
});
}
function fetchCommits(offset, retry) {
if (typeof offset === "undefined") {
offset = 0;
}
if (typeof retry === "undefined") {
// Number of retries for failed requests.
retry = 3;
}
/*
* "start" and "end" URL parameters works this way: "start=0&end=2" returns
* 3 items whose indexes are 0, 1 and 2. Hence the "-1" in the calculation
* of "end".
*/
$.getJSON("https://github.com/" + repo + "/network/chunk", {
"nethash" : nethash,
"start" : offset,
"end" : offset + config.number_of_commits_per_request - 1
}).done(function (response) {
numberOfTotalCommits += response.commits.length;
$commitFilterLoading.find(".number").text(numberOfTotalCommits)
// Template object.
var $tr = $("<tr><td class='author'></td><td class='message'></td><td class='date'></td></tr>");
for (var i = 0; i < response.commits.length; i++) {
var $newTr = $tr.clone();
var commitUrl = "https://github.com/" + getUserRepoBySpace(response.commits[i]["space"]) + "/commit/" + response.commits[i]["id"];
var $commitLink = $("<a target='_blank'></a>").attr("href", commitUrl).text(response.commits[i]["message"]);
// Check if the commit exists in the upstream repo.
if (getUserBySpace(response.commits[i]["space"]) != getUserBySpace(0)) {
$newTr.addClass("non-merged");
}
$newTr.find(".author") .text(response.commits[i]["login"] || "(" + response.commits[i]["author"] + ")"); // "login" may be empty sometimes.
$newTr.find(".message") .html($commitLink);
$newTr.find(".date") .text(response.commits[i]["date"]);
$commitFilterTable.prepend($newTr);
}
var fetchCompleted = false;
if (numberOfTotalCommits >= config.number_of_max_commits) {
// We've reached the limit (defined by us).
fetchCompleted = true;
}
if (response.commits.length < config.number_of_commits_per_request) {
// All the commits are fetched. Let's show them.
fetchCompleted = true;
}
if (fetchCompleted) {
$commitFilterLoading.hide();
$commitFilterTable.show();
/*
* Searching uses `innerText` and `innerText` requires elements to
* be visible in order to work. This is why we cache them in the
* beginnig when all the rows are visible.
*/
buildSearchIndex();
$commitFilterInput.attr("placeholder", "Search in " + numberOfTotalCommits + " commits from " + networkMetaData.users.length + " repos...")
$commitFilterInput.focus();
} else {
// There is more...
window.setTimeout(function () {
fetchCommits(offset + config.number_of_commits_per_request);
}, config.api_request_interval);
}
}).fail(function () {
retry--;
if (! retry) {
$commitFilterLoading.append("<div class='flash flash-error'>Fetch operation failed. You can refresh the page to retry.</div>");
return;
}
// Retry the same request.
window.setTimeout(function () {
fetchCommits(offset, retry);
}, config.api_request_interval);
});
}
function buildSearchIndex() {
$commitFilterTable.find("tr").each(function () {
this.setAttribute("search-text", this.innerText.toLowerCase().replace(/\s+/g, " "));
});
}
/**
* "space" values are numbers returned from the GitHub API. They represent index
* of the line which a commit's point resides on in the network graph. "0" is
* the top (horizontal) line, "1" is the one below it, and so on.
*/
function getUserBySpace(space) {
for (var i = networkMetaData.blocks.length - 1; i >= 0; i--) {
if (space < networkMetaData.blocks[i].start) {
continue;
}
return networkMetaData.blocks[i].name;
}
return "_user_not_found_";
}
function getUserRepo(user) {
for (var i = 0; i < networkMetaData.users.length; i++) {
if (networkMetaData.users[i].name == user) {
return networkMetaData.users[i].name + "/" + networkMetaData.users[i].repo;
}
}
return user + "/_repo_not_found_";
}
function getUserRepoBySpace(space) {
return getUserRepo(getUserBySpace(space));
}
})();