diff options
author | Ruben Rodriguez <ruben@gnu.org> | 2018-09-13 20:58:40 +0000 |
---|---|---|
committer | Ruben Rodriguez <ruben@gnu.org> | 2018-09-13 20:58:40 +0000 |
commit | 3e4c252f982637b54719b427ac85ac3701d44abd (patch) | |
tree | 7b433796d4d3fcb9647e0da0cafe89e5e4d62521 /bg | |
parent | 0e004bf1a3c154b98a96494dc97ecba58b8162ed (diff) | |
parent | 3bf972bafeac738301623db6b08bedfd476abeca (diff) |
Merge #17 `Brand new settings UI to manage whitelist, blacklist and other preferences. `
Diffstat (limited to 'bg')
-rw-r--r-- | bg/ListManager.js | 37 | ||||
-rw-r--r-- | bg/Storage.js | 104 |
2 files changed, 19 insertions, 122 deletions
diff --git a/bg/ListManager.js b/bg/ListManager.js index 34d9531..e0a85e9 100644 --- a/bg/ListManager.js +++ b/bg/ListManager.js @@ -23,27 +23,28 @@ A class to manage whitelist/blacklist operations */ -let {ListStore} = require("./Storage"); +let {ListStore} = require("../common/Storage"); class ListManager { constructor(whitelist, blacklist, builtInHashes) { this.lists = {whitelist, blacklist}; this.builtInHashes = new Set(builtInHashes); } - async whitelist(key) { - await this.lists.blacklist.remove(key); - await this.lists.whitelist.store(key); + + static async move(fromList, toList, ...keys) { + await Promise.all([fromList.remove(...keys), toList.store(...keys)]); } - async blacklist(key) { - await this.lists.whitelist.remove(key); - await this.lists.blacklist.store(key); + + async whitelist(...keys) { + ListManager.move(this.lists.blacklist, this.lists.whitelist, ...keys); } - async forget(key) { - for (let list of Object.values(this.lists)) { - await list.remove(key); - } + async blacklist(...keys) { + ListManager.move(this.lists.whitelist, this.lists.blacklist, ...keys); } - /* key is a string representing either a URL or an optional path + async forget(...keys) { + await Promise.all(Object.values(this.lists).map(l => l.remove(...keys))); + } + /* key is a string representing either a URL or an optional path with a trailing (hash). Returns "blacklisted", "whitelisted" or defValue */ @@ -53,16 +54,16 @@ class ListManager { if (!match) { let url = ListStore.urlItem(key); let site = ListStore.siteItem(key); - return (blacklist.contains(url) || blacklist.contains(site)) + return (blacklist.contains(url) || blacklist.contains(site)) ? "blacklisted" - : whitelist.contains(url) || whitelist.contains(site) - ? "whitelisted" : defValue; + : whitelist.contains(url) || whitelist.contains(site) + ? "whitelisted" : defValue; } - + let [hashItem, srcHash] = match; // (hash), hash - + return blacklist.contains(hashItem) ? "blacklisted" - : this.builtInHashes.has(srcHash) || whitelist.contains(hashItem) + : this.builtInHashes.has(srcHash) || whitelist.contains(hashItem) ? "whitelisted" : defValue; } diff --git a/bg/Storage.js b/bg/Storage.js deleted file mode 100644 index ecdc9e4..0000000 --- a/bg/Storage.js +++ /dev/null @@ -1,104 +0,0 @@ -/** -* GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. -* -* Copyright (C) 2018 Giorgio Maone <giorgio@maone.net> -* -* This file is part of GNU LibreJS. -* -* GNU LibreJS is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* GNU LibreJS is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with GNU LibreJS. If not, see <http://www.gnu.org/licenses/>. -*/ - -/** - A tiny wrapper around extensions storage API, supporting CSV serialization for - retro-compatibility -*/ - -var Storage = { - ARRAY: { - async load(key) { - let array = (await browser.storage.local.get(key))[key]; - return array ? new Set(array) : new Set(); - }, - async save(key, list) { - return await browser.storage.local.set({[key]: [...list]}); - }, - }, - - CSV: { - async load(key) { - let csv = (await browser.storage.local.get(key))[key]; - return csv ? new Set(csv.split(/\s*,\s*/)) : new Set(); - }, - - async save(key, list) { - return await browser.storage.local.set({[key]: [...list].join(",")}); - } - } -}; - -/** - A class to hold and persist blacklists and whitelists -*/ - -class ListStore { - constructor(key, storage = Storage.ARRAY) { - this.key = key; - this.storage = storage; - this.items = new Set(); - } - - static hashItem(hash) { - return hash.startsWith("(") ? hash : `(${hash})`; - } - static urlItem(url) { - let queryPos = url.indexOf("?"); - return queryPos === -1 ? url : url.substring(0, queryPos); - } - static siteItem(url) { - if (url.endsWith("/*")) return url; - try { - return `${new URL(url).origin}/*`; - } catch (e) { - return `${url}/*`; - } - } - - async save() { - return await this.storage.save(this.key, this.items); - } - - async load() { - try { - this.items = await this.storage.load(this.key); - } catch (e) { - console.error(e); - } - return this.items; - } - - async store(item) { - let size = this.items.size; - return (size !== this.items.add(item).size) && await this.save(); - } - - async remove(item) { - return this.items.delete(item) && await this.save(); - } - - contains(item) { - return this.items.has(item); - } -} - -module.exports = { ListStore, Storage }; |