使用 Javascript 获取用户的 Github 存储库列表

Use Javascript to get the list of a user's Github repositories

本文关键字:存储 列表 Github Javascript 获取 用户 使用      更新时间:2023-09-26

首先,感谢您的阅读。

我在 GitHub 上托管我当前的项目。使用GitHub页面,我托管我的个人博客,你可以在这里访问博客。

在博客上,我有一个页面专门介绍我目前正在从事的所有项目。基本上,我想通过查询 GitHub 自动显示我所有正在进行的项目的列表。

在谷歌搜索很多的时候,我发现这可以使用JavaScript来实现。我试过了,但它没有按预期工作。加载页面时,我只收到文本消息"在 GitHub 中查询存储库"。似乎什么也没发生。

我联系了GitHub维护者,他们友好地回答说,这种技术使用了过时的GitHub API。

由于我在 JavaScript 方面没有经验,任何人都可以帮助我修复它吗?

问候
罗兰。


这是我在 HTML 页面中使用的代码

<div id="opensource-projects"></div> 
<!-- JavaScript to load and display repos from GitHub -->
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/javascripts/git.js" type="text/javascript"></script>
<script type="text/javascript">
  $(function() {
    $("#opensource-projects").loadRepositories("Yonaba");
  });
  </script>

然后,在文件git.js 中,我有以下内容:

// http://aboutcode.net/2010/11/11/list-github-projects-using-javascript.html
jQuery.githubUser = function(username, callback) {
  jQuery.getJSON("http://github.com/api/v1/json/" + username + "?callback=?", callback);
}
jQuery.fn.loadRepositories = function(username) {
  this.html("<span>Querying GitHub for " + username +"'s repositories...</span>");
  var target = this;
  $.githubUser(username, function(data) {
    var repos = data.user.repositories;
    sortByNumberOfWatchers(repos);
    var list = $('<dl/>');
    target.empty().append(list);
    $(repos).each(function() {
      list.append('<dt><a href="'+ this.url +'">' + this.name + '</a></dt>');
      list.append('<dd>' + this.description + '</dd>');
    });
  });
  function sortByNumberOfWatchers(repos) {
    repos.sort(function(a,b) {
      return b.watchers - a.watchers;
    });
  }
};

@jcolebrand:谢谢你的帮助,但我真的不明白你的意思。我也尝试向Chrome的控制台发送一些命令。我想$是jQuery的别名,不是吗?好吧,发送相同的东西控制台只是输出多个对象,描述我的存储库。棒!
我认为现在的问题在于正确解析它们并显示它们。哎呀,我需要为此学习JavaScipt语法...

您发布的脚本不起作用,因为 URL 适用于较旧的 API。将 URL 更改为此,它应该对您有用。

https://api.github.com/users/YOUR_USERNAME_HERE/repos?callback=CALLBACK

注意:callback=<YOUR_CALLBACK>是可选的。

http://developer.github.com/v3/对

如何做到这一点非常明确。事实上,由于我的用户名和这里的用户名相同,让我向您展示。

我在 github 上打开了我的存储库页面,https://github.com/jcolebrand(所以到目前为止这很明显)并在 Chrome 中按 F12。

我询问了jQuery确实安装了,因为我喜欢做示例时的快捷方式。

然后,正如它所说,我完全从 API 页面测试了$.getJSON('//api.github.com/users/jcolebrand/repos',{},function(data){console.log(data)}),你瞧,我获得了我自己看到的五个存储库。

以下是我没有做的事情:我没有获得 API 密钥,我没有通过 API 工作,我使用了我现有的凭据。请记住这些事情,但这就是改进它的方法。

你也可以使用github API lib。这个库是我最喜欢的 https://github.com/michael/github

使用XMLHttpRequest JQuery快捷方式扩展到@JColebrand的答案,$.getJson(),这是一个在 2020 年有效的 API 调用。

    user = 'tik9'
        apirepo = `https://api.github.com/users/${user}`
        
   listrepos = document.createElement('ul')
        document.getElementById('github').appendChild(listrepos)
        $.getJSON(apirepo + '/repos', function (data) {
            console.log('data now', data)
            function compare(a, b) {
                if (a.watchers > b.watchers) {
                    return -1
                }
                if (a.watchers < b.watchers) {
                    return 1
                }
                return 0
            }
            data.sort(compare)
            data.forEach(v => {
                listItemRepo = document.createElement('li')
                listrepos.appendChild(listItemRepo)
                hlink = document.createElement('a')
                listItemRepo.appendChild(hlink)
                hlink.textContent = `${v.description} | Stars: ${v.watchers}`
                hlink.href = v.html_url
            })
        })
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<h4><span id=repocount></span> Github Repositories</h4>
        <div id=github></div>