【问题标题】:How to fetch Github folders outside of Github to display the files inside of a specific folder如何获取 Github 外部的 Github 文件夹以显示特定文件夹内的文件
【发布时间】:2020-09-02 19:58:02
【问题描述】:

我正在尝试获取 Github 存储库并显示特定文件夹“hacks”中的文件(有问题的存储库是 https://github.com/Prodigy-Hacking/ProdigyMathGameHacking)。我将如何在 Javascript 中执行此操作,而无需复制文件并不断更新它们? 提前致谢!

【问题讨论】:

  • 我不知道答案,但是API在这里:developer.github.com/v4
  • SO 不是一个让别人为你从头开始做某事的好地方。请编辑您的问题并与我们分享您到目前为止所做的事情。我建议请花点时间阅读How do I ask a good question?

标签: javascript api github dynamic fetch


【解决方案1】:

GitHub API REST Client(称为 v3)使用起来非常简单

GET https://api.github.com/repos/Prodigy-Hacking/ProdigyMathGameHacking

将为您提供您可以在该存储库中查询的所有内容,您将看到这是一个 contents 路径,因此请尝试使用该路径:

GET https://api.github.com/repos/Prodigy-Hacking/ProdigyMathGameHacking/contents/hacks

然后您可以遍历返回数组并获取每个文件,但是如果您确实需要获取特定文件的原始内容,那么假设对于这个示例,我们需要/hacks/Character/customName.js

GET https://api.github.com/repos/Prodigy-Hacking/ProdigyMathGameHacking/contents/hacks/Character/customName.js

{
  "name": "customName.js",
  "path": "hacks/Character/customName.js",
  "sha": "77b86151fbc3930d5f11e785333f82adbcc33ebf",
  "size": 118,
  "url": "https://api.github.com/repos/Prodigy-Hacking/ProdigyMathGameHacking/contents/hacks/Character/customName.js?ref=master",
  "html_url": "https://github.com/Prodigy-Hacking/ProdigyMathGameHacking/blob/master/hacks/Character/customName.js",
  "git_url": "https://api.github.com/repos/Prodigy-Hacking/ProdigyMathGameHacking/git/blobs/77b86151fbc3930d5f11e785333f82adbcc33ebf",
  "download_url": "https://raw.githubusercontent.com/Prodigy-Hacking/ProdigyMathGameHacking/master/hacks/Character/customName.js",
  "type": "file",
  "content": "Ly8gQ3VzdG9tIG5hbWUgKENsaWVudCBzaWRlIG9ubHkpLiAoUHV0IHRleHQg\naW4gdGV4dCBoZXJlLikKaGFjay5pbnN0YW5jZS5wcm9kaWd5LnBsYXllci5n\nZXROYW1lID0gKCkgPT4gIlRFWFQgSEVSRSI7Cg==\n",
  "encoding": "base64",
  "_links": {
    "self": "https://api.github.com/repos/Prodigy-Hacking/ProdigyMathGameHacking/contents/hacks/Character/customName.js?ref=master",
    "git": "https://api.github.com/repos/Prodigy-Hacking/ProdigyMathGameHacking/git/blobs/77b86151fbc3930d5f11e785333f82adbcc33ebf",
    "html": "https://github.com/Prodigy-Hacking/ProdigyMathGameHacking/blob/master/hacks/Character/customName.js"
  }
}

点击download_url会给你

// Custom name (Client side only). (Put text in text here.)
hack.instance.prodigy.player.getName = () => "TEXT HERE";

GitHub Documentation中解释得很好

您现在可以像...一样轻松地循环遍历数据...记住您应该authenticate your calls,否则您将达到 GitHub 限制。

【讨论】:

    猜你喜欢
    • 2018-02-05
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多