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
|
|
|
|
|
|
|
var scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.scope.lists = {};
|
|
|
|
scope = window.scope.lists;
|
|
|
|
}
|
|
|
|
|
|
|
|
var preferences = require("sdk/simple-prefs");
|
|
|
|
var prefs = preferences.prefs;
|
|
|
|
|
|
|
|
|
|
|
|
function getDomainRegExpList(domainList){
|
|
|
|
var list = domainList
|
|
|
|
.split(",")
|
|
|
|
.map(function(entry){
|
|
|
|
return entry.replace(/^\s+|\s+$/g, "");
|
|
|
|
})
|
|
|
|
.filter(function(entry){
|
|
|
|
return !!entry.length;
|
|
|
|
})
|
|
|
|
.map(function(entry){
|
|
|
|
var regExp;
|
|
|
|
var domain = !!entry.match(/^[\w.]+$/);
|
|
|
|
if (domain){
|
2017-10-03 15:35:31 +02:00
|
|
|
regExp = new RegExp("(?:^|\\.)" + entry.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "\\.?$", "i");
|
2017-06-25 22:33:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
regExp = new RegExp(entry, "i");
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
var lists = {
|
|
|
|
white: [],
|
|
|
|
"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"){
|
|
|
|
value = prefs[type + "List"];
|
|
|
|
}
|
|
|
|
lists[type] = getDomainRegExpList(value);
|
2017-06-25 22:33:12 +02:00
|
|
|
}
|
|
|
|
Object.keys(lists).forEach(function(type){
|
2017-09-23 23:47:44 +02:00
|
|
|
preferences.on(type + "List", function(value){
|
|
|
|
updateList(type, value);
|
2017-06-25 22:33:12 +02:00
|
|
|
});
|
2017-09-23 23:47:44 +02:00
|
|
|
updateList(type, prefs[type + "List"]);
|
2015-09-06 12:26:50 +02:00
|
|
|
});
|
|
|
|
|
2017-09-23 23:47:44 +02:00
|
|
|
function updateStackList(value){
|
2017-06-25 22:33:12 +02:00
|
|
|
var list;
|
|
|
|
try {
|
2017-09-23 23:47:44 +02:00
|
|
|
var 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
|
|
|
}
|
2017-06-25 22:33:12 +02:00
|
|
|
catch(e){
|
|
|
|
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-09-23 23:47:44 +02:00
|
|
|
preferences.on("stackList", function(value){
|
|
|
|
updateStackList(value);
|
2017-06-25 22:33:12 +02:00
|
|
|
});
|
2017-09-23 23:47:44 +02:00
|
|
|
updateStackList(prefs.stackList);
|
2015-12-31 13:37:27 +01:00
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
scope.get = function getList(type){
|
|
|
|
return lists[type];
|
|
|
|
};
|
|
|
|
scope.appendTo = function appendToList(type, entry){
|
|
|
|
prefs[type + "List"] += (prefs[type + "List"]? ",": "") + entry;
|
|
|
|
var obj = {};
|
|
|
|
obj[type + "List"] = prefs[type + "List"];
|
|
|
|
browser.storage.local.set(obj);
|
|
|
|
updateList(type);
|
|
|
|
};
|
|
|
|
scope.update = updateList;
|
2017-07-07 08:46:58 +02:00
|
|
|
scope.updateAll = function updateAllLists(){
|
|
|
|
updateList("white");
|
|
|
|
updateList("ignore");
|
|
|
|
updateList("black");
|
2017-09-23 23:47:44 +02:00
|
|
|
updateStackList(prefs.stackList);
|
2017-07-07 08:46:58 +02:00
|
|
|
};
|
2017-06-25 22:33:12 +02:00
|
|
|
}());
|