From c5c1ac559e6ae47f16ad1d91a6b09cda6031242c Mon Sep 17 00:00:00 2001 From: "Ilya Ig. Petrov" Date: Mon, 30 Jan 2017 17:36:31 +0000 Subject: [PATCH] Fix exceptions UI (final), fix block-informer async bugs --- .../extension/14-ip-to-host-api.js | 36 ++++-- .../extension/15-pac-kitchen-api.js | 2 +- .../pages/choose-pac-provider/index.html | 2 +- .../pages/choose-pac-provider/index.js | 121 ++++++------------ 4 files changed, 70 insertions(+), 91 deletions(-) diff --git a/extensions/chromium/runet-censorship-bypass/extension/14-ip-to-host-api.js b/extensions/chromium/runet-censorship-bypass/extension/14-ip-to-host-api.js index c4f386a..708eef5 100644 --- a/extensions/chromium/runet-censorship-bypass/extension/14-ip-to-host-api.js +++ b/extensions/chromium/runet-censorship-bypass/extension/14-ip-to-host-api.js @@ -188,7 +188,7 @@ const self = window.apis.ipToHost = { - persistVoid() { + persist() { console.log('Persisting ipToHost...', privates); const ipToHost = {}; @@ -199,15 +199,16 @@ }, - resetToDefaultsVoid() { + resetToDefaults() { _state(ip2host, null); reinit(); }, - _purgeIpsForVoid(hostStr) { + _purgeOldIpsForSync(hostStr) { + console.log('Purging old IPs...'); for(const ip of Object.keys(privates._ipToHostObj)) { delete privates._ipToHostObj[ip]; } @@ -218,22 +219,25 @@ getIpsFor(hostStr, (err, ips, ...warns) => { - console.log('IPS', ips); + console.log('IPS', ips, err); if (!err) { - this._purgeIpsForVoid(hostStr); + this._purgeOldIpsForSync(hostStr); // Object may be shared, string can't. const hostObj = _getHostObj(hostStr); for(const ip of ips) { + console.log('IP', ip); privates._ipToHostObj[ip] = hostObj; + console.log(privates._ipToHostObj[ip], privates); } } + console.log('PP', privates); return cb(err, null, ...warns); }); }, - updateAllAsync(cb = mandatory()) { + _updateAllAsync(cb = mandatory()) { const hostArr = Object.keys(privates._strToHostObj); @@ -267,6 +271,20 @@ }, + + updateAllAsync(cb = mandatory()) { + + this._updateAllAsync((err, ...args) => { + + if (!err) { + this.persist(); + } + cb(err, ...args); + + }); + + }, + _replaceAllAsync(hostArr = mandatory(), cb) { if (typeof(hostArr) === 'function') { @@ -274,12 +292,12 @@ hostArr = Object.keys(privates._strToHostObj); } - this.resetToDefaultsVoid(); + this.resetToDefaults(); for(const hostStr of hostArr) { _createHostObj(hostStr); } - this.updateAllAsync(cb); + this._updateAllAsync(cb); }, @@ -296,7 +314,7 @@ this._replaceAllAsync(hostArr, (allErr, ...args) => { if (!allErr) { - this.persistVoid(); + this.persist(); } cb(allErr, ...args); diff --git a/extensions/chromium/runet-censorship-bypass/extension/15-pac-kitchen-api.js b/extensions/chromium/runet-censorship-bypass/extension/15-pac-kitchen-api.js index 77792a1..2c0ecfe 100644 --- a/extensions/chromium/runet-censorship-bypass/extension/15-pac-kitchen-api.js +++ b/extensions/chromium/runet-censorship-bypass/extension/15-pac-kitchen-api.js @@ -332,7 +332,7 @@ }, - resetToDefaultsVoid() { + resetToDefaults() { // Pruge all but exceptions. const exceptions = kitchenState(modsKey).exceptions; diff --git a/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.html b/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.html index 19a9354..a3346ec 100755 --- a/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.html +++ b/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.html @@ -351,7 +351,7 @@
Проксировать - ? + ?
    diff --git a/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.js b/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.js index 9350182..7f74a16 100755 --- a/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.js +++ b/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.js @@ -244,11 +244,6 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => } // EXCEPTIONS PANEL - /* - Iterating and modifying select.selectedOptions - at the same time is buggy, iterate this way instead: - [...select.selectedOptions] - */ { @@ -273,14 +268,12 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => }; - const excPrefix = '*.'; - const prefRe = new RegExp('^(\\s*\\*\\.?)?'); const ifProxyHtml = '✔'; const addOption = function addOption(host, ifProxy) { const opt = document.createElement('option'); - opt.value = excPrefix + host; + opt.value = host; opt.dataset.host = host; opt.innerHTML = ifProxy ? ifProxyHtml : '✘'; const editorHost = excEditor.value.trim(); @@ -292,17 +285,6 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => }; - const excList = document.getElementById('exc-list'); - const getExactOpts = (_host) => { - - const _nakedHost = _host.replace(prefRe, ''); - return [].filter.call( - excList.childNodes, - (opt) => opt.dataset.host === _nakedHost - ); - - }; - const thisYes = document.getElementById('this-yes'); const thisNo = document.getElementById('this-no'); const thisAuto = document.getElementById('this-auto'); @@ -319,88 +301,65 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => } + const hideOpt = (opt) => opt.value = '\n'; + const unhideOpt = (opt) => opt.value = opt.dataset.host + ' '; + + const excList = document.getElementById('exc-list'); + excEditor.onkeydown = function(event) { - console.log('DOWN', event); moveCursorIfNeeded(); - const start = this.selectionStart; - const end = this.selectionEnd; - if (start < 2 && event.key.length === 1) { - this.setSelectionRange(2, end < 2 ? 2 : end); - } if(event.key === 'Enter') { - // Hide all non-exact matches. - alert(12); + // Hide all. + excList.childNodes.forEach( hideOpt ); } return true; }; - excEditor.oninput = function(event) { + excEditor.onclick = excEditor.oninput = function(event) { - console.log('INPUT') - const _host = this.value; - const prefixedHost = this.value.replace(prefRe, excPrefix); const setInputValue = (newValue) => { + if (event && event.type === 'click') { + return; + } + // See bug in my comment to http://stackoverflow.com/a/32394157/521957 + // The only shortcoming: first click on empty input may be still ignored. const nu = this.selectionStart + newValue.length - this.value.length; this.value = newValue; excEditor.dataset.moveCursorTo = nu; window.setTimeout(moveCursorIfNeeded, 0); } - setInputValue(prefixedHost); - const _nakedHost = _host.trim().replace(prefRe, ''); + const host = this.value.trim() || ' '; + + setInputValue(host); thisAuto.checked = true; - const toDefault = (opt) => opt.value = excPrefix + opt.dataset.host;; - - const delim = ' | '; - let exactOpt = false; - const ifLineSelected = _host.includes(delim); + this.classList.remove(noClass, yesClass); excList.childNodes.forEach( (opt) => { - const ifExactMatch = opt.dataset.host === _nakedHost || opt.value === _host; - if (ifExactMatch) { - exactOpt = opt; - return; + const ifExactMatch = opt.dataset.host === host; + if (!ifExactMatch) { + return unhideOpt(opt); } - toDefault(opt); - const ifCommonPrefix = opt.dataset.host.startsWith(_nakedHost); - if (ifCommonPrefix) { - opt.value = prefixedHost + delim + '^' + opt.dataset.host; - return; - } - const ifCommonSuffix = opt.dataset.host.endsWith(_nakedHost); - if (ifCommonSuffix) { - opt.value = this.value + delim + opt.dataset.host + '$'; + + const exactOpt = opt; + + hideOpt(exactOpt); + if(exactOpt.innerHTML === ifProxyHtml) { + thisYes.checked = true; + this.classList.add(yesClass); + } else { + thisNo.checked = true; + this.classList.add(noClass); } } ); - this.classList.remove(noClass, yesClass); - if (exactOpt) { - console.log('EXACT', exactOpt); - //excList.childNodes.forEach( (opt) => opt.value = false ); - setInputValue(toDefault(exactOpt)); - if (ifLineSelected) { - // Hide all. - excList.childNodes.forEach( (opt) => opt.value = false ); - } else { - // Hide exact. - exactOpt.value = false; - } - - if(exactOpt.innerHTML === ifProxyHtml) { - thisYes.checked = true; - this.classList.add(yesClass); - } else { - thisNo.checked = true; - this.classList.add(noClass); - } - } return true; }; @@ -422,7 +381,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => return true; } - const _host = excEditor.value.trim().replace(prefRe, ''); + const host = excEditor.value.trim(); const pacMods = pacKitchen.getPacMods(); pacMods.exceptions = pacMods.exceptions || {}; @@ -430,11 +389,11 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => let fixUi = () => {}; if (thisAuto.checked) { - delete pacMods.exceptions[_host]; + delete pacMods.exceptions[host]; fixUi = () => excEditor.value = ''; } else { // YES or NO. - if (!validateHost(_host)) { + if (!validateHost(host)) { return false; } if (thisYes.checked && !pacMods.filteredCustomsString) { @@ -443,8 +402,8 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => )); return false; } - pacMods.exceptions[_host] = thisYes.checked; - fixUi = () => addOption(_host, thisYes.checked); + pacMods.exceptions[host] = thisYes.checked; + fixUi = () => addOption(host, thisYes.checked); } conduct( @@ -453,7 +412,9 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => 'Исключения применены.', () => { - getExactOpts(_host).forEach( (opt) => opt.remove() ); + excList.childNodes.forEach( + (opt) => opt.dataset.host === host && opt.remove() + ); fixUi(); excEditor.oninput(); @@ -557,8 +518,8 @@ HTTPS 11.22.33.44:8080;">${conf.value || localStorage.getItem(uiRaw) || ''}