You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.3 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const SDK = {
data: require("sdk/self").data,
XMLHttpRequest: require("sdk/net/xhr").XMLHttpRequest,
tabs: require("sdk/tabs"),
panel: require("sdk/panel"),
storage: require('sdk/preferences/service'),
pagemod: require("sdk/page-mod"),
timers: require("sdk/timers"),
winutils: require('sdk/window/utils'),
windows: require("sdk/windows").browserWindows,
unload: require('sdk/system/unload')
};
var toolbarBtn = require("./button.js");
var prefix = "extensions.informst.";
var optionsPage = SDK.data.url("options/options.html");
var popup = SDK.panel.Panel({
width: 570,
height: 130,
contentURL: SDK.data.url("popup/popup.html"),
contentScriptFile: [SDK.data.url("popup/jsonToDOM.js"), SDK.data.url("popup/popup.js")],
contentScriptWhen: "start"
});
popup.port.on("request_update_popup", function() {
updatePopup();
});
function updatePopup() {
popup.port.emit("update_popup", {
balance: balance,
tariffnow: tariffnow,
tariffthen: tariffthen,
tariffstatus: tariffstatus,
tariffprice: tariffprice,
iswarning: iswarning,
islowb: islowb,
islowt: islowt
});
}
function createButton() {
for (var win in SDK.winutils.windows(null, { includePrivate: true })) {
toolbarBtn.create(SDK.winutils.windows(null, { includePrivate: true })[win], {
label: 'Баланс "СТ"',
tooltiptext: 'Информер баланса "СетиТагила"',
image: SDK.data.url("icons/main16.png"),
imageLarge: SDK.data.url("icons/main32.png"),
stylesheet: SDK.data.url('button/button.css'),
stylesheet2: SDK.data.url('button/button2.css'),
panel: popup,
afterCustomization: function(){ updateButton(); }
});
}
}
SDK.windows.on('open', function(window) {
createButton();
updateButton();
});
SDK.tabs.on('open', function (tab) {
createButton();
updateButton();
});
var pageMod = SDK.pagemod.PageMod({
include: optionsPage,
contentScriptFile: SDK.data.url("options/options.js"),
contentScriptWhen: "start",
onAttach: startListening
});
function getKey(data) {
for (elem in data) return elem;
}
function startListening(worker) {
worker.port.on('save_setting', function(setting) {
var key = getKey(setting);
SDK.storage.set(prefix + key, setting[key]);
updateButton();
});
worker.port.on('request_read_settings', function() {
var settings = {};
var keys = SDK.storage.keys(prefix);
for (index in keys)
settings[keys[index].replace(prefix, "")] = SDK.storage.get(keys[index]);
worker.port.emit('read_settings', settings);
});
/*
worker.port.on('update_badge', function(setting) {
updateButton();
});
*/
}
// Main informer code
var checkPeriod = 5 * 60000;
var dataurl = 'http://portal.setitagila.ru/reminder.php?ver=informst_ff_1.2.0';
var iswarning = false;
var islowb = false;
var islowt = false;
//var date;
var balance = 0;
var tariffnow = '-';
var tariffthen = '-';
var tariffstatus = '-';
var tariffprice = '-';
var blinkTimer;
var mainLoop;
var blinkColor = '#000000';
function blink() {
if (blinkColor != '#EB3659')
blinkColor = '#EB3659';
else
blinkColor = SDK.storage.get(prefix + "bstyle_fcolor") || "#000000";
toolbarBtn.setBadge(parseFloat(balance.toFixed(1)).toString(), blinkColor, SDK.storage.get(prefix + "bstyle_bcolor") || "#f0ffff");
}
function updateButton() {
iswarning = false;
islowb = false;
islowt = false;
if (SDK.storage.get(prefix + "notify1") == true)
if (balance <= SDK.storage.get(prefix + "balanceRub")) {
islowb = true;
iswarning = true;
}
if (SDK.storage.get(prefix + "notify2") == true) {
dt = new Date();
dim = new Date(dt.getFullYear(), 1 + dt.getMonth(), 0).getDate();
if ((dim - dt.getDate()) <= SDK.storage.get(prefix + "balanceDay") & (balance - (dim - dt.getDate() + 1) * 8) <= tariffprice) {
islowt = true;
iswarning = true;
}
}
var newLabel = 'Текущий баланс: ' + balance + ' руб.' + "\r\n" +
'Текущий тариф: ' + tariffnow + ' (' + (tariffstatus == 'on' ? 'включен' : 'ОТКЛЮЧЕН') + ")\r\n" +
'Тариф в след. месяце: ' + tariffthen + "\r\n" +
'Стоимость тарифа в след. месяце: ' + tariffprice + ' руб.';
var popupHeight = 123;
if (islowb) popupHeight += 27;
if (islowt) popupHeight += 27;
popup.resize(570, popupHeight);
var fcolor, bcolor = null;
if (iswarning == true) {
if (islowb || islowt) newLabel += "\r\n";
if (islowb) newLabel += "\r\nУ Вас мало денег на счету!";
if (islowt) newLabel += "\r\nСредств не хватит на начало след. месяца!";
if (SDK.storage.get(prefix + "warn2") == 1) {
SDK.timers.clearInterval(blinkTimer);
blinkTimer = SDK.timers.setInterval(blink, 1000);
} else {
SDK.timers.clearInterval(blinkTimer);
fcolor = '#EB3659';
bcolor = SDK.storage.get(prefix + "bstyle_bcolor") || '#F0FFFF';
}
} else SDK.timers.clearInterval(blinkTimer);
if (fcolor != null && bcolor != null)
toolbarBtn.setBadge(parseFloat(balance.toFixed(1)).toString(), fcolor, bcolor);
else
toolbarBtn.setBadge(parseFloat(balance.toFixed(1)).toString(), SDK.storage.get(prefix + "bstyle_fcolor") || "#000000", SDK.storage.get(prefix + "bstyle_bcolor") || "#f0ffff");
toolbarBtn.setLabel(newLabel);
updatePopup();
}
function fetchData() {
var req = new SDK.XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == 4) {
SDK.timers.clearTimeout(reqTimeout);
var datatag = req.responseXML.getElementsByTagName('data')[0];
//date = datatag.getElementsByTagName('date')[0].textContent;
balance = parseFloat(datatag.getElementsByTagName('balance')[0].textContent);
tariffnow = datatag.getElementsByTagName('tariffnow')[0].textContent;
tariffthen = datatag.getElementsByTagName('tariffthen')[0].textContent;
tariffstatus = datatag.getElementsByTagName('tariffstatus')[0].textContent;
tariffprice = parseInt(datatag.getElementsByTagName('tariffprice')[0].textContent);
updateButton();
}
};
req.open('GET', dataurl + '&timerand=' + (new Date()).getTime(), true);
var reqTimeout = SDK.timers.setTimeout(timeoutFunc, 3000);
function timeoutFunc(){
req.abort();
}
req.send(null);
}
mainLoop = SDK.timers.setInterval(fetchData, checkPeriod);
createButton();
fetchData();
function remove() {
SDK.timers.clearInterval(mainLoop);
SDK.timers.clearInterval(blinkTimer);
}
SDK.unload.when(remove);