Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
148 lines (121 loc) · 3.86 KB
/
Copy pathmain.js
File metadata and controls
148 lines (121 loc) · 3.86 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
function get_base_url() {
const url = document.location.href;
let i = url.length-1;
let cnt = 0;
while(i >=0 ) {
if(url[i] === '/') cnt++;
i--;
if(cnt === 3) break;
}
let final_url = "";
let j = 0;
while(j <= i) {
final_url += url[j];
j++;
}
return final_url;
}
function get_branch() {
const branchName = document
.querySelector("#ref-picker-commits")
?.getAttribute("aria-label")
?.replace(/ branch$/, "");
return branchName;
}
function get_sha() {
const latestCommit = [...document.querySelectorAll(
'a[href*="/commit/"]'
)].find(a => /\/commit\/[a-f0-9]{40}/i.test(a.pathname));
const SHA = latestCommit?.pathname.match(
/\/commit\/([a-f0-9]{40})/i
)?.[1];
if(latestCommit === undefined) {
alert('cant find SHA open issue at: https://github.com/Anxhul10/github-root-commit/issues')
}
return SHA;
}
function get_url() {
const base_url = get_base_url();
const SHA = get_sha();
const branch = get_branch();
let total_commit = Number(sessionStorage.getItem("Anxhul10"+base_url+"-total-commits"));
if(!total_commit) {
total_commit = Number(prompt("enter repo total commits"));
}
const jump = total_commit-35+1;
if(jump < 0) {
alert(
"You're already on the root commit page: https://github.com/Anxhul10/github-root-commit/issues"
)
return null;
}
const URL = `${base_url}/commits/${branch}/?after=${SHA}+${jump}`
return URL;
}
function normalize_url(url) {
if(url == null) return null;
const url_split = url.split('/');
return url_split[0]+'//'+url_split[2]+'/'+url_split[3]+'/'+url_split[4];
}
function runner() {
// to make it work dynamically we need to know the number of commints to skip after the current commit SHA
const base_url = get_base_url();
const normal_url = normalize_url(base_url);
let res = sessionStorage.getItem("Anxhul10"+normal_url+"-oldest-url");
if(!res) {
const URL = get_url();
if(!URL) {
return document.location.href;
}
sessionStorage.setItem("Anxhul10"+normal_url+"-oldest-url", URL);
return URL;
}
return res;
}
function normalize_commit(total_commit) {
let commits = "";
for(let i = 0 ; i < total_commit.length; i++) {
if(total_commit[i] >= "0" && total_commit[i] <= "9") {
commits += total_commit[i];
}
}
return commits;
}
const observer = new MutationObserver(() => {
const root = document.location.href;
if(root.split("/").length === 5 && !sessionStorage.getItem("Anxhul10"+root+"-total-commits")) {
const cmtContainer = document.querySelector(
'a[href*="/commits/"]'
);
const total_commits = cmtContainer
?.querySelector('[data-component="text"] span')
?.textContent;
const commit = normalize_commit(total_commits);
sessionStorage.setItem("Anxhul10"+root+"-total-commits", commit);
return;
}
// document.body.style.border = "5px solid red";
const curr_div = document.querySelector(
".d-flex.flex-column.flex-sm-row.gap-2"
)
if(root.split('/')[5] === "commits") {
if(!curr_div)return;
if(document.getElementById("jumper-div")) return;
const main_div = document.createElement("div");
main_div.id = "jumper-div";
const btn = document.createElement("button");
btn.id = "btn-id";
btn.textContent = "Jump to oldest commits";
btn.addEventListener("click", function () {
location.href = runner();
})
btn.classList.add("button-40");
main_div.appendChild(btn);
curr_div.appendChild(main_div);
}
return;
})
observer.observe(document.body, {
childList: true,
subtree: true
});