Files
ntut-com-tw/chipet/ndupdate/ndupdate.js
2018-02-01 19:48:42 +08:00

228 lines
6.7 KiB
JavaScript

var widgetName = NDgetKeyValue("Info.plist", "CFBundleName");
var location = 'http://novisdesign.net/WidgetResorces/Updates/ChiPet.txt';
var current = NDgetKeyValue("Info.plist", "CFBundleVersion");
var downloadPage = 'http://www.novisdesign.net/Widgets?chipet';
var checkDays = 5;
var newestVersion;
function NDupdate() {
if (window.widget) {
var lastDate = widget.preferenceForKey('lastDate');
if (lastDate != undefined) {
NDcheckAmount();
}
else {
NDsetTimeForNow();
}
}
}
var NDreq;
function NDcheck() {
NDreq = new XMLHttpRequest();
NDreq.onreadystatechange = NDcompare;
NDreq.open("GET", location, true);
NDreq.setRequestHeader("Cache-Control", "no-cache");
NDreq.send(null);
}
function NDcompare() {
if (NDreq.readyState == 4) {
if (NDreq.status == 200) {
var newNumber = NDreq.responseText;
if (newNumber > current) {
newestVersion = newNumber;
NDshow()
}
else {
NDaddTime()
}
}
}
}
var w = window.innerWidth;
var h = window.innerHeight;
var leftPx = Math.round(((w - 20) - 54) / 2)
var topPx = Math.round(((h - 30) - 36) / 2)
function NDshow() {
if (!document.getElementById('NDmain')) {
var newElm = document.createElement('div');
newElm.id = 'NDmain';
var title = document.createElement('div');
title.id = 'NDtitle';
title.innerHTML = 'Update Avaliable';
var update = document.createElement('div');
update.id = 'NDupdate';
update.innerHTML = widgetName + ' version ' + newestVersion + '<br/> <b>now available</b>';
var buttons = document.createElement('div');
buttons.id = 'NDbuttons';
var bElm = document.createElement('div');
bElm.id = 'NDbElm';
bElm.innerHTML = '<img src="ndupdate/Images/download.png" id="NDdownload" onclick="NDgoToDownlaod();NDaddTime();"/> <img src="ndupdate/Images/later.png" id="NDlater" onclick="NDaddTime()"/>';
buttons.appendChild(bElm);
var top = document.createElement('div');
top.id = 'NDtop';
var topFill = document.createElement('div');
topFill.id = 'NDtopFill';
var topLeft = document.createElement('div');
topLeft.id = 'NDtopLeft';
var topRight = document.createElement('div');
topRight.id = 'NDtopRight';
top.appendChild(topFill);
top.appendChild(topLeft);
top.appendChild(topRight);
var mid = document.createElement('div');
mid.id = 'NDmid';
var midLogo = document.createElement('div');
midLogo.id = 'NDmidLogo';
midLogo.style.left = leftPx + 'px';
midLogo.style.top = topPx + 'px';
var midTop = document.createElement('div');
midTop.id = 'NDmidTop';
midTop.style.height = topPx + 'px';
var midBottom = document.createElement('div');
midBottom.id = 'NDmidBottom';
midBottom.style.top = topPx + 36 + 'px';
var midLeft = document.createElement('div');
midLeft.id = 'NDmidLeft';
midLeft.style.top = topPx + 'px';
midLeft.style.width = leftPx + 'px';
var midRight = document.createElement('div');
midRight.id = 'NDmidRight';
midRight.style.top = topPx + 'px';
midRight.style.left = leftPx + 54 + 'px';
mid.appendChild(midLogo);
mid.appendChild(midTop);
mid.appendChild(midBottom);
mid.appendChild(midLeft);
mid.appendChild(midRight);
var bottom = document.createElement('div');
bottom.id = 'NDbottom';
var bottomFill = document.createElement('div');
bottomFill.id = 'NDbottomFill';
var bottomLeft = document.createElement('div');
bottomLeft.id = 'NDbottomLeft';
var bottomRight = document.createElement('div');
bottomRight.id = 'NDbottomRight';
bottom.appendChild(bottomFill);
bottom.appendChild(bottomLeft);
bottom.appendChild(bottomRight);
var gloss = document.createElement('div');
gloss.id = 'NDgloss';
var glossFill = document.createElement('div');
glossFill.id = 'NDglossFill';
var glossLeft = document.createElement('div');
glossLeft.id = 'NDglossLeft';
var glossRight = document.createElement('div');
glossRight.id = 'NDglossRight';
gloss.appendChild(glossFill);
gloss.appendChild(glossLeft);
gloss.appendChild(glossRight);
var shadow = document.createElement('div');
shadow.id = 'NDshadow';
var shadowBottom = document.createElement('div');
shadowBottom.id = 'NDshadowBottom';
var shadowBottomLeft = document.createElement('div');
shadowBottomLeft.id = 'NDshadowBottomLeft';
var shadowBottomFill = document.createElement('div');
shadowBottomFill.id = 'NDshadowBottomFill';
var shadowBottomRight = document.createElement('div');
shadowBottomRight.id = 'NDshadowBottomRight';
shadowBottom.appendChild(shadowBottomLeft)
shadowBottom.appendChild(shadowBottomFill)
shadowBottom.appendChild(shadowBottomRight)
var shadowRight = document.createElement('div');
shadowRight.id = 'NDshadowRight';
var shadowRightFill = document.createElement('div');
shadowRightFill.id = 'NDshadowRightFill';
var shadowTopRight = document.createElement('div');
shadowTopRight.id = 'NDshadowTopRight';
shadowRight.appendChild(shadowRightFill)
shadowRight.appendChild(shadowTopRight)
shadow.appendChild(shadowBottom)
shadow.appendChild(shadowRight)
newElm.appendChild(title);
newElm.appendChild(update);
newElm.appendChild(buttons);
newElm.appendChild(top);
newElm.appendChild(mid);
newElm.appendChild(bottom);
newElm.appendChild(gloss);
newElm.appendChild(shadow);
document.body.appendChild(newElm)
}
}
function NDgoToDownlaod() {
if (window.widget) {
widget.openURL(downloadPage);
}
}
function NDaddTime() {
if (window.widget) {
var d = new Date();
var mil = d.getTime();
widget.setPreferenceForKey(mil,'lastDate');
}
document.body.removeChild(document.getElementById('NDmain'));
}
function NDcheckAmount() {
if (window.widget) {
var d = new Date();
var lastDate = widget.preferenceForKey('lastDate');
var nowDate = d.getTime();
if ((nowDate - lastDate) > (86400000 * checkDays)) {
NDcheck();
}
}
}
function NDsetTimeForNow() {
if (window.widget) {
var d = new Date();
var mil = d.getTime();
widget.setPreferenceForKey(mil,'lastDate');
}
}
// Adapted from Jon Brown's Tutorial
function NDgetKeyValue(plist, key) {
var xml_http = new XMLHttpRequest();
xml_http.open("GET", plist, false);
xml_http.send(null);
var xml = xml_http.responseXML;
var key_value = null;
var nodes = xml.getElementsByTagName("dict")[0].childNodes;
for (var i=0; i < nodes.length; i++) {
if (nodes[i].nodeType == 1 && nodes[i].tagName.toLowerCase() == "key" && nodes[i].firstChild.data == key) {
if (nodes[i+2].tagName.toLowerCase() != "array") {
key_value = nodes[i+2].firstChild.data;
}
else {
key_value = new Array();
var ar_nodes = nodes[i+2].childNodes;
for (var j=0; j < ar_nodes.length; j++) {
if (ar_nodes[j].nodeType == 1)
key_value.push(ar_nodes[j].firstChild.data);
}
}
break;
}
}
return key_value;
}