【hta版】獲取AppStore上架後的應用版本號

之前寫過一篇文章:獲取AppStore上架後的應用版本號,那一篇文章使用node.js實現,存在的問題就是如果在沒有安裝node.js運行環境下是無法運行的,而且該程序依賴request模塊,爲了方便其它人也能使用,想到把它做成一個本地應用程序。然後想了一下,覺得最簡單的就是使用hta文件(它的Ajax請求可跨域^_^)。

因爲我們手遊產品已經有三款了,所以“應用地址”那一欄,我使用了下拉框,其它組的成員只需要點擊選中需要檢測的應用,然後點擊“檢測版本”按鈕,程序將開始運行。當匹配到版本爲最新的版本時,登錄OA系統,向需要獲取版本更新信息的人員發送OA提醒。

原理比較簡單,代碼也並不複雜。將源碼本地另存爲.hta後綴的文件,然後雙擊它就可以運行了。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>檢測App最新版本號</title>
<hta:application id="fetchFIle" applicationname="fetchFIle" singleinstance="yes" border="thin" scroll="no" maximizeButton="no">
<style type='text/css'>
h3 {border-left:3px solid 666; color:#666; text-indent:10px; margin:15px 0;}
textarea {border:2px solid #888; background-color:#222; font-family:'Courier New'; font-size:14px; color:#3c0;}
a {color:#657528;}
a:hover {background:#c8dc7b;}

.exec-btn {border:1px solid #666; background-color:#9c0; color:#360; padding:5px 5px 3px 5px; margin-left:10px; width:70px; height:30px; vertical-align:middle; cursor:pointer; display:inline-block;}

body {background-color:#eee; margin:0; padding:0; overflow:hidden; text-align:center;}

#wrapper {width:800px; margin:30px auto; padding:30px; border:1px solid #ccc; text-align:left;}
.ipt {width:600px; height:25px; line-height:22px; vertical-align:middle; padding:0 2px;}
</style>
</head>
<body>
<div id="wrapper">
    <p>  應用的地址:<select id='ipt_url'>
        <option value="">【請選擇一款應用】</option>
        <option value="https://itunes.apple.com/cn/app/huang-di-jue-qi/id604615270?mt=8">【皇帝崛起】</option>
        <option value="https://itunes.apple.com/cn/app/feng-liu-tian-zi/id670188672?mt=8">【風流天子】</option>
        <option value="https://itunes.apple.com/cn/app/gong-ting-feng-yunhd/id543140000?mt=8">【宮廷風雲】</option>
    </select></p>
    <p>應用的最新版本:<input type='text' class='ipt' value="1.0.2" style="width:100px;" id="ipt_ver"/></p>
    <p> 輪詢間隔時長:<input type='text' class='ipt' value='3' style='width:100px;' id='ipt_duration'/>秒</p>
    <p>需要通知的人員:<textarea style='width:600px; height:140px;' id="ipt_uids">zhangyi</textarea></p>
    <p style='padding-left:118px;'><button class='exec-btn' onclick="startCheck()" id='btn_check'>檢測版本</button><span style='padding-left:350px;color:#666;'>多人請使用回車進行分隔</span></p>
</div>

<div style='border:1px solid #ccc; width:800px; margin:30px auto; text-align:left; padding:10px;'>
    <p>應用名稱:<span id='app_name'></span></p>
    <p>當前版本:<span id='curr_ver'></span></p>
</div>

<div style='position:absolute; bottom:10px; right:30px;'>&copy;版本所有:<a href="http://www.cnblogs.com/meteoric_cry" target='_blank'>Meteoric_cry</a></div>

<script type="text/javascript">
String.prototype.trim = function(r){
    return this.replace(r || /(^\s+)|(\s+$)/g, "");
}

function getEl(id) {
    return typeof id == 'string' ? document.getElementById(id) : id;
}

var FWKAjax = function () {
    return {
        getXHR: function () {
            var e = null;
            try {
                return (e = new XMLHttpRequest());
            } catch (d) {
                for (var c = 0, b = ["MSXML3", "MSXML2", "Microsoft"]; c < b.length; c++) {
                    try {
                        e = new ActiveXObject(b[c] + ".XMLHTTP");
                        break;
                    } catch (d) {}
                }
            }
            return e;
        },
        request: function (b, c) {
            var d = this.getXHR();
            if (!d) {
                throw new Error("cant't initialize xhr instance.");
            }
            var a = {};
            a.method = (c.method || "get").toUpperCase();
            a.asyn = true;
            a.onSuccess = c.onSuccess || function () {};
            a.onFailure = c.onFailure || function () {};
            a.postData = c.postData || null;
            d.open(a.method, b, a.asyn);

            if ("POST" == a.method) {
                d.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            } else {
                d.setRequestHeader('If-Modified-Since', new Date(0).toGMTString());
                d.setRequestHeader('Cache-Control', 'no-cache');
            }

            d.onreadystatechange = function () {
                if (d.readyState == 4) {
                    if (d.status == 0 || d.status == 200) {
                        a.onSuccess(d);
                    } else {
                        a.onFailure(d);
                    }

                    d = null;
                    a = null;
                }
            };

            d.send(a.postData);
        }
    };
}();


function startCheck() {
    var url = getEl('ipt_url').value.trim()
    var ver = getEl('ipt_ver').value.trim()

    if (!url) {
        alert('請選擇要檢測的應用');
        getEl('ipt_url').focus();
        return false;
    }

    if (!ver) {
        alert('請輸入要檢測的應用版本號');
        return false;
    }

    var duration = getEl('ipt_duration').value * 1000 || 3 * 1000;
    var uids = getEl('ipt_uids').value.trim().replace(/\r\n/g, ',');

    if (!uids) {
        alert("請輸入要通知的人員名單列表");
        return ;
    }

    var btnElem = getEl('btn_check');
    btnElem.setAttribute("disabled", "disabled");
    btnElem.innerHTML = "正在檢測";

    checkHandler(url, ver, duration, uids)
}

function checkHandler(url, ver, duration, uids) {
    FWKAjax.request(url + "&t=" + new Date().getTime(), {
        'method' : 'get',
        'onSuccess' : ajaxCallback,
        'onFailure' : ajaxCallback,
        'postData' : null
    });

    function ajaxCallback(xhr) {
        var htmlContent = xhr.responseText;
        xhr = null;

        if (/<h1>([^<]+)<\/h1>/.test(htmlContent)) {
            var appName = RegExp["$1"];
            getEl('app_name').innerHTML = appName;
        }

        if (/\<li\><span class=\"label\">版本\: <\/span>([^<]+)\<\/li\>/.test(htmlContent)) {
            var currVer = RegExp["$1"];

            getEl('curr_ver').innerHTML = currVer;

            var appDomainName = url.split('/app/')[1].split('/')[0];

            if (currVer == ver) {
                sendOA_Notification(appDomainName, currVer, uids);
                return ;
            }
        }

        setTimeout(function() {
            checkHandler(url, ver, duration, uids)
        }, duration);
    }
}

function sendOA_Notification(appName, currVer, uids) {
    FWKAjax.request("http://oa.xx.net/logincheck.php?UNAME=xx1&PASSWORD=xx2", {
        'method' : 'GET',
        'onSuccess' : function(xhr) {
            if (/location=\"general\"\;/.test(xhr.responseText)) {
                sendMsg(appName, currVer, uids);
            }
         },
        'onFailure' : null,
        'postData' : null
    });
}

function sendMsg(appName, currVer, uids) {

    var data_str = "uid=" + uids + "&cont=[" + appName + "] AppStore Latest Version:" + currVer;

    FWKAjax.request("http://oa.xx.net/general/reservation/sendsmsapi.php", {
        'method' : 'post',
        'onSuccess' : function(xhr) {
            alert("版本更新提醒已發送!");

            var btnElem = getEl('btn_check');
            btnElem.removeAttribute("disabled");
            btnElem.innerHTML = "檢測版本";
         },
        'onFailure' : null,
        'postData' : data_str
    });
}


!(function() {
    var winsize = {
        winwidth: 900,
        winheight: 650,
        scrnwidth: screen.availWidth,
        scrnheight: screen.availHeight
    };

    window.resizeTo(winsize.winwidth, winsize.winheight);
})();

window.onerror=function(a,b,c){
    alert("檢測腳本發生了以下異常:"+a+"\n所在行:"+c);
    return true;
}

</script>

</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章