|
| 1 | +import { getCurrentTab, showLoading } from "./helpers/utils.js"; |
| 2 | + |
1 | 3 | export default { |
2 | | - icon: `<i class="fa-solid fa-backward-fast fa-lg"></i>`, |
| 4 | + icon: `<i class="fa-solid fa-code-branch fa-lg"></i>`, |
3 | 5 | name: { |
4 | | - en: "Go to first commit", |
5 | | - vi: "Đi tới commit đầu tiên", |
| 6 | + en: "Go to any commit", |
| 7 | + vi: "Đi tới commit bất kỳ", |
6 | 8 | }, |
7 | 9 | description: { |
8 | | - en: "Go to first commit of github repo", |
9 | | - vi: "Đi tới commit đầu tiên của repo github", |
| 10 | + en: "Go to any commit of github repo. Included first commit.", |
| 11 | + vi: "Đi tới commit bất kỳ của repo github. Bao gồm cả commit đầu tiên.", |
10 | 12 | }, |
11 | 13 |
|
12 | | - whiteList: ["https://github.com/*"], |
13 | | - |
14 | | - onClick: async function () { |
15 | | - // Source: https://github.com/FarhadG/init |
16 | | - // Modified by HoangTran |
17 | | - |
18 | | - let args = window.location.pathname.match( |
19 | | - /\/([^\/]+\/[^\/]+)(?:\/tree\/([^\/]+))?/ |
20 | | - ); |
21 | | - // args[1] is the `orgname/repo` url fragment |
22 | | - // args[2] is the optional branch or hash |
23 | | - |
24 | | - if (!args || !args[1]) { |
25 | | - alert("Vui lòng mở 1 trang repo github rồi chạy lại chức năng."); |
26 | | - return; |
27 | | - } |
28 | | - |
| 14 | + onClickExtension: async () => { |
| 15 | + let { closeLoading, setLoadingText } = showLoading("Đang chờ nhập url..."); |
29 | 16 | try { |
30 | | - let res = await fetch( |
31 | | - "https://api.github.com/repos/" + |
32 | | - args[1] + |
33 | | - "/commits?sha=" + |
34 | | - (args[2] || "") |
35 | | - ); |
36 | | - // the link header has additional urls for paging |
37 | | - // parse the original JSON for the case where no other pages exist |
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 |
41 | | - |
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]; |
| 17 | + let tab = await getCurrentTab(); |
| 18 | + let url = prompt("Nhập link của github repo: ", tab.url); |
| 19 | + if (url == null) return; |
50 | 20 |
|
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; |
| 21 | + if (!url.includes("github.com")) |
| 22 | + throw Error( |
| 23 | + "Link không đúng định dạng.\nLink ví dụ: https://github.com/HoangTran0410/useful-script" |
| 24 | + ); |
61 | 25 |
|
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.`); |
65 | | - } |
| 26 | + let repoName = shared.getRepoNameFromUrl(url); |
| 27 | + let [user, repo] = repoName.split("/"); |
66 | 28 |
|
67 | | - // fetch the selected page |
68 | | - res = await fetch(pageurlWithoutPage + "&page=" + (pageNumber || "1")); |
69 | | - commits = await res.json(); |
70 | | - } else { |
71 | | - // if no link, we know we're on the only page |
72 | | - commits = results[1]; |
73 | | - } |
| 29 | + setLoadingText("Đang đếm số lượng commit..."); |
| 30 | + let totalCommits = await shared.getTotalCommitCount(user, repo); |
74 | 31 |
|
75 | | - // get the last commit and extract the url |
| 32 | + setLoadingText("Đang chờ lựa chọn..."); |
| 33 | + let index; |
76 | 34 | 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`, |
| 35 | + index = prompt( |
| 36 | + `Có ${totalCommits} commits trong '${repo}' của ${user} .\n\n` + |
| 37 | + `Nhập commit muốn xem (1-${totalCommits}):\n` + |
| 38 | + ` (1 là commit đầu tiên)`, |
80 | 39 | 1 |
81 | 40 | ); |
82 | | - if (commitIndex == null) return; |
83 | | - |
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 { |
| 41 | + if (index == null) return; |
| 42 | + if (index < 1 || index > totalCommits) |
89 | 43 | alert( |
90 | | - `Lựa chọn không hợp lệ (1-${commits.length}). Vui lòng chọn lại.` |
| 44 | + `Lựa chọn không hợp lệ (1-${totalCommits}). Vui lòng chọn lại.` |
91 | 45 | ); |
92 | | - } |
| 46 | + else break; |
93 | 47 | } |
| 48 | + |
| 49 | + setLoadingText("Đang tìm link commit..."); |
| 50 | + let firstCommit = await shared.getCommitAtIndex( |
| 51 | + user, |
| 52 | + repo, |
| 53 | + totalCommits - index + 1 |
| 54 | + ); |
| 55 | + |
| 56 | + if (firstCommit.html_url) window.open(firstCommit.html_url); |
| 57 | + else throw new Error("Không tìm thấy link."); |
94 | 58 | } catch (e) { |
95 | 59 | alert("ERROR: " + e); |
| 60 | + } finally { |
| 61 | + closeLoading(); |
96 | 62 | } |
97 | 63 | }, |
98 | 64 | }; |
99 | 65 |
|
100 | | -export function goToFirstCommit() {} |
| 66 | +export const shared = { |
| 67 | + // Idea from: https://github.com/FarhadG/init |
| 68 | + // https://github.com/HoangTran0410/useful-script => repoName = HoangTran0410/useful-script |
| 69 | + getRepoNameFromUrl(url) { |
| 70 | + let pathName = new URL(url).pathname; |
| 71 | + let [repoName, branchOrHash] = |
| 72 | + pathName.match(/\/([^\/]+\/[^\/]+)(?:\/tree\/([^\/]+))?/) || []; |
| 73 | + return repoName.slice(1); |
| 74 | + }, |
| 75 | + getTotalCommitCount: async (user, repo) => { |
| 76 | + let res = await fetch( |
| 77 | + `https://api.github.com/repos/${user}/${repo}/commits?per_page=1` |
| 78 | + ); |
| 79 | + let linkInHeader = res.headers.get("link"); |
| 80 | + let commitCount = linkInHeader.match(/&page=(\d+)>; rel="last"/)?.[1]; |
| 81 | + return Number(commitCount); |
| 82 | + }, |
| 83 | + getCommitAtIndex: async (user, repo, index) => { |
| 84 | + let res = await fetch( |
| 85 | + `https://api.github.com/repos/${user}/${repo}/commits?per_page=1&page=${index}` |
| 86 | + ); |
| 87 | + let json = await res.json(); |
| 88 | + return json[0]; |
| 89 | + }, |
| 90 | +}; |
0 commit comments