【问题标题】:Download link for latest release of GitHub projectGitHub 项目最新版本的下载链接
【发布时间】:2015-03-29 23:39:49
【问题描述】:

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

我发现了几个关于这个问题的问题,answers using 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

标签: javascript html ajax github github-api


【解决方案1】:

您正在加载一个 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>

【讨论】:

  • 我无法让它工作;我使用您的功能和链接,然后单击链接并重定向到同一站点。当我右键单击然后“保存链接地址”时,它会保存我的网站地址。
  • 在函数中使用console.log() 来检测downloadURL 同时检查控制台并确保那里没有任何错误。
  • 不太确定如何...如果我在控制台中输入mongodb-download,它会返回具有该 ID 的标签。如果我输入GetLatestReleaseInfo(),我会得到ReferenceError: $ is not defined。我必须在我的 html 文件的哪个部分调用GetLatestReleaseInfo()
  • 如果你得到$ is not defined,这意味着你没有加载你的jquery库或者在GetLatestReleaseInfo()之后加载它
  • 现在我正在尝试使用 &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"&gt;&lt;/script&gt; 加载 jquery 库。在控制台中输入 GetLatestReleaseInfo() 会给我一个未捕获的错误和一个类型错误 undefined is not a function
【解决方案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 即可!

【讨论】:

    【解决方案3】:
    <!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>
    

    这对我有用,希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2014-02-21
      • 2015-09-03
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多