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) { 'use strict';
var status = document.querySelector('#status');
if (msg) {
status.classList.remove('off');
status.innerHTML = msg;
} else
status.classList.add('off');
}
chrome.runtime.getBackgroundPage( backgroundPage => { renderPage();
var antiCensorRu = backgroundPage.antiCensorRu; function renderPage() {
console.log('Rendering started.');
// DATE function setStatusTo(msg) {
var status = document.querySelector('#status');
var dateForUser = 'неизвестно'; if (msg) {
if( antiCensorRu.lastPacUpdateStamp ) { status.classList.remove('off');
var diff = Date.now() - antiCensorRu.lastPacUpdateStamp; status.innerHTML = msg;
var units = ' мс' } else
var gauges = [ status.classList.add('off');
[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 + ' назад';
} }
var dateElement = document.querySelector('.update-date'); chrome.runtime.getBackgroundPage( backgroundPage => {
dateElement.innerText = dateForUser;
dateElement.title = new Date(antiCensorRu.lastPacUpdateStamp).toLocaleString('ru-RU');
// CLOSE var antiCensorRu = backgroundPage.antiCensorRu;
document.querySelector('.close-button').onclick = () => window.close(); // SET DATE
// RADIOS function setDate() {
var dateForUser = '...';
var currentRadio = () => { if( antiCensorRu.lastPacUpdateStamp ) {
var id = antiCensorRu.currentPacProviderKey || 'none'; var diff = Date.now() - antiCensorRu.lastPacUpdateStamp;
return document.querySelector('#'+id); var units = ' мс';
} var gauges = [
var checkChosenProvider = () => { [1000, ' с'],
var radio = currentRadio(); [60, ' мин'],
radio.checked = true; [60, ' ч'],
} [24, ' дн'],
var triggerChosenProvider = () => { [7, ' недель'],
var event = document.createEvent('HTMLEvents'); [4, ' месяцев'],
event.initEvent('change', false, true); [12, ' г']
currentRadio().dispatchEvent(event); ];
} for(var g of gauges) {
var diffy = Math.floor(diff / g[0]);
var ul = document.querySelector('#list-of-providers'); if (!diffy)
var _firstChild = ul.firstChild; break;
for( var providerKey of Object.keys(antiCensorRu.pacProviders) ) { diff = diffy;
var li = document.createElement('li'); var units = g[1];
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;}; dateForUser = diff + units + ' назад';
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(); var dateElement = document.querySelector('.update-date');
setStatusTo('Установка...'); dateElement.innerText = dateForUser;
antiCensorRu.installPac(pacKey, () => { dateElement.title = new Date(antiCensorRu.lastPacUpdateStamp).toLocaleString('ru-RU');
setStatusTo('PAC-скрипт установлен.');
enableDisableInputs();
});
} }
}
setStatusTo(''); setDate();
checkChosenProvider(); chrome.storage.onChanged.addListener( setDate );
if (antiCensorRu.ifNotInstalled)
triggerChosenProvider();
}); // 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] }, get pacProvider() { return this.pacProviders[this.currentPacProviderKey] },
ifNotInstalled: true, /*
Offer PAC choice if this is the first time extension was installed.
*/
ifFirstInstall: true,
// PROTECTED // PROTECTED
@ -60,7 +63,7 @@ window.antiCensorRu = {
if (Object.getOwnPropertyDescriptor(this, key).writable && typeof(this[key]) !== 'function') if (Object.getOwnPropertyDescriptor(this, key).writable && typeof(this[key]) !== 'function')
onlySettable[key] = this[key] 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) { pullFromStorage(cb) {
@ -85,7 +88,7 @@ window.antiCensorRu = {
updatePacProxyIps( updatePacProxyIps(
this.pacProvider, this.pacProvider,
() => { () => {
this.ifNotInstalled = false; this.ifFirstInstall = false;
this.pushToStorage(cb) this.pushToStorage(cb)
} }
)} )}
@ -167,7 +170,7 @@ chrome.runtime.onInstalled.addListener( details => {
//window.antiCensorRu.installPac(); //window.antiCensorRu.installPac();
break; break;
case 'install': case 'install':
window.antiCensorRu.ifNotInstalled = true; window.antiCensorRu.ifFirstInstall = true;
chrome.runtime.openOptionsPage(); chrome.runtime.openOptionsPage();
} }
} }