我正在用 HTML 制作一個網站,我想用我放在 Github 存盤庫中的 midi 檔案創建一個表格。為此,我只是想象自己將所有這些檔案插入到一個陣列中,然后根據陣列中的檔案生成表。將檔案的名稱存盤在陣列中,然后僅根據名稱查找檔案可能也有效。
但問題是我不知道如何獲取位于檔案夾(位于 Github 存盤庫中)中的所有檔案(或其名稱)。這感覺是一件非常簡單的事情,但我已經搜索了一段時間并沒有找到任何幫助。
那么,有人知道我如何使用 js 從 Github 存盤庫中創建一個包含檔案的陣列嗎?
uj5u.com熱心網友回復:
在查看了 GitHubs API 之后,我發現了一個可以讓我獲取存盤庫內容的東西!
const xhr = new XMLHttpRequest();
const url = "https://api.github.com/repos/-username-/-repo-/contents/:path"
// Replace -username- with your GitHub username, -repo- with the repository name, and then :path with a path to the file or folder you want to get the content of (leave blank to ge all files of the repository)
xhr.open('GET', URL, true);
xhr.onload = function() {
const data = JSON.parse(this.response);
console.log(data);
};
xhr.send();
API 呼叫回傳一個包含存盤庫內容的 JSON 字串,這對我來說是完美的。
這是 API 檔案的鏈接:https : //docs.github.com/en/rest/reference/repos#get-repository-content
這是一個幫助我理解 GitHub API 的網站:https : //codesnippet.io/github-api-tutorial/
我要感謝@user2190492建議使用 GitHub API。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313259.html
標籤:javascript html 数组 文件 github
