社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Murtada Altarouti  »  全部回复
回复总数  1
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();
  }