如何使用js从Github存储库创建文件数组?



我正在制作一个带有HTML的网站,我想创建一个带有midi文件的表,我已经把它放在Github存储库中。为此,我只是想象自己将所有这些文件插入到一个数组中,然后根据数组中的文件生成表。将文件的名称存储在数组中,然后根据名称查找文件也可能有效。

但问题是,我不知道如何获得所有文件(或他们的名字)位于一个文件夹(这是在一个Github存储库)。这似乎是一件非常简单的事情,但我已经搜索了一段时间,发现没有任何帮助。

所以,有人知道我如何创建一个数组与Github存储库与js文件?

查看github 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。

最新更新