簡體   English   中英

如何使用 js 從 Github 存儲庫中創建包含文件的數組?

[英]How to create an array with files from a Github repository with js?

我正在用 HTML 制作一個網站,我想用我放在 Github 存儲庫中的 midi 文件創建一個表格。 為此,我只是想象自己將所有這些文件插入到一個數組中,然后根據數組中的文件生成表。 將文件的名稱存儲在數組中,然后僅根據名稱查找文件可能也有效。

但問題是我不知道如何獲取位於文件夾(位於 Github 存儲庫中)中的所有文件(或其名稱)。 這感覺是一件非常簡單的事情,但我已經搜索了一段時間並沒有找到任何幫助。

那么,有人知道我如何使用 js 從 Github 存儲庫中創建一個包含文件的數組嗎?

在查看了 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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM