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

5

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

我发现了几个与此问题有关的问题,其中答案使用了curl,ajaxphp

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

<!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函数,就像在我的尝试中看到的那样(Link,Link2和Link3)。我对javascript或ajax不是很熟悉,因此我会感激任何帮助;也许有一种更简单的方法而不需要使用Ajax?


看起来像是xss - Nano
3个回答

10

你正在加载一个HTML页面而不是他们的REST API。获取标签的正确URL为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>

我无法让它工作;我使用了您的函数和链接,然后单击链接,却被重定向到同一网站。当我右键单击然后选择“保存链接地址”时,它会保存我的网站地址。 - user1981275
在函数中使用 console.log() 来检测 downloadURL,同时检查控制台确保没有任何错误。 - Gal Sisso
不太确定怎么做...如果我在控制台中输入 mongodb-download,它会返回该ID的标签。如果我输入 GetLatestReleaseInfo(),则会出现 ReferenceError: $ is not defined 的错误。我应该在HTML文件的哪个部分调用 GetLatestReleaseInfo() - user1981275
现在我正在尝试使用 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> 加载 jQuery 库。在控制台中输入 GetLatestReleaseInfo() 将会抛出未捕获的错误和类型错误 undefined is not a function - user1981275
我注意到一件事情:当我放置第二个指向相同位置的链接时,只有第一个链接有效。例如:在第一个链接后面放置“<a id='mongodb-download' href="">下载最新的mongo 2</a>”将不会在单击时执行任何操作。为什么会这样呢? - user1981275
显示剩余4条评论

2

对我而言,这个方法很有效:

<!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>

如果你遇到了undefined的问题,只需将$更改为jQuery,一切都应该能正常工作!


1
<!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>

这对我有效,希望能帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接