2015-09-06 12:26:50 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
(function(){
|
2015-09-06 15:40:34 +02:00
|
|
|
"use strict";
|
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
let scope;
|
2017-06-25 22:33:12 +02:00
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-12 22:24:23 +01:00
|
|
|
scope = require.register("./lists", {});
|
2017-06-25 22:33:12 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const settings = require("./settings");
|
2022-04-24 20:50:06 +02:00
|
|
|
const logging = require("./logging");
|
2017-06-25 22:33:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
function getDomainRegExpList(domainList){
|
2019-11-28 01:26:35 +01:00
|
|
|
const list = domainList
|
2017-06-25 22:33:12 +02:00
|
|
|
.split(",")
|
|
|
|
.map(function(entry){
|
|
|
|
return entry.replace(/^\s+|\s+$/g, "");
|
|
|
|
})
|
|
|
|
.filter(function(entry){
|
|
|
|
return !!entry.length;
|
|
|
|
})
|
|
|
|
.map(function(entry){
|
2019-11-28 01:26:35 +01:00
|
|
|
let regExp;
|
2022-04-26 17:08:28 +02:00
|
|
|
const domain = !!entry.match(/^[A-Za-z0-9_.*-]+$/);
|
2017-06-25 22:33:12 +02:00
|
|
|
if (domain){
|
2022-04-26 17:08:28 +02:00
|
|
|
regExp = new RegExp(
|
|
|
|
"(?:^|\\.)" + entry.replace(/([\\+?[^\]$(){}=!|.])/g, "\\$1").replace(/\*/g, ".+") + "\\.?$",
|
|
|
|
"i"
|
|
|
|
);
|
2017-06-25 22:33:12 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-04-24 20:50:06 +02:00
|
|
|
try {
|
|
|
|
regExp = new RegExp(entry, "i");
|
|
|
|
}
|
|
|
|
catch (error){
|
|
|
|
logging.error("Error in regular expression", entry, error);
|
|
|
|
regExp = new RegExp(
|
|
|
|
"(?:^|\\.)" + entry.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "\\.?$",
|
|
|
|
"i"
|
|
|
|
);
|
|
|
|
}
|
2017-06-25 22:33:12 +02:00
|
|
|
}
|
|
|
|
return {
|
2021-05-25 16:44:20 +02:00
|
|
|
value: entry,
|
2017-06-25 22:33:12 +02:00
|
|
|
match: function(url){
|
|
|
|
if (domain){
|
|
|
|
return (url.hostname || "").match(regExp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return url.href.match(regExp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-09-06 15:40:34 +02:00
|
|
|
});
|
2017-06-25 22:33:12 +02:00
|
|
|
|
2018-07-17 13:07:50 +02:00
|
|
|
return addMatchToList(list);
|
|
|
|
}
|
|
|
|
function addMatchToList(list){
|
2017-10-05 19:00:00 +02:00
|
|
|
list.match = function(url){
|
|
|
|
return this.some(function(entry){
|
|
|
|
return entry.match(url);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return list;
|
2017-06-25 22:33:12 +02:00
|
|
|
}
|
2015-09-06 12:26:50 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const lists = {
|
2017-06-25 22:33:12 +02:00
|
|
|
white: [],
|
2018-07-17 13:07:50 +02:00
|
|
|
sessionWhite: [],
|
2017-06-25 22:33:12 +02:00
|
|
|
"ignore": [],
|
|
|
|
black: []
|
|
|
|
};
|
2015-09-06 12:26:50 +02:00
|
|
|
|
2017-09-23 23:47:44 +02:00
|
|
|
function updateList(type, value){
|
|
|
|
if (typeof value === "undefined"){
|
2017-11-07 00:36:44 +01:00
|
|
|
value = settings[type + "List"];
|
2017-09-23 23:47:44 +02:00
|
|
|
}
|
|
|
|
lists[type] = getDomainRegExpList(value);
|
2018-07-17 12:54:10 +02:00
|
|
|
return lists[type];
|
2017-06-25 22:33:12 +02:00
|
|
|
}
|
|
|
|
Object.keys(lists).forEach(function(type){
|
2017-11-07 00:36:44 +01:00
|
|
|
settings.on(type + "List", function({newValue}){
|
|
|
|
updateList(type, newValue);
|
2017-06-25 22:33:12 +02:00
|
|
|
});
|
2017-11-07 00:36:44 +01:00
|
|
|
updateList(type, settings[type + "List"]);
|
2015-09-06 12:26:50 +02:00
|
|
|
});
|
|
|
|
|
2017-09-23 23:47:44 +02:00
|
|
|
function updateStackList(value){
|
2019-11-28 01:26:35 +01:00
|
|
|
let list;
|
2017-06-25 22:33:12 +02:00
|
|
|
try {
|
2019-11-28 01:26:35 +01:00
|
|
|
let data = JSON.parse(value);
|
2017-06-25 22:33:12 +02:00
|
|
|
if (!Array.isArray(data)){
|
|
|
|
data = [data];
|
|
|
|
}
|
|
|
|
list = data.filter(function(entry){
|
|
|
|
return typeof entry === "object" && typeof entry.url === "string";
|
|
|
|
});
|
2015-12-31 13:37:27 +01:00
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
catch(error){
|
2017-06-25 22:33:12 +02:00
|
|
|
list = [];
|
|
|
|
}
|
|
|
|
list.match = function(stack){
|
|
|
|
return this.some(function(stackRule){
|
|
|
|
return stack.match(stackRule);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
lists.stack = list;
|
2015-12-31 13:37:27 +01:00
|
|
|
}
|
2017-06-25 22:33:12 +02:00
|
|
|
lists.stack = [];
|
2017-11-07 00:36:44 +01:00
|
|
|
settings.on("stackList", function({newValue}){
|
|
|
|
updateStackList(newValue);
|
2017-06-25 22:33:12 +02:00
|
|
|
});
|
2017-11-07 00:36:44 +01:00
|
|
|
updateStackList(settings.stackList);
|
2015-12-31 13:37:27 +01:00
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
scope.get = function getList(type){
|
2018-07-17 13:07:50 +02:00
|
|
|
if (type === "white"){
|
2019-11-28 01:26:35 +01:00
|
|
|
const combined = lists.white.slice().concat(lists.sessionWhite);
|
2018-07-17 13:07:50 +02:00
|
|
|
return addMatchToList(combined);
|
|
|
|
}
|
2017-06-25 22:33:12 +02:00
|
|
|
return lists[type];
|
|
|
|
};
|
2019-12-28 23:23:55 +01:00
|
|
|
scope.appendTo = async function appendToList(type, entry){
|
2019-11-28 01:26:35 +01:00
|
|
|
const oldValue = settings[type + "List"];
|
2019-12-28 23:23:55 +01:00
|
|
|
await settings.set(type + "List", oldValue + (oldValue? ",": "") + entry);
|
|
|
|
return updateList(type);
|
2017-06-25 22:33:12 +02:00
|
|
|
};
|
2021-05-25 16:44:20 +02:00
|
|
|
scope.removeFrom = async function removeFromList(type, entry){
|
|
|
|
const oldValue = settings[type + "List"];
|
|
|
|
const filter = entry.forEach? v => entry.indexOf(v) === -1: v => v !== entry;
|
|
|
|
await settings.set(
|
|
|
|
type + "List",
|
|
|
|
oldValue
|
|
|
|
.split(",")
|
|
|
|
.map(v => v.replace(/^\s+|\s+$/, ""))
|
|
|
|
.filter(filter)
|
|
|
|
.join(",")
|
|
|
|
);
|
|
|
|
return updateList(type);
|
|
|
|
};
|
2017-06-25 22:33:12 +02:00
|
|
|
scope.update = updateList;
|
2017-07-07 08:46:58 +02:00
|
|
|
scope.updateAll = function updateAllLists(){
|
|
|
|
updateList("white");
|
2021-05-25 16:44:20 +02:00
|
|
|
updateList("sessionWhite");
|
2017-07-07 08:46:58 +02:00
|
|
|
updateList("ignore");
|
|
|
|
updateList("black");
|
2017-11-07 00:36:44 +01:00
|
|
|
updateStackList(settings.stackList);
|
2017-07-07 08:46:58 +02:00
|
|
|
};
|
2017-11-07 00:36:44 +01:00
|
|
|
settings.onloaded(scope.updateAll);
|
2017-06-25 22:33:12 +02:00
|
|
|
}());
|