Skip to content

Commit 7364480

Browse files
author
hoang.tran12
committed
udpate go to first commit - wip
1 parent 696cff3 commit 7364480

File tree

1 file changed

+60
-24
lines changed

1 file changed

+60
-24
lines changed

scripts/github_goToFirstCommit.js

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ export default {
1111

1212
whiteList: ["https://github.com/*"],
1313

14-
onClick: function () {
14+
onClick: async function () {
1515
// Source: https://github.com/FarhadG/init
16+
// Modified by HoangTran
1617

1718
let args = window.location.pathname.match(
1819
/\/([^\/]+\/[^\/]+)(?:\/tree\/([^\/]+))?/
@@ -25,39 +26,74 @@ export default {
2526
return;
2627
}
2728

28-
fetch(
29-
"https://api.github.com/repos/" +
30-
args[1] +
31-
"/commits?sha=" +
32-
(args[2] || "")
33-
)
29+
try {
30+
let res = await fetch(
31+
"https://api.github.com/repos/" +
32+
args[1] +
33+
"/commits?sha=" +
34+
(args[2] || "")
35+
);
3436
// the link header has additional urls for paging
3537
// parse the original JSON for the case where no other pages exist
36-
.then((res) => Promise.all([res.headers.get("link"), res.json()]))
38+
let results = await Promise.all([res.headers.get("link"), res.json()]);
39+
// results[0] is the link
40+
// results[1] is the first page of commits
3741

38-
// get last page of commits
39-
.then((results) => {
40-
// results[0] is the link
41-
// results[1] is the first page of commits
42+
let commits;
43+
if (results[0]) {
44+
// the link contains two urls in the form
45+
// <https://github.com/...>; rel=blah, <https://github.com/...>; rel=thelastpage
46+
// split the url out of the string
47+
let pageurl = results[0].split(",")[1].split(";")[0].slice(2, -1);
48+
let pageurlWithoutPage = pageurl.split("&page=")[0];
49+
let count = pageurl.match(/page=(.*?)$/)[1];
4250

43-
if (results[0]) {
44-
// the link contains two urls in the form
45-
// <https://github.com/...>; rel=blah, <https://github.com/...>; rel=thelastpage
46-
// split the url out of the string
47-
var pageurl = results[0].split(",")[1].split(";")[0].slice(2, -1);
48-
// fetch the last page
49-
return fetch(pageurl).then((res) => res.json());
51+
let pageNumber;
52+
while (true) {
53+
pageNumber = window.prompt(
54+
`Tìm thấy ${count} trang commits.\n` +
55+
`Mỗi trang 30 commits => ~${30 * count} commits.\n\n` +
56+
`Nhập số thứ tự trang muốn xem (1-${count}):\n` +
57+
`(Nhập ${count} để xem commit đầu tiên)`,
58+
1
59+
);
60+
if (pageNumber == null) return;
61+
62+
pageNumber = Number(pageNumber);
63+
if (pageNumber >= 1 && pageNumber <= count) break;
64+
else alert(`Lựa chọn không hợp lệ (1-${count}). Vui lòng chọn lại.`);
5065
}
5166

67+
// fetch the selected page
68+
res = await fetch(pageurlWithoutPage + "&page=" + (pageNumber || "1"));
69+
commits = await res.json();
70+
} else {
5271
// if no link, we know we're on the only page
53-
return results[1];
54-
})
72+
commits = results[1];
73+
}
5574

5675
// get the last commit and extract the url
57-
.then((commits) => commits.pop().html_url)
76+
while (true) {
77+
let commitIndex = prompt(
78+
`Tìm thấy ${commits.length} commits trong trang này.\n` +
79+
`Chọn vị trí commit muốn xem (1-${commits.length}):\n`,
80+
1
81+
);
82+
if (commitIndex == null) return;
5883

59-
// navigate there
60-
.then((url) => (window.location = url));
84+
let index = Number(commitIndex);
85+
if (index >= 1 && index <= commits.length) {
86+
window.open(commits[commits.length - index - 1].html_url);
87+
break;
88+
} else {
89+
alert(
90+
`Lựa chọn không hợp lệ (1-${commits.length}). Vui lòng chọn lại.`
91+
);
92+
}
93+
}
94+
} catch (e) {
95+
alert("ERROR: " + e);
96+
}
6197
},
6298
};
6399

0 commit comments

Comments
 (0)