|
| 1 | +const div = document.querySelector('.github-topic-projects') |
| 2 | + |
| 3 | +async function getDataBatch(page) { |
| 4 | + const response = await fetch(`https://api.github.com/search/repositories?q=topic:fastapi&per_page=100&page=${page}`, { headers: { Accept: 'application/vnd.github.mercy-preview+json' } }) |
| 5 | + const data = await response.json() |
| 6 | + return data |
| 7 | +} |
| 8 | + |
| 9 | +async function getData() { |
| 10 | + let page = 1 |
| 11 | + let data = [] |
| 12 | + let dataBatch = await getDataBatch(page) |
| 13 | + data = data.concat(dataBatch.items) |
| 14 | + const totalCount = dataBatch.total_count |
| 15 | + while (data.length < totalCount) { |
| 16 | + page += 1 |
| 17 | + dataBatch = await getDataBatch(page) |
| 18 | + data = data.concat(dataBatch.items) |
| 19 | + } |
| 20 | + return data |
| 21 | +} |
| 22 | + |
| 23 | +function setupTermynal() { |
| 24 | + document.querySelectorAll(".use-termynal").forEach(node => { |
| 25 | + node.style.display = "block"; |
| 26 | + new Termynal(node, { |
| 27 | + lineDelay: 500 |
| 28 | + }); |
| 29 | + }); |
| 30 | + const progressLiteralStart = "---> 100%"; |
| 31 | + const promptLiteralStart = "$ "; |
| 32 | + const customPromptLiteralStart = "# "; |
| 33 | + const termynalActivateClass = "termy"; |
| 34 | + let termynals = []; |
| 35 | + |
| 36 | + function createTermynals() { |
| 37 | + document |
| 38 | + .querySelectorAll(`.${termynalActivateClass} .highlight`) |
| 39 | + .forEach(node => { |
| 40 | + const text = node.textContent; |
| 41 | + const lines = text.split("\n"); |
| 42 | + const useLines = []; |
| 43 | + let buffer = []; |
| 44 | + function saveBuffer() { |
| 45 | + if (buffer.length) { |
| 46 | + let isBlankSpace = true; |
| 47 | + buffer.forEach(line => { |
| 48 | + if (line) { |
| 49 | + isBlankSpace = false; |
| 50 | + } |
| 51 | + }); |
| 52 | + dataValue = {}; |
| 53 | + if (isBlankSpace) { |
| 54 | + dataValue["delay"] = 0; |
| 55 | + } |
| 56 | + if (buffer[buffer.length - 1] === "") { |
| 57 | + // A last single <br> won't have effect |
| 58 | + // so put an additional one |
| 59 | + buffer.push(""); |
| 60 | + } |
| 61 | + const bufferValue = buffer.join("<br>"); |
| 62 | + dataValue["value"] = bufferValue; |
| 63 | + useLines.push(dataValue); |
| 64 | + buffer = []; |
| 65 | + } |
| 66 | + } |
| 67 | + for (let line of lines) { |
| 68 | + if (line === progressLiteralStart) { |
| 69 | + saveBuffer(); |
| 70 | + useLines.push({ |
| 71 | + type: "progress" |
| 72 | + }); |
| 73 | + } else if (line.startsWith(promptLiteralStart)) { |
| 74 | + saveBuffer(); |
| 75 | + const value = line.replace(promptLiteralStart, "").trimEnd(); |
| 76 | + useLines.push({ |
| 77 | + type: "input", |
| 78 | + value: value |
| 79 | + }); |
| 80 | + } else if (line.startsWith("// ")) { |
| 81 | + saveBuffer(); |
| 82 | + const value = "💬 " + line.replace("// ", "").trimEnd(); |
| 83 | + useLines.push({ |
| 84 | + value: value, |
| 85 | + class: "termynal-comment", |
| 86 | + delay: 0 |
| 87 | + }); |
| 88 | + } else if (line.startsWith(customPromptLiteralStart)) { |
| 89 | + saveBuffer(); |
| 90 | + const promptStart = line.indexOf(promptLiteralStart); |
| 91 | + if (promptStart === -1) { |
| 92 | + console.error("Custom prompt found but no end delimiter", line) |
| 93 | + } |
| 94 | + const prompt = line.slice(0, promptStart).replace(customPromptLiteralStart, "") |
| 95 | + let value = line.slice(promptStart + promptLiteralStart.length); |
| 96 | + useLines.push({ |
| 97 | + type: "input", |
| 98 | + value: value, |
| 99 | + prompt: prompt |
| 100 | + }); |
| 101 | + } else { |
| 102 | + buffer.push(line); |
| 103 | + } |
| 104 | + } |
| 105 | + saveBuffer(); |
| 106 | + const div = document.createElement("div"); |
| 107 | + node.replaceWith(div); |
| 108 | + const termynal = new Termynal(div, { |
| 109 | + lineData: useLines, |
| 110 | + noInit: true, |
| 111 | + lineDelay: 500 |
| 112 | + }); |
| 113 | + termynals.push(termynal); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + function loadVisibleTermynals() { |
| 118 | + termynals = termynals.filter(termynal => { |
| 119 | + if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) { |
| 120 | + termynal.init(); |
| 121 | + return false; |
| 122 | + } |
| 123 | + return true; |
| 124 | + }); |
| 125 | + } |
| 126 | + window.addEventListener("scroll", loadVisibleTermynals); |
| 127 | + createTermynals(); |
| 128 | + loadVisibleTermynals(); |
| 129 | +} |
| 130 | + |
| 131 | +function shuffle(array) { |
| 132 | + var currentIndex = array.length, temporaryValue, randomIndex; |
| 133 | + while (0 !== currentIndex) { |
| 134 | + randomIndex = Math.floor(Math.random() * currentIndex); |
| 135 | + currentIndex -= 1; |
| 136 | + temporaryValue = array[currentIndex]; |
| 137 | + array[currentIndex] = array[randomIndex]; |
| 138 | + array[randomIndex] = temporaryValue; |
| 139 | + } |
| 140 | + return array; |
| 141 | +} |
| 142 | + |
| 143 | +async function showRandomAnnouncement(groupId, timeInterval) { |
| 144 | + const announceFastAPI = document.getElementById(groupId); |
| 145 | + if (announceFastAPI) { |
| 146 | + let children = [].slice.call(announceFastAPI.children); |
| 147 | + children = shuffle(children) |
| 148 | + let index = 0 |
| 149 | + const announceRandom = () => { |
| 150 | + children.forEach((el, i) => {el.style.display = "none"}); |
| 151 | + children[index].style.display = "block" |
| 152 | + index = (index + 1) % children.length |
| 153 | + } |
| 154 | + announceRandom() |
| 155 | + setInterval(announceRandom, timeInterval |
| 156 | + ) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +async function main() { |
| 161 | + if (div) { |
| 162 | + data = await getData() |
| 163 | + div.innerHTML = '<ul></ul>' |
| 164 | + const ul = document.querySelector('.github-topic-projects ul') |
| 165 | + data.forEach(v => { |
| 166 | + if (v.full_name === 'tiangolo/fastapi') { |
| 167 | + return |
| 168 | + } |
| 169 | + const li = document.createElement('li') |
| 170 | + li.innerHTML = `<a href="${v.html_url}" target="_blank">★ ${v.stargazers_count} - ${v.full_name}</a> by <a href="${v.owner.html_url}" target="_blank">@${v.owner.login}</a>` |
| 171 | + ul.append(li) |
| 172 | + }) |
| 173 | + } |
| 174 | + |
| 175 | + setupTermynal(); |
| 176 | + showRandomAnnouncement('announce-left', 5000) |
| 177 | + showRandomAnnouncement('announce-right', 10000) |
| 178 | +} |
| 179 | + |
| 180 | +main() |
0 commit comments