使用Javascript获取用户的Github存储库列表 [英] Use Javascript to get the list of a user's Github repositories

查看:132
本文介绍了使用Javascript获取用户的Github存储库列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有的冷杉,感谢您的阅读。
我在Github托管当前的开发项目。
使用Github的页面,我使用Github托管我的个人博客,您可以访问此处
在那里,我有一个页面专用于我目前正在进行的所有项目。基本上,我想自动显示我所有正在进行的项目的列表,询问Github。
谷歌搜索了很多,我发现这可以使用Javascript实现
我试过了,但我没有按预期工作。当加载页面时,我只是收到短信'查询Github的存储库'。并没有任何事情发生。



我联系了Github维护人员,他们很好地回复说,这种技术使用了过时的Github API版本。
好​​吧,它似乎是v3。



由于我没有经历过JS,任何人都可以帮我解决它吗?



问候,

罗兰。






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

 < div id =opensource-projects>< / div> 
<! - Javascript从GitHub加载和显示回购站 - >
< script src =http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.jstype =text / javascript>< / script>
< script src =/ javascripts / git.jstype =text / javascript>< / script>
< script type =text / javascript>
$(function(){
$(#opensource-projects)。loadRepositories(Yonaba);
});
< / script>
<! - End GitHub repo code - >

然后,在文件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 =函数(用户名){
this.html(< span>查询GitHub for+ username +的存储库.. < /跨度>中);

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>');
});
});

函数sortByNumberOfWatchers(repos){
repos.sort(function(a,b){
return b.watchers - a.watchers;
});
}
};

@jcolebrand:感谢您的善意帮助,但我并没有真正了解您的意思。我也尝试向Chrome的控制台发送一些命令。我猜$是jQuery的别名,不是吗?好吧,发送相同的东西控制台只输出多个对象,描述我的回购。真棒。
我认为问题现在正确解析并显示它们。啧啧,我需要学习JS语法...

解决方案

您发布的脚本无法正常工作,因为url适用于较旧的API。将网址更改为此,它应该适用于您。

  https://api.github.com/users/YOUR_USERNAME_HERE/回购?回调=? 


Firs of all, thanks for reading. I am hosting my current dev projects at Github. Using Github's pages, I used Github to host my personal blog, that you can reach here There, I have a page dedicated to all the projects I am currently working on. Basically, I wanted to display the list of all my on-going projects automatically, querying Github. Googling a lot, I found this can be achieved using Javascript. I tried it, but i didn't work as expected. When loading the page, I just get the text message 'Querying Github for repositories'. And nothing seems to happen.

I contacted Github maintainers, and they kindly replied that this technique uses an outdated version of Github API. Well, it seems the v3.

As I am not experienced in JS, can anyone help me to fix it ?

Regards,
Roland.


Here is the code I used inside the HTML page

<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>
<!-- End GitHub repo code -->

Then, inside the file git.js, I have the following:

// 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 : Thanks for your kind help, but i didn't really get what you meant. I tried sending some command to Chrome's console, too. I guess $ is an alias for jQuery, isn't it ? well, sending same stuff The console just outputs multiple objects, describing my repos. Awesome. I think the issue is now parsing them properly and display them. Gee, I need to learn JS syntax for that ...

解决方案

The script that you posted isn't working because the url is for the older API. Change the url to this and it should work for you.

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

这篇关于使用Javascript获取用户的Github存储库列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆