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

后端 未结 4 1830
迷失自我
迷失自我 2021-02-04 19:58

First of all, thanks for reading.

I am hosting my current projects on GitHub. Using GitHub Pages, I ]host my personal blog, you can reach the blog here.

On the b

相关标签:
4条回答
  • 2021-02-04 20:45

    you can use a github API lib too. This lib is my favourite https://github.com/michael/github

    0 讨论(0)
  • 2021-02-04 20:52

    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=CALLBACK
    

    Note: callback=<YOUR_CALLBACK> is optional.

    0 讨论(0)
  • 2021-02-04 20:53

    Extending to @JColebrand's answer with the JQuery shortcut to XMLHttpRequest, $.getJson() , here is an API Call that works in 2020.

        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>

    0 讨论(0)
  • 2021-02-04 21:05

    http://developer.github.com/v3/ is pretty explicit on how to do this. In fact, since my username there and here are the same, let me show you.

    I opened my repo page on github, https://github.com/jcolebrand (so this is evident so far) and pressed F12 in Chrome.

    I interrogated to see that jQuery is indeed installed, because I like shortcuts when I'm doing examples.

    I then tested $.getJSON('//api.github.com/users/jcolebrand/repos',{},function(data){console.log(data)}) exactly from the API page, as it says, and lo and behold, I was granted the five repos I see for myself.

    Here are the things I have not done: I did not acquire an API key, I did not work via API, and I used my existing credentials. Keep those things in mind, but that's how to improve it going forward.

    0 讨论(0)
提交回复
热议问题