live updates for date

This commit is contained in:
Ilya Ig. Petrov 2016-01-26 21:47:09 +05:00
parent a4b83d100c
commit 6c2b2eba30
2 changed files with 108 additions and 91 deletions

View File

@ -1,100 +1,114 @@
function setStatusTo(msg) {
var status = document.querySelector('#status');
if (msg) {
status.classList.remove('off');
status.innerHTML = msg;
} else
status.classList.add('off');
}
'use strict';
chrome.runtime.getBackgroundPage( backgroundPage => {
renderPage();
var antiCensorRu = backgroundPage.antiCensorRu;
function renderPage() {
console.log('Rendering started.');
// DATE
var dateForUser = 'неизвестно';
if( antiCensorRu.lastPacUpdateStamp ) {
var diff = Date.now() - antiCensorRu.lastPacUpdateStamp;
var units = ' мс'
var gauges = [
[1000, ' с'],
[60, ' мин'],
[60, ' ч'],
[24, ' дн'],
[7, ' недель'],
[4, ' месяцев'],
[12, ' г']
];
for(var g of gauges) {
var diffy = Math.floor(diff / g[0]);
if (!diffy)
break;
diff = diffy;
var units = g[1];
}
dateForUser = diff + units + ' назад';
function setStatusTo(msg) {
var status = document.querySelector('#status');
if (msg) {
status.classList.remove('off');
status.innerHTML = msg;
} else
status.classList.add('off');
}
var dateElement = document.querySelector('.update-date');
dateElement.innerText = dateForUser;
dateElement.title = new Date(antiCensorRu.lastPacUpdateStamp).toLocaleString('ru-RU');
chrome.runtime.getBackgroundPage( backgroundPage => {
// CLOSE
var antiCensorRu = backgroundPage.antiCensorRu;
document.querySelector('.close-button').onclick = () => window.close();
// SET DATE
// RADIOS
var currentRadio = () => {
var id = antiCensorRu.currentPacProviderKey || 'none';
return document.querySelector('#'+id);
}
var checkChosenProvider = () => {
var radio = currentRadio();
radio.checked = true;
}
var triggerChosenProvider = () => {
var event = document.createEvent('HTMLEvents');
event.initEvent('change', false, true);
currentRadio().dispatchEvent(event);
}
var ul = document.querySelector('#list-of-providers');
var _firstChild = ul.firstChild;
for( var providerKey of Object.keys(antiCensorRu.pacProviders) ) {
var li = document.createElement('li');
li.innerHTML = '<input type="radio" name="pacProvider" id="'+providerKey+'"> <label for="'+providerKey+'">'+providerKey+'</label> <a href class="link-button">[обновить]</a>';
li.querySelector('.link-button').onclick = () => {triggerChosenProvider(); return false;};
ul.insertBefore( li, _firstChild );
}
var radios = [].slice.apply( document.querySelectorAll('[name=pacProvider]') );
for(var radio of radios) {
radio.onchange = function(event) {
var pacKey = event.target.id;
if (pacKey === 'none')
return antiCensorRu.clearPac();
function enableDisableInputs() {
var inputs = document.querySelectorAll('input');
console.log(inputs.length);
for (var i = 0; i < inputs.length; i++)
inputs[i].disabled = !inputs[i].disabled;
function setDate() {
var dateForUser = '...';
if( antiCensorRu.lastPacUpdateStamp ) {
var diff = Date.now() - antiCensorRu.lastPacUpdateStamp;
var units = ' мс';
var gauges = [
[1000, ' с'],
[60, ' мин'],
[60, ' ч'],
[24, ' дн'],
[7, ' недель'],
[4, ' месяцев'],
[12, ' г']
];
for(var g of gauges) {
var diffy = Math.floor(diff / g[0]);
if (!diffy)
break;
diff = diffy;
var units = g[1];
}
dateForUser = diff + units + ' назад';
}
enableDisableInputs();
setStatusTo('Установка...');
antiCensorRu.installPac(pacKey, () => {
setStatusTo('PAC-скрипт установлен.');
enableDisableInputs();
});
var dateElement = document.querySelector('.update-date');
dateElement.innerText = dateForUser;
dateElement.title = new Date(antiCensorRu.lastPacUpdateStamp).toLocaleString('ru-RU');
}
}
setStatusTo('');
checkChosenProvider();
if (antiCensorRu.ifNotInstalled)
triggerChosenProvider();
setDate();
chrome.storage.onChanged.addListener( setDate );
});
// CLOSE BUTTON
document.querySelector('.close-button').onclick = () => window.close();
// RADIOS
var currentRadio = () => {
var id = antiCensorRu.currentPacProviderKey || 'none';
return document.querySelector('#'+id);
}
var checkChosenProvider = () => {
var radio = currentRadio();
radio.checked = true;
}
var triggerChosenProvider = () => {
var event = document.createEvent('HTMLEvents');
event.initEvent('change', false, true);
currentRadio().dispatchEvent(event);
}
var ul = document.querySelector('#list-of-providers');
var _firstChild = ul.firstChild;
for( var providerKey of Object.keys(antiCensorRu.pacProviders) ) {
var li = document.createElement('li');
li.innerHTML = '<input type="radio" name="pacProvider" id="'+providerKey+'"> <label for="'+providerKey+'">'+providerKey+'</label> <a href class="link-button">[обновить]</a>';
li.querySelector('.link-button').onclick = () => {triggerChosenProvider(); return false;};
ul.insertBefore( li, _firstChild );
}
var radios = [].slice.apply( document.querySelectorAll('[name=pacProvider]') );
for(var radio of radios) {
radio.onchange = function(event) {
var pacKey = event.target.id;
if (pacKey === 'none')
return antiCensorRu.clearPac();
function enableDisableInputs() {
var inputs = document.querySelectorAll('input');
console.log(inputs.length);
for (var i = 0; i < inputs.length; i++)
inputs[i].disabled = !inputs[i].disabled;
}
enableDisableInputs();
setStatusTo('Установка...');
antiCensorRu.installPac(pacKey, () => {
setStatusTo('PAC-скрипт установлен.');
enableDisableInputs();
});
}
}
setStatusTo('');
checkChosenProvider();
if (antiCensorRu.ifFirstInstall)
triggerChosenProvider();
});
}

View File

@ -49,7 +49,10 @@ window.antiCensorRu = {
get pacProvider() { return this.pacProviders[this.currentPacProviderKey] },
ifNotInstalled: true,
/*
Offer PAC choice if this is the first time extension was installed.
*/
ifFirstInstall: true,
// PROTECTED
@ -60,7 +63,7 @@ window.antiCensorRu = {
if (Object.getOwnPropertyDescriptor(this, key).writable && typeof(this[key]) !== 'function')
onlySettable[key] = this[key]
return chrome.storage.local.set(onlySettable, () => cb(chrome.runtime.lastError, onlySettable) );
return chrome.storage.local.set(onlySettable, () => cb && cb(chrome.runtime.lastError, onlySettable) );
},
pullFromStorage(cb) {
@ -85,7 +88,7 @@ window.antiCensorRu = {
updatePacProxyIps(
this.pacProvider,
() => {
this.ifNotInstalled = false;
this.ifFirstInstall = false;
this.pushToStorage(cb)
}
)}
@ -167,7 +170,7 @@ chrome.runtime.onInstalled.addListener( details => {
//window.antiCensorRu.installPac();
break;
case 'install':
window.antiCensorRu.ifNotInstalled = true;
window.antiCensorRu.ifFirstInstall = true;
chrome.runtime.openOptionsPage();
}
}