-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
237 lines (180 loc) · 6.82 KB
/
script.js
File metadata and controls
237 lines (180 loc) · 6.82 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
// Grapping Elements
// Input Section Elements
let inputSection = document.getElementById("input-card");
let githubURL = document.getElementById("github-url");
let branchSelect = document.getElementById("select-branch");
let otherBranch = document.getElementById("custom-branch");
let fetchBtn = document.getElementById("fetchBtn");
// Output Section Elements
let outputSection = document.getElementById("output-card");
let searchBar = document.getElementById("search");
let detailCont = document.getElementById("details");
let output = document.getElementById("result");
let urlSection = document.querySelector("#overlay");
// Hide by default
otherBranch.style.display = 'none';
outputSection.style.display = 'none';
urlSection.style.display = 'none';
// Logic for custom input field display
branchSelect.addEventListener('change', function () {
if (this.value === 'other') {
otherBranch.style.display = 'inline-block';
otherBranch.focus();
} else {
otherBranch.style.display = 'none';
otherBranch.value = ''; // Clear input if 'other' is not selected
}
});
// Function to get branch name
function getSelectedValue() {
if (branchSelect.value === 'other') {
return otherBranch.value;
} else {
return branchSelect.value;
}
}
// Global Variables To Use Anywhere In Code
let searchArray = [];
let userName = "";
let repoName = "";
let branchName = "";
// Function to fetch data from API
const fetchData = async () => {
const repoLink = githubURL.value.trim();
// No longer required after making form
if (!repoLink) {
alert("Please enter a GitHub repo URL!");
return;
}
// Extract userName, repoName & branchName
let repoElements = repoLink.split("/");
let user = repoElements[repoElements.length - 2]
let repo = repoElements[repoElements.length - 1]
let branch = getSelectedValue();
userName = user;
repoName = repo;
branchName = branch;
inputSection.style.display = 'none';
outputSection.style.display = 'flex';
detailCont.innerHTML += `
<p style="color: #172554;">${userName}</p>
<p style="color: #172554;">${repoName}</p>
`
// Editing container on loading state
output.innerHTML = `<p style="color: white;">⏳ Fetching files...</p>`; // loading state
// Fetching data
try {
const res = await fetch(`https://api.github.com/repos/${userName}/${repoName}/git/trees/${branchName}?recursive=1`);
const data = await res.json();
if (!data.tree) {
output.innerHTML = `<p style="color: white;>⚠️ Could not fetch files. Check repo URL or branch name.</p>`;
return;
}
searchArray = data["tree"];
output.innerHTML = ""
for (let i = 0; i < searchArray.length; i++) {
let path = searchArray[i].path;
output.innerHTML += `
<p style="color: white;">
<button
style="background: none; border: none; cursor: pointer; font-size: 20px;"
onclick="getURL('${path}')">🔗</button>
${path}
</p>
`
}
}
catch (error) {
output.innerHTML = `<p>❌ Error while fetching data</p>`;
}
}
// Calling Main Function
fetchBtn.addEventListener("click", fetchData);
// Debouncing + Fragment For Efficient DOM Behaviour
let debounceTimer;
searchBar.addEventListener("input", function () {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(runSearch, 300);
});
function runSearch() {
const query = searchBar.value.toLowerCase();
output.innerHTML = "";
const fragment = document.createDocumentFragment(); // efficient DOM building
for (let i = 0; i < searchArray.length; i++) {
let path = searchArray[i].path;
if (path.toLowerCase().includes(query)) {
let p = document.createElement("p");
p.style.color = "white";
p.innerHTML = `
<button
style="background:none;border:none;cursor:pointer;font-size:20px;"
onclick="getURL('${path}')">🔗</button>
${path}
`;
fragment.appendChild(p);
}
}
output.appendChild(fragment);
}
// Function For Full Path
const getURL = (path) => {
urlSection.style.display = 'flex';
correctPath = path.replaceAll(" ", "%20");
let generatedURL = `https://github.com/${userName}/${repoName}/blob/${branchName}/${correctPath}`;
urlSection.innerHTML = `
<div class="url">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" onclick="closeCont()"
xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Close"
style="position: fixed; right: 10px; top: 10px; cursor: pointer;">
<path d="M6 6L18 18M6 18L18 6" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
</svg>
<h2>Full Path</h2>
<div class="path-detail">
<p class="url-animate">
https://github.com/${userName}/${repoName}/blob/${branchName}/${correctPath}
</p>
<button class="copy-btn" onclick="copy('${generatedURL}')">
<svg width="20" height="20" viewBox="0 0 16 16" fill="none"
xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Copy">
<rect x="3.5" y="3.5" width="8" height="8" rx="1" stroke="currentColor" stroke-width="1.2" fill="none"/>
<rect x="5.5" y="1.5" width="8" height="8" rx="1" stroke="currentColor" stroke-width="1.2" fill="none"/>
</svg>
</button>
</div>
</div>
`
};
// To close URL container
function closeCont() {
urlSection.style.display = "none";
}
// Function For Copy Text Functionality
const copy = (URL) => {
// Check If The Clipboard Api Is Available
if (navigator.clipboard) {
// Copy The Text Inside The Text Field
navigator.clipboard.writeText(URL)
.then(() => {
// Alert The Copied Text
alert("Text Copied!!");
})
.catch((error) => {
alert("Failed to copy text. Please try again.");
});
}
else {
// Fallback for older browsers and mobile
const textArea = document.createElement("textarea");
textArea.value = URL;
document.body.appendChild(textArea);
textArea.select();
textArea.setSelectionRange(0, 99999); // For mobile devices
try {
document.execCommand("copy");
alert("Text Copied!!");
} catch (error) {
alert("Failed to copy text. Please try again.");
}
document.body.removeChild(textArea);
}
};