Rollback provider key on error, remove unnecessary returns

This commit is contained in:
Ilya Ig. Petrov 2016-11-21 09:39:46 -08:00
parent 403c8e47b7
commit aa20f220ee
2 changed files with 36 additions and 33 deletions

View File

@ -79,7 +79,7 @@
} }
} }
return chrome.storage.local.clear( chrome.storage.local.clear(
() => chrome.storage.local.set( () => chrome.storage.local.set(
onlySettable, onlySettable,
chromified(cb, onlySettable) chromified(cb, onlySettable)
@ -101,7 +101,7 @@
} }
console.log('Synced with storage, any callback?', !!cb); console.log('Synced with storage, any callback?', !!cb);
return cb && cb(err, storage); cb && cb(err, storage);
}); });
}, },
@ -124,7 +124,7 @@
this.setAlarms(); this.setAlarms();
} }
return resolve([err, res]); resolve([err, res]);
} }
) )
@ -138,7 +138,7 @@
if (ipsError && ipsError.clarification) { if (ipsError && ipsError.clarification) {
ipsError.clarification.ifNotCritical = true; ipsError.clarification.ifNotCritical = true;
} }
return resolve([ipsError]); resolve([ipsError]);
} }
) )
@ -150,7 +150,6 @@
if (pacErr && ipsErr) { if (pacErr && ipsErr) {
return cb(pacErr, pacRes); return cb(pacErr, pacRes);
} }
return cb(pacErr || ipsErr);
this.pushToStorage( this.pushToStorage(
(pushErr) => cb(pacErr || ipsErr || pushErr, pacRes) (pushErr) => cb(pacErr || ipsErr || pushErr, pacRes)
); );
@ -193,11 +192,22 @@
key = undefined; key = undefined;
} }
if(key) { const oldKey = this.currentPacProviderKey;
if(key || key !== oldKey) {
this.currentPacProviderKey = key; this.currentPacProviderKey = key;
const _cb = cb;
cb = (err, res) => {
if (err && !(err.clarification && err.clarification.ifNotCritical)) {
console.log('Rollback privider key.');
this.currentPacProviderKey = oldKey;
}
_cb(err, res);
};
} }
return this.syncWithPacProvider(cb); this.syncWithPacProvider(cb);
}, },
@ -214,7 +224,7 @@
return cb(err); return cb(err);
} }
this.currentPacProviderKey = undefined; this.currentPacProviderKey = undefined;
return this.pushToStorage(cb); this.pushToStorage(cb);
} }
) )
@ -258,7 +268,7 @@
if (antiCensorRu.ifFirstInstall) { if (antiCensorRu.ifFirstInstall) {
// INSTALL // INSTALL
console.log('Installing...'); console.log('Installing...');
return chrome.runtime.openOptionsPage(); chrome.runtime.openOptionsPage();
} }
if (!antiCensorRu.pacProvider) { if (!antiCensorRu.pacProvider) {
@ -311,7 +321,7 @@
console.groupEnd(); console.groupEnd();
console.log('Group finished.'); console.log('Group finished.');
return cb.apply(this, cbArgs); cb.apply(this, cbArgs);
} }
} }
@ -341,7 +351,7 @@
args = replaceArgs; args = replaceArgs;
} }
const err = checkChromeError(stack); const err = checkChromeError(stack);
return cb && cb.call(this, err, ...args); cb && cb.call(this, err, ...args);
}; };
@ -371,7 +381,7 @@
return cb({clarification: {message:'Настройки прокси контролирует другое расширение. <a href="chrome://settings/search#proxy">Какое?</a>'}}); return cb({clarification: {message:'Настройки прокси контролирует другое расширение. <a href="chrome://settings/search#proxy">Какое?</a>'}});
} }
console.log('Successfuly set PAC in proxy settings..'); console.log('Successfuly set PAC in proxy settings..');
return cb(); cb();
}); });
}); });
@ -381,7 +391,7 @@
function httpGet(url, cb) { function httpGet(url, cb) {
const start = Date.now(); const start = Date.now();
return fetch(url).then( fetch(url).then(
(res) => { (res) => {
const textCb = const textCb =
@ -393,13 +403,13 @@
return textCb(res); return textCb(res);
} }
console.log('GETed with success:', url, Date.now() - start); console.log('GETed with success:', url, Date.now() - start);
return textCb(); textCb();
}, },
(err) => { (err) => {
err.clarification = {message: 'Что-то не так с сетью, проверьте соединение.'}; err.clarification = {message: 'Что-то не так с сетью, проверьте соединение.'};
return cb && cb(err); cb && cb(err);
} }
); );
@ -455,7 +465,7 @@
err.data = err.data || res; err.data = err.data || res;
} }
} }
return cb( err, res ); cb( err, res );
} }
); );
}; };
@ -528,7 +538,7 @@
if ( ++i === provider.proxyHosts.length ) { if ( ++i === provider.proxyHosts.length ) {
failure = Object.keys(failure.errors).length ? failure : null; failure = Object.keys(failure.errors).length ? failure : null;
return cb(failure, provider.proxyIps); cb(failure, provider.proxyIps);
} }
} }
) )
@ -574,6 +584,7 @@ window.addEventListener('unhandledrejection', (event) => {
chrome.proxy.settings.onChange.addListener((details) => { chrome.proxy.settings.onChange.addListener((details) => {
console.log('Settings changed:', details); console.log('Proxy settings changed.', details);
const ifOther = details.levelOfControl.startsWith('controlled_by_other');
}); });

View File

@ -67,18 +67,8 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => {
return document.querySelector('#'+id); return document.querySelector('#'+id);
} }
const checkChosenProvider = () => { const checkChosenProvider = () => currentRadio().checked = true;
const triggerChosenProvider = () => currentRadio().click();
currentRadio().checked = true;
}
const triggerChosenProvider = () => {
const event = document.createEvent('HTMLEvents');
event.initEvent('change', false, true);
currentRadio().dispatchEvent(event);
}
const ul = document.querySelector('#list-of-providers'); const ul = document.querySelector('#list-of-providers');
const _firstChild = ul.firstChild; const _firstChild = ul.firstChild;
@ -91,11 +81,12 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => {
const radios = [].slice.apply( document.querySelectorAll('[name=pacProvider]') ); const radios = [].slice.apply( document.querySelectorAll('[name=pacProvider]') );
for(const radio of radios) { for(const radio of radios) {
radio.onchange = function(event) { radio.onclick = function(event) {
const pacKey = event.target.id; const pacKey = event.target.id;
if (pacKey === 'none') if (pacKey === 'none') {
return antiCensorRu.clearPac(); return antiCensorRu.clearPac();
}
const enableDisableInputs = function () { const enableDisableInputs = function () {
@ -113,6 +104,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => {
backgroundPage.console.log('Popup callback...'); backgroundPage.console.log('Popup callback...');
if (!err) { if (!err) {
setStatusTo('PAC-скрипт установлен.'); setStatusTo('PAC-скрипт установлен.');
checkChosenProvider();
} }
else { else {
const ifNotCritical = err.clarification && err.clarification.ifNotCritical; const ifNotCritical = err.clarification && err.clarification.ifNotCritical;
@ -134,7 +126,6 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => {
getStatus().querySelector('.link-button').onclick = function() { getStatus().querySelector('.link-button').onclick = function() {
const div = document.createElement('div'); const div = document.createElement('div');
backgroundPage.console.log('ERROR', err);
div.innerHTML = ` div.innerHTML = `
Более подробную информацию можно узнать из логов фоновой страницы:<br/> Более подробную информацию можно узнать из логов фоновой страницы:<br/>
<a href="chrome://extensions?id=${chrome.runtime.id}" data-in-bg="true">chrome://extensions</a> Это расширение Отладка страниц: фоновая страница Console (DevTools) <a href="chrome://extensions?id=${chrome.runtime.id}" data-in-bg="true">chrome://extensions</a> Это расширение Отладка страниц: фоновая страница Console (DevTools)
@ -148,6 +139,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => {
} }
enableDisableInputs(); enableDisableInputs();
}); });
return false;
} }
} }