aboutsummaryrefslogtreecommitdiff
path: root/bg/Storage.js
blob: ecdc9e420820b8806b02b46d42429182d8321087 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* 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 };