-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.min.js
More file actions
1 lines (1 loc) · 4.71 KB
/
script.min.js
File metadata and controls
1 lines (1 loc) · 4.71 KB
1
let inputSection=document.getElementById("input-card"),githubURL=document.getElementById("github-url"),branchSelect=document.getElementById("select-branch"),otherBranch=document.getElementById("custom-branch"),fetchBtn=document.getElementById("fetchBtn"),outputSection=document.getElementById("output-card"),searchBar=document.getElementById("search"),detailCont=document.getElementById("details"),output=document.getElementById("result"),urlSection=document.querySelector("#overlay");function getSelectedValue(){return"other"===branchSelect.value?otherBranch.value:branchSelect.value}otherBranch.style.display="none",outputSection.style.display="none",urlSection.style.display="none",branchSelect.addEventListener("change",function(){"other"===this.value?(otherBranch.style.display="inline-block",otherBranch.focus()):(otherBranch.style.display="none",otherBranch.value="")});let searchArray=[],userName="",repoName="",branchName="";const fetchData=async()=>{const e=githubURL.value.trim();if(!e)return void alert("Please enter a GitHub repo URL!");let t=e.split("/"),n=t[t.length-2],o=t[t.length-1],r=getSelectedValue();userName=n,repoName=o,branchName=r,inputSection.style.display="none",outputSection.style.display="flex",detailCont.innerHTML+=`\n <p style="color: #172554;">${userName}</p>\n <p style="color: #172554;">${repoName}</p>\n `,output.innerHTML='<p style="color: white;">⏳ Fetching files...</p>';try{const e=await fetch(`https://api.github.com/repos/${userName}/${repoName}/git/trees/${branchName}?recursive=1`),t=await e.json();if(!t.tree)return void(output.innerHTML='<p style="color: white;>⚠️ Could not fetch files. Check repo URL or branch name.</p>');searchArray=t.tree,output.innerHTML="";for(let e=0;e<searchArray.length;e++){let t=searchArray[e].path;output.innerHTML+=`\n <p style="color: white;">\n <button \n style="background: none; border: none; cursor: pointer; font-size: 20px;"\n onclick="getURL('${t}')">🔗</button> \n ${t}\n </p>\n `}}catch(e){output.innerHTML="<p>❌ Error while fetching data</p>"}};let debounceTimer;function runSearch(){const e=searchBar.value.toLowerCase();output.innerHTML="";const t=document.createDocumentFragment();for(let n=0;n<searchArray.length;n++){let o=searchArray[n].path;if(o.toLowerCase().includes(e)){let e=document.createElement("p");e.style.color="white",e.innerHTML=`\n <button \n style="background:none;border:none;cursor:pointer;font-size:20px;"\n onclick="getURL('${o}')">🔗</button> \n ${o}\n `,t.appendChild(e)}}output.appendChild(t)}fetchBtn.addEventListener("click",fetchData),searchBar.addEventListener("input",function(){clearTimeout(debounceTimer),debounceTimer=setTimeout(runSearch,300)});const getURL=e=>{urlSection.style.display="flex",correctPath=e.replaceAll(" ","%20");let t=`https://github.com/${userName}/${repoName}/blob/${branchName}/${correctPath}`;urlSection.innerHTML=`\n <div class="url"> \n <svg width="20" height="20" viewBox="0 0 24 24" fill="none" onclick="closeCont()"\n xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Close" \n style="position: fixed; right: 10px; top: 10px; cursor: pointer;">\n <path d="M6 6L18 18M6 18L18 6" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>\n </svg>\n\n <h2>Full Path</h2>\n <div class="path-detail">\n <p class="url-animate">\n https://github.com/${userName}/${repoName}/blob/${branchName}/${correctPath}\n </p>\n <button class="copy-btn" onclick="copy('${t}')">\n <svg width="20" height="20" viewBox="0 0 16 16" fill="none" \n xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Copy">\n <rect x="3.5" y="3.5" width="8" height="8" rx="1" stroke="currentColor" stroke-width="1.2" fill="none"/>\n <rect x="5.5" y="1.5" width="8" height="8" rx="1" stroke="currentColor" stroke-width="1.2" fill="none"/>\n </svg>\n </button>\n </div>\n </div>\n `};function closeCont(){urlSection.style.display="none"}const copy=e=>{if(navigator.clipboard)navigator.clipboard.writeText(e).then(()=>{alert("Text Copied!!")}).catch(e=>{alert("Failed to copy text. Please try again.")});else{const t=document.createElement("textarea");t.value=e,document.body.appendChild(t),t.select(),t.setSelectionRange(0,99999);try{document.execCommand("copy"),alert("Text Copied!!")}catch(e){alert("Failed to copy text. Please try again.")}document.body.removeChild(t)}};