Skip to content

Commit 63ad436

Browse files
committed
Sloppy sort
1 parent c816396 commit 63ad436

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ You should change the [script file](project/useful_forks.js) so that it includes
1111
The extension uses [content_scripts](https://developer.chrome.com/extensions/content_scripts#declaratively).
1212

1313
## To-do
14-
* Sort by Star if forks of forks were inserted
14+
* Sort by Star (a proper one!)
1515
* Cache responses for a little while (https://docs.github.com/en/free-pro-team@latest/rest/guides/getting-started-with-the-rest-api#conditional-requests)
1616
* Publish as a chrome extension
1717
* Allow people to input their Access Token to increase API limits (from Extension's settings rather than through script)

project/useful_forks.js

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,28 @@ function behind_badge(amount) {
5050
return '<svg xmlns="http://www.w3.org/2000/svg" width="92" height="18" role="img"><title>How far behind of the original repo\'s master branch this fork\'s master branch is</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="r"><rect width="92" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="47" height="18" fill="#555"/><rect x="47" width="45" height="18" fill="'+ color +'"/><rect width="92" height="18" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="245" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="370">behind</text><text x="245" y="130" transform="scale(.1)" fill="#fff" textLength="370">behind</text><text x="685" y="130" transform="scale(.1)" fill="#fff" textLength="' + badge_width(amount) + '">' + amount + '</text></g></svg>';
5151
}
5252

53-
function build_fork_element_html(table_body, combined_name, num_stars, num_watches, num_forks) {
54-
table_body.append(
55-
$('<tr>', {id: extract_username_from_fork(combined_name), class: "useful_forks_repo"}).append(
56-
$('<td>').html(svg_literal_fork + ' <a href=https://github.com/' + combined_name + '>' + combined_name + '</a>'),
57-
$('<td>').html(UF_TABLE_SEPARATOR),
58-
$('<td>').html(svg_literal_star + ' x ' + num_stars),
59-
$('<td>').html(UF_TABLE_SEPARATOR),
60-
$('<td>').html(svg_literal_eye + ' x ' + num_watches),
61-
$('<td>').html(UF_TABLE_SEPARATOR),
62-
$('<td>').html(svg_literal_fork + ' x ' + num_forks)
63-
)
64-
);
65-
}
66-
6753
function getElementById_$(id) {
6854
return $(toHtmlId(id));
6955
}
7056

57+
function getTdValue(rows, index, col) {
58+
return rows.item(index).getElementsByTagName('td').item(col).getAttribute("value");
59+
}
60+
61+
/** 'sortColumn' index starts at 0. https://stackoverflow.com/a/37814596/9768291 */
62+
function sortTable(table_id, sortColumn){
63+
let tableData = document.getElementById(table_id).getElementsByTagName('tbody').item(0);
64+
let rows = tableData.getElementsByTagName('tr');
65+
for(let i = 0; i < rows.length - 1; i++) {
66+
for(let j = 0; j < rows.length - (i + 1); j++) {
67+
if(getTdValue(rows, j, sortColumn) < getTdValue(rows, j+1, sortColumn)) {
68+
tableData.insertBefore(rows.item(j+1), rows.item(j));
69+
}
70+
}
71+
}
72+
}
73+
74+
/** The secondary request which appends the badges. */
7175
function commits_count(request, table_body, fork_username) {
7276
return () => {
7377
const response = JSON.parse(request.responseText);
@@ -89,12 +93,14 @@ function commits_count(request, table_body, fork_username) {
8993
}
9094
}
9195

96+
/** To remove erroneous repos. */
9297
function commits_count_failure(fork_username) {
9398
return () => {
9499
getElementById_$(fork_username).remove();
95100
}
96101
}
97102

103+
/** To use the Access Token with a request. */
98104
function authenticatedRequestHeaderFactory(url) {
99105
let request = new XMLHttpRequest();
100106
request.open('GET', url);
@@ -103,6 +109,37 @@ function authenticatedRequestHeaderFactory(url) {
103109
return request;
104110
}
105111

112+
function onreadystatechangeFactory(xhr, successFn, failureFn) {
113+
return () => {
114+
if (xhr.readyState === 4) {
115+
if (xhr.status === 200) {
116+
successFn();
117+
} else if (xhr.status === 403) {
118+
console.warn('Looks like the rate-limit was exceeded.');
119+
getElementById_$(UF_ID_MSG).html(UF_MSG_API_RATE);
120+
} else {
121+
console.warn('GitHub API returned status:', xhr.status);
122+
failureFn();
123+
}
124+
} else {
125+
// Request is still in progress
126+
}
127+
};
128+
}
129+
130+
/** Dynamically fills the second part of the rows. */
131+
function build_fork_element_html(table_body, combined_name, num_stars, num_watches, num_forks) {
132+
table_body.append(
133+
$('<tr>', {id: extract_username_from_fork(combined_name), class: "useful_forks_repo"}).append(
134+
$('<td>').html(svg_literal_fork + ' <a href=https://github.com/' + combined_name + '>' + combined_name + '</a>'),
135+
$('<td>').html(UF_TABLE_SEPARATOR + svg_literal_star + ' x ' + num_stars).attr("value", num_stars),
136+
$('<td>').html(UF_TABLE_SEPARATOR + svg_literal_eye + ' x ' + num_watches).attr("value", num_watches),
137+
$('<td>').html(UF_TABLE_SEPARATOR + svg_literal_fork + ' x ' + num_forks).attr("value", num_forks)
138+
)
139+
);
140+
}
141+
142+
/** Prepares, appends, and updates dynamically a table row. */
106143
function add_fork_elements(forkdata_array, user, repo) {
107144
if (!forkdata_array || forkdata_array.length === 0)
108145
return;
@@ -129,31 +166,6 @@ function add_fork_elements(forkdata_array, user, repo) {
129166
}
130167
}
131168

132-
function onreadystatechangeFactory(xhr, successFn, failureFn) {
133-
return () => {
134-
if (xhr.readyState === 4) {
135-
if (xhr.status === 200) {
136-
successFn();
137-
} else if (xhr.status === 403) {
138-
console.warn('Looks like the rate-limit was exceeded.');
139-
getElementById_$(UF_ID_MSG).html(UF_MSG_API_RATE);
140-
} else {
141-
console.warn('GitHub API returned status:', xhr.status);
142-
failureFn();
143-
}
144-
} else {
145-
// Request is still in progress
146-
}
147-
};
148-
}
149-
150-
function add_css() {
151-
let styleSheet = document.createElement('style');
152-
styleSheet.type = "text/css";
153-
styleSheet.innerText = additional_css_literal;
154-
document.head.appendChild(styleSheet);
155-
}
156-
157169
/** Paginated request. Pages index start at 1. */
158170
function request_fork_page(page_number, user, repo) {
159171
let request = authenticatedRequestHeaderFactory('https://api.github.com/repos/' + user + '/' + repo + '/forks?sort=stargazers&per_page=' + FORKS_PER_PAGE + '&page=' + page_number)
@@ -176,6 +188,11 @@ function request_fork_page(page_number, user, repo) {
176188
}
177189
}
178190

191+
/* Half-assed sort. (todo: redo this) */
192+
$( () => {
193+
sortTable(UF_ID_TABLE, 1);
194+
});
195+
179196
/* Populate the table. */
180197
add_fork_elements(response, user, repo);
181198
});
@@ -196,7 +213,14 @@ function prepare_display() {
196213
);
197214
}
198215

199-
/* Entry point. */
216+
function add_css() {
217+
let styleSheet = document.createElement('style');
218+
styleSheet.type = "text/css";
219+
styleSheet.innerText = additional_css_literal;
220+
document.head.appendChild(styleSheet);
221+
}
222+
223+
/** Entry point. */
200224
const pathComponents = window.location.pathname.split('/');
201225
if (pathComponents.length >= 3) {
202226
const user = pathComponents[1], repo = pathComponents[2];

0 commit comments

Comments
 (0)