0

我有我的清单文件,其中包含我的版本号和一个选项页面。有没有办法在选项页面上显示已安装版本和最新可用版本而无需手动执行?

4

2 回答 2

3

您可以使用chrome.runtime.getManifest().version.

为了获得您的扩展的“最新版本”,您需要下载updates.xml并提取版本号:

var extensionID = chrome.i18n.getMessage('@@extension_id');
var currentVersion = chrome.runtime.getManifest().version;
var url = 'https://clients2.google.com/service/update2/crx?x=id%3D' + extensionID + '%26v%3D' + currentVersion;

var x = new XMLHttpRequest();
x.open('GET', url);
x.onload = function() {
    var doc = new DOMParser().parseFromString(x.responseText, 'text/xml');
    // Get and show version info. Exercise for the reader.
};
x.send();
于 2013-05-22T11:10:13.680 回答
0

如果你想用 PHP 自定义你的请求,避免每次谷歌更改 API 时更新扩展,我建议如下

update_info.php(来自您的网站):

<?php

    $last_ver;
    $googleupdate   = 'http://clients2.google.com/service/update2/crx?response=updatecheck&x=id%3D__id__%26uc';
    $ver            = $_POST['ver'];
    $id             = $_POST['id'];

    //filter/control for post request very fast for this example
    if(isset($id) && isset($ver)){
        if(strlen($id)>0){
            $urlupdate = str_replace('__id__', $id, $googleupdate);
            $last_ver  = _GetLastVersion($urlupdate);
            if($last_ver>0 && $last_ver>$ver){
                echo 'New version available, v'.$last_ver;
            }else{
                echo 'Your version is update';
            }
        }else{
            echo 'Insert id for response';
        }
    }else{
        echo 'Insert data for response';
    }

    //if your server do not connect with file_get_contents() use this function
    function getContent ($url) {

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

            $output = curl_exec($ch);
            $info = curl_getinfo($ch, CURLINFO_HTTP_CODE);

            curl_close($ch);
            if ($output === false || $info != 200) {
              $output = null;
            }
            return $output;

    }

    //this function return 0 if error or not content load
    function _GetLastVersion($url){     
        try {
            $last=0;
            //if you have proble you can use getContent($url)
            $xmlcontent = file_get_contents($url);
            if($xmlcontent){
                $doc = new DOMDocument();
                $doc->loadXML($xmlcontent);
                $items = $doc->getElementsByTagName('updatecheck');
                foreach ($items as $item) {
                    if($item->getAttribute('version')>0){
                        return $item->getAttribute('version');
                    }
                }
                return $last;
            }else{
                return 0;
            }

        } catch (Exception $e) {
            return 0;
        }       
    }
?>

在您的扩展程序中向您的网页发送请求,现在您可以控制您的脚本,您可以决定每次返回哪个答案

于 2013-05-31T17:23:00.510 回答