Keep objects in mods.in/ex(cluded). Refactor

This commit is contained in:
ilyaigpetrov 2021-01-12 17:29:18 +00:00
parent e95c186de1
commit ccda45a57c
2 changed files with 17 additions and 13 deletions

View File

@ -298,18 +298,20 @@
self.included = [];
self.excluded = [];
for(const host of Object.keys(self.exceptions)) {
if (self.exceptions[host]?.ifIncluded) {
self.included.push(host);
const ifProxy = self.exceptions[host]?.ifProxy || false;
const ifWild = self.exceptions[host]?.ifWild || false;
if (ifProxy) {
self.included.push([host, ifWild]);
} else {
self.excluded.push(host);
self.excluded.push([host, ifWild]);
}
}
['included', 'excluded'].forEach((who) => {
self[who] = self[who]
.map( (s) => s.split('').reverse() )
.map( ([domain, ifWild]) => [domain.split('').reverse(), ifWild] )
.sort()
.map( (a) => a.reverse().join('') );
.map( ([rDomain, ifWild]) => [rDomain.reverse().join(''), ifWild] );
});
if (self.included.length && !self.filteredCustomsString) {
@ -390,7 +392,7 @@
if (pacMods.ifProxyMoreDomains) {
finalExceptions = pacMods.moreDomains.reduce((acc, tld) => {
acc[tld] = { ifIncluded: true, ifWild: true };
acc[tld] = { ifProxy: true, ifWild: true };
return acc;
}, finalExceptions);
@ -406,7 +408,7 @@
/******/ /* EXCEPTIONS START */
/******/ const dotHost = '.' + host;
/******/ const isHostInDomain = (domain) => dotHost.endsWith('.' + domain);
/******/ const domainReducer = (maxWeight, [domain, { ifIncluded, ifWild }]) => {
/******/ const domainReducer = (maxWeight, [domain, { ifProxy, ifWild }]) => {
/******/
/******/ if (!isHostInDomain(domain)) {
/******/ return maxWeight;
@ -415,7 +417,7 @@
/******/ if (newWeightAbs < Math.abs(maxWeight)) {
/******/ return maxWeight;
/******/ }
/******/ return newWeightAbs*(ifIncluded ? 1 : -1);
/******/ return newWeightAbs*(ifProxy ? 1 : -1);
/******/
/******/ };
/******/

View File

@ -110,7 +110,7 @@ export default function getExcEditor(theState) {
isHostValid(host) {
const ValidHostnameRegex = /^(?:\*\.)?(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
const ValidHostnameRegex = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
if(!ValidHostnameRegex.test(host)) {
this.props.funs.showErrors(new TypeError('Должно быть только доменное имя, без протокола, порта и пути. Попробуйте ещё раз.'));
return false;
@ -122,6 +122,8 @@ export default function getExcEditor(theState) {
handleRadioClick(event) {
let host = this.state.trimmedInputValueOrSpace;
const ifWild = host.startsWith('*.');
host = host.replace(/^\*\./g, '');
(() => { // `return` === `preventDefault`.
if(!this.isHostValid(host)) {
@ -147,8 +149,7 @@ export default function getExcEditor(theState) {
return false;
}
const ifWild = host.startsWith('*.');
pacMods.exceptions[host.replace(/^\*\./g, '')] = { ifIncluded: ifYesClicked, ifWild };
pacMods.exceptions[host] = { ifProxy: ifYesClicked, ifWild };
break;
default:
@ -293,7 +294,7 @@ export default function getExcEditor(theState) {
if ( acc !== undefined ) {
return acc;
}
return this.state.trimmedInputValueOrSpace === excHost ? excState : undefined;
return this.state.trimmedInputValueOrSpace === excHost ? (excState || {}).ifProxy : undefined;
}, undefined);
@ -322,9 +323,10 @@ export default function getExcEditor(theState) {
// 1. Option's value may be changed to hide it from the tooltip.
// 2. Space is used in matching so even an empty input (replaced with space) has tooltip with prompts.
const ifProxy = (excState || {}).ifProxy;
return <option
value={ this.state.ifHostHiddenMap[excHost] ? '\n' : excHost + ' ' }
label={ excState === true ? labelIfProxied : (excState === false ? labelIfNotProxied : labelIfAuto) }/>
label={ ifProxy === true ? labelIfProxied : (ifProxy === false ? labelIfNotProxied : labelIfAuto) }/>
})
}