下载最新版本的 GitHub 项目的链接

Download link for latest release of GitHub project

本文关键字:项目 链接 GitHub 下载 新版本 最新版      更新时间:2023-09-26

我正在尝试向我的网站添加指向项目最新 github 版本的下载链接。例如,链接 https://github.com/mongodb/mongo/archive/r3.0.0-rc7.zip 确实链接到最新版本(截至今天),但我不想在网站上对版本号进行硬编码。

我发现了几个关于这个问题的问题,使用 curl、ajax 或 php 来回答。

我尝试了使用github发布API的ajax的解决方案:

<!DOCTYPE html>
<HTML> <BODY>
<script language="javascript" type="text/javascript">   
    $(document).ready(function () { 
        GetLatestReleaseInfo();   
    });   
    function GetLatestReleaseInfo() {
      $.getJSON("https://github.com/mongodb/mongo/releases").done(function (json) {
         var release = json[0];
         var asset = release.assets[0];
         var downloadURL = "https://github.com/mongodb/mongo/releases" + release.tag_name + "/" + asset.name;
         $(".mongodb-download").attr("href", downloadURL);   
      });    
    } 
</script>
<a href="GetLatestReleaseInfo();">Link</a> 
<a href="" onclick="location.href=this.href+downloadURL;return false;">Link2</a> 
<a href="" onclick="location.href=this.href+mongodb-download;return false;">Link3</a>
</BODY>
</HTML>

但我无法正确调用 javascript 函数,正如我在上面的尝试(链接、链接 2 和 Link3)中所说的那样。我对javascript或ajax不是很熟悉,所以我不胜感激任何帮助;也许没有阿贾克斯有更简单的方法?

您正在加载一个 html 页面而不是他们的 REST API。获取标记的正确网址是 https://api.github.com/repos/mongodb/mongo/tags

你可能想在这里阅读更多关于 github api 的信息 - https://developer.github.com/v3/repos/

你的html可能看起来像这样:

<!DOCTYPE html>
<HTML> <BODY>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">  
$(document).ready(function () {
     GetLatestReleaseInfo();  
});  

function GetLatestReleaseInfo() {
   $.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (json) {
        var release = json[0];
        var downloadURL = release.zipball_url;
        $("#mongodb-download").attr("href", downloadURL);  
   });    
}  
</script>
<a id='mongodb-download' href="">Download latest mongo</a>
</BODY>
</HTML>
对我来说

,这很好用:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script>
            $(document).ready (function () {
                $.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (data) {
                    $ ('#mongodb-download').attr ('href', data[0].zipball_url); 
                })
            });
        </script>
    </head>
    <body>
        <a id="mongodb-download">Download the latest version of MongoDB</a>
    </body>
</html>

如果您遇到未定义的问题,只需将$更改为jQuery,它应该都可以工作!

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script>
            jQuery(document).ready (function () {
                jQuery.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (data) {
                    jQuery('#mongodb-download').attr ('href', data[0].zipball_url); 
                })
            });
        </script>
    </head>
    <body>
        <a id="mongodb-download">Download the latest version of MongoDB</a>
    </body>
</html>

这对我有用,希望对您有所帮助!