mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-07-04 20:46:39 +02:00
Code cleanup: reduce block nesting
This commit is contained in:
parent
10413a89c3
commit
abdb95b815
6 changed files with 310 additions and 293 deletions
|
@ -165,76 +165,77 @@
|
|||
};
|
||||
|
||||
scope.initializeUrlContainer = function(eventHandler){
|
||||
if (scope.urlContainer){
|
||||
scope.urlContainer.on(function({newValue, oldValue}){
|
||||
newValue.forEach(function(urlSetting){
|
||||
let regExp;
|
||||
const domain = !!urlSetting.url.match(/^[A-Za-z0-9_.-]+$/);
|
||||
if (domain){
|
||||
regExp = new RegExp(
|
||||
"(?:^|\\.)" + urlSetting.url.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "\\.?$",
|
||||
"i"
|
||||
);
|
||||
if (!scope.urlContainer){
|
||||
return;
|
||||
}
|
||||
scope.urlContainer.on(function({newValue, oldValue}){
|
||||
newValue.forEach(function(urlSetting){
|
||||
let regExp;
|
||||
const domain = !!urlSetting.url.match(/^[A-Za-z0-9_.-]+$/);
|
||||
if (domain){
|
||||
regExp = new RegExp(
|
||||
"(?:^|\\.)" + urlSetting.url.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "\\.?$",
|
||||
"i"
|
||||
);
|
||||
}
|
||||
else {
|
||||
regExp = new RegExp(urlSetting.url, "i");
|
||||
}
|
||||
const match = function(url){
|
||||
if (!url){
|
||||
return false;
|
||||
}
|
||||
else if (
|
||||
url instanceof String ||
|
||||
(typeof url) === "string"
|
||||
){
|
||||
return url === urlSetting.url;
|
||||
}
|
||||
else if (domain){
|
||||
return (url.hostname || "").match(regExp);
|
||||
}
|
||||
else {
|
||||
regExp = new RegExp(urlSetting.url, "i");
|
||||
return url.href.match(regExp);
|
||||
}
|
||||
const match = function(url){
|
||||
if (!url){
|
||||
return false;
|
||||
}
|
||||
else if (
|
||||
url instanceof String ||
|
||||
(typeof url) === "string"
|
||||
){
|
||||
return url === urlSetting.url;
|
||||
}
|
||||
else if (domain){
|
||||
return (url.hostname || "").match(regExp);
|
||||
}
|
||||
else {
|
||||
return url.href.match(regExp);
|
||||
}
|
||||
};
|
||||
Object.defineProperty(
|
||||
urlSetting,
|
||||
"match",
|
||||
{
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
value: match
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
const newUrls = newValue.map(function(entry){return entry.url;});
|
||||
const oldUrls = oldValue.map(function(entry){return entry.url;});
|
||||
const matching = {};
|
||||
newUrls.forEach(function(url, i){
|
||||
matching[url] = {new: i, old: oldUrls.indexOf(url)};
|
||||
});
|
||||
oldUrls.forEach(function(url, i){
|
||||
if (!matching[url]){
|
||||
matching[url] = {new: -1, old: i};
|
||||
};
|
||||
Object.defineProperty(
|
||||
urlSetting,
|
||||
"match",
|
||||
{
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
value: match
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
const newUrls = newValue.map(function(entry){return entry.url;});
|
||||
const oldUrls = oldValue.map(function(entry){return entry.url;});
|
||||
const matching = {};
|
||||
newUrls.forEach(function(url, i){
|
||||
matching[url] = {new: i, old: oldUrls.indexOf(url)};
|
||||
});
|
||||
oldUrls.forEach(function(url, i){
|
||||
if (!matching[url]){
|
||||
matching[url] = {new: -1, old: i};
|
||||
}
|
||||
});
|
||||
Object.keys(matching).forEach(function(url){
|
||||
const oldEntry = oldValue[matching[url].old] || {};
|
||||
const newEntry = newValue[matching[url].new] || {};
|
||||
scope.urlContainer.entries.forEach(function(settingDefinition){
|
||||
const name = settingDefinition.name;
|
||||
const oldValue = oldEntry[name];
|
||||
const newValue = newEntry[name];
|
||||
|
||||
if (oldValue !== newValue){
|
||||
((eventHandler[name] || {})[url] || []).forEach(function(callback){
|
||||
callback({name, newValue, oldValue, url});
|
||||
});
|
||||
}
|
||||
});
|
||||
Object.keys(matching).forEach(function(url){
|
||||
const oldEntry = oldValue[matching[url].old] || {};
|
||||
const newEntry = newValue[matching[url].new] || {};
|
||||
scope.urlContainer.entries.forEach(function(settingDefinition){
|
||||
const name = settingDefinition.name;
|
||||
const oldValue = oldEntry[name];
|
||||
const newValue = newEntry[name];
|
||||
|
||||
if (oldValue !== newValue){
|
||||
((eventHandler[name] || {})[url] || []).forEach(function(callback){
|
||||
callback({name, newValue, oldValue, url});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
}());
|
Loading…
Add table
Add a link
Reference in a new issue