Moved url container initialization

Fixes #166
This commit is contained in:
kkapsner 2018-09-16 12:27:50 +02:00
parent e4c2196131
commit 55af2c3dc4
2 changed files with 76 additions and 73 deletions

View File

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
"use strict";
var scope;
if ((typeof exports) !== "undefined"){
scope = exports;
@ -135,4 +135,78 @@
});
}
};
scope.initializeUrlContainer = function(eventHandler){
if (scope.urlContainer){
scope.urlContainer.on(function({newValue, oldValue}){
newValue.forEach(function(urlSetting){
var regExp;
var 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 {
return url.href.match(regExp);
}
};
Object.defineProperty(
urlSetting,
"match",
{
enumerable: false,
writable: true,
configurable: true,
value: match
}
);
});
var newUrls = newValue.map(function(entry){return entry.url;});
var oldUrls = oldValue.map(function(entry){return entry.url;});
var 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){
var oldEntry = oldValue[matching[url].old] || {};
var newEntry = newValue[matching[url].new] || {};
scope.urlContainer.entries.forEach(function(settingDefinition){
var name = settingDefinition.name;
var oldValue = oldEntry[name];
var newValue = newEntry[name];
if (oldValue !== newValue){
((eventHandler[name] || {})[url] || []).forEach(function(callback){
callback({name, newValue, oldValue, url});
});
}
});
});
});
}
};
}());

View File

@ -374,79 +374,8 @@
}
});
if (settingContainers.urlContainer){
settingContainers.urlContainer.on(function({newValue, oldValue}){
newValue.forEach(function(urlSetting){
var regExp;
var 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 {
return url.href.match(regExp);
}
};
Object.defineProperty(
urlSetting,
"match",
{
enumerable: false,
writable: true,
configurable: true,
value: match
}
);
});
var newUrls = newValue.map(function(entry){return entry.url;});
var oldUrls = oldValue.map(function(entry){return entry.url;});
var 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){
var oldEntry = oldValue[matching[url].old] || {};
var newEntry = newValue[matching[url].new] || {};
settingContainers.urlContainer.entries.forEach(function(settingDefinition){
var name = settingDefinition.name;
var oldValue = oldEntry[name];
var newValue = newEntry[name];
if (oldValue !== newValue){
((eventHandler[name] || {})[url] || []).forEach(function(callback){
callback({name, newValue, oldValue, url});
});
}
});
});
});
}
settingContainers.initializeUrlContainer(eventHandler);
logging.verbose("loading settings");
let initialized = false;
scope.isInitialized = function(){