私信  •  关注

Murtada Altarouti

Murtada Altarouti 最近回复了
3 年前
回复了 Murtada Altarouti 创建的主题 » 有没有办法在Javascript中获取GitHub概要文件的贡献数?

我尝试了许多解决方案,并改进了其中一个,以获得以下最适合我的解决方案:

  1. 在Javascript中创建函数
function get_contribution() {
    profile_url = "https://cors-anywhere.herokuapp.com/https://github.com/users/USERNAME/contributions";

    var xhr = new XMLHttpRequest();
    xhr.responseType = 'document';

    xhr.open('GET', profile_url, true);
    xhr.onload = function () {
        if (this.status == 200) {
            var response = xhr.responseXML.getElementsByClassName('f4 text-normal mb-2')[0].innerText;
            // get only the numbers in response
            contribution = response.replace(/[^0-9]/g, '');
            
            // The number of contributions is now saved in the contribution variable
        }
    };
    xhr.send();
}
  • 将用户名更改为所需的GitHub用户名
  • 请注意,您必须使用“cors”,否则它将不起作用
  1. 现在,您可以在任何地方使用该函数,但在我的情况下,我将在页面加载时调用它,并将其设置在HTML中的某个位置:
onload = function(){
    get_contribution();
  }