From 6c2b2eba300cfaa1fdbc6c26031b758855a326e2 Mon Sep 17 00:00:00 2001 From: "Ilya Ig. Petrov" Date: Tue, 26 Jan 2016 21:47:09 +0500 Subject: [PATCH] live updates for date --- .../pages/choose-pac-provider/index.js | 188 ++++++++++-------- .../sync-pac-script-with-pac-provider.js | 11 +- 2 files changed, 108 insertions(+), 91 deletions(-) diff --git a/extensions/chromium/minimalistic-pac-setter/extnesion/pages/choose-pac-provider/index.js b/extensions/chromium/minimalistic-pac-setter/extnesion/pages/choose-pac-provider/index.js index e35dac8..d369dd5 100755 --- a/extensions/chromium/minimalistic-pac-setter/extnesion/pages/choose-pac-provider/index.js +++ b/extensions/chromium/minimalistic-pac-setter/extnesion/pages/choose-pac-provider/index.js @@ -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 = ' [обновить]'; - 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 = ' [обновить]'; + 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(); + + }); + +} diff --git a/extensions/chromium/minimalistic-pac-setter/extnesion/sync-pac-script-with-pac-provider.js b/extensions/chromium/minimalistic-pac-setter/extnesion/sync-pac-script-with-pac-provider.js index 0ee7f42..85002d3 100755 --- a/extensions/chromium/minimalistic-pac-setter/extnesion/sync-pac-script-with-pac-provider.js +++ b/extensions/chromium/minimalistic-pac-setter/extnesion/sync-pac-script-with-pac-provider.js @@ -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(); } }