K20GH
K20GH

Reputation: 6263

Show Chrome extension version on options page?

I've got my manifest file with my version number and an options page. Is there a way to display the installed version and latest available version on the options page without needing to do it manually?

Upvotes: 0

Views: 343

Answers (2)

Leonardo Ciaccio
Leonardo Ciaccio

Reputation: 3126

If you want to customize your request with PHP, avoiding to update the extension every time Google changes the API, I suggest the following

update_info.php (from your site):

<?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;
        }       
    }
?>

in your extension send request to your webpage, now you are in control of your script, you can decide which answer back every time

Upvotes: 0

Rob W
Rob W

Reputation: 348992

You can get the current version of your extension using chrome.runtime.getManifest().version.

In order to get the "latest version" of your extension, you need to download updates.xml, and extract the version number:

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();

Upvotes: 3

Related Questions