【问题标题】:Programatically Get the Latest Version Number of Firefox以编程方式获取 Firefox 的最新版本号
【发布时间】:2015-08-25 00:01:11
【问题描述】:

如何以编程方式解析 Firefox 的版本号。

所以,我不必每次都访问该页面。 我所要做的就是运行脚本,它会给我最新的版本。

http://download.cdn.mozilla.net/pub/mozilla.org/firefox/releases/latest/update/win32/en-US/

该文件将始终包含“.complete.mar”。它是该目录下唯一带有“完成”字样的文件。 如何从中解析版本“40.0.2”。

【问题讨论】:

    标签: firefox updates mozilla auto-update


    【解决方案1】:

    下载最新版本

    简单的答案是Mozilla Release Engineering 已经提供了一种下载最新版本的方法。见https://ftp.mozilla.org/pub/firefox/releases/latest/README.txt

    例如,我想下载最新的 Linux 64 位美国英文版 Firefox。所以我会:

    curl -Lo firefox.tar.bz2 'https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=en-US'
    tar -xjf firefox.tar.bz2
    cd firefox
    ./firefox --version
    

    请注意,这些是稳定版本,而不是 RC 或 nightly。对于那些看到release notes in the appropriate subfolder

    注意事项:

    • curl 命令 URL 用单引号 (') 括起来,以避免 bash 解释与符号 (&)。
    • 您可能希望将下载的 Firefox 添加到 $PATH(或在 Windows 中为 %PATH%)环境变量的开头。

    获取最新发布版本号

    要在不下载存档的情况下获取最新版本号,您可以使用 HTTP HEAD 方法(curl -I 选项)。例如,

    curl -fI 'https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=en-US' | grep -o 'firefox-[0-9.]\+[0-9]'
    

    这将返回类似firefox-67.0.4

    【讨论】:

    • 在 windows 上如果您遇到 curl 问题,请尝试将链接周围的单引号 (') 替换为双引号 (")
    • 这很好,但是如果您想每天运行一个脚本并且只获取比您当前版本新的 tarball 怎么办?
    • 如果您使用 HTTP HEAD 方法,您可以在重定向中看到版本号。例如,curl -fI ...
    • 用一个例子更新了答案以获得最新的发布版本号。
    【解决方案2】:

    因为我必须知道许多应用程序的最新版本号,所以我创建了名为 vergrabber 的在线服务,它以 json 格式提供该信息。 您可以通过http://vergrabber.kingu.pl/vergrabber.json尝试这项免费服务

    【讨论】:

    • 这是一个很棒的工具,但我不愿意依赖未来可能会消失的服务。您能否详细说明您的服务如何获取这些信息?
    • 如果我的服务消失了(不太可能,因为它很便宜),您可以自己运行它 - 代码在 github 上,您也可以贡献:github.com/twkrol/vergrabber
    【解决方案3】:

    您将遇到问题,因为您要检查的数据不在同一个域中。

    但是,您可以使用类似 node webkit(现在为 nwjs)的东西来通过浏览器限制。

    1. 从以下链接开始为您的操作系统下载 nodewebkit 文件:

    http://nwjs.io/

    1. 提取内容。

    2. 下载 JQuery 并将其放在解压后的文件夹中(将文件重命名为 jquery.js)。

    3. 新建一个文本文件,添加如下内容并保存为package.json

    package.json 内容:

    {
      "main": "index.html",
      "name": "firefoxversion",
      "version": "1",
      "window": {
        "title": "latest firefox version",
        "icon": "link.png",
        "toolbar": true,
        "width": 800,
        "height":600
       }
    }
    

    创建文件名index.html并保存以下内容:

    index.html 内容:

    <html>
        <head>
            <title>Latest Firefox Version</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
        </head>
        <body>
    
            <div id="result"></div>
    
    
    
    
            <script type="text/javascript" src="jquery.js"></script>
            <script type="text/javascript" src="main.js"></script>
        </body>
    </html>
    
    1. 接下来创建一个名为 main.js 的文件并保存以下内容:

    main.js 内容:

    var url ="http://download.cdn.mozilla.net/pub/mozilla.org/firefox/releases/latest/update/win32/en-US/";
    
    
    var version;
    
    $.get(url,function(data){//begin function
    
    
    $(data).contents().find("a").each(function(){//begin each function
    
    
    //create an array to hold the hmtl
    var html = [];
    
    
    if($(this).attr("href").indexOf("complete.mar" !== -1 )){//begin if then
    
    
    version = $(this).attr("href").split(".c");
    
    
    //start building your html to output
    html.push("Download the latest Firefox Version " + version[0] + " below:<br>");
    
    //add the download button
    html.push("<input type ='button' id ='firefox-latest' value = 'Download Firefox'>");
    
    
    //display the html in the #result div
    $("#result").html(html.join(""));
    
    
    }//end if then
    
    
    });//end each function
    
    
    
    
    });//end function
    
    //on click event for #firefox-latest
    $(document).on("click","#firefox-latest",function(){//begin on click event
    
    //change the window location to the file for the latest firefox version
    window.location.href = url + version[0] + ".complete.mar";
    
    
    });//end on click event
    
    1. 最后点击之前解压的文件夹内的 nw.exe 图标 您应该会看到最新的 Firefox 版本号。

    【讨论】:

    • 现在,有没有办法使用该版本号并将其导出到文件中?或者使用解析后的版本并将其添加到 URL 的末尾,以将其用作下载文件的链接。目标是动态查找最新版本,然后下载最新的 .mar 文件。
    • 应该不是问题,我会处理并编辑我的答案
    • 尝试我的答案中提供的新 javascript 代码,让我知道它会产生什么结果。如果它不起作用,我明天可能有时间处理它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 2013-01-11
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多