update readme
[dotfiles/.git] / .config / BraveSoftware / Brave-Browser / Default / Extensions / cimiefiiaegbelhefglklhhakcgmhkai / 1.7.6_0 / utils.js
1 /*
2     Copyright (C) 2019 Kai Uwe Broulik <kde@privat.broulik.de>
3
4     This program is free software; you can redistribute it and/or
5     modify it under the terms of the GNU General Public License as
6     published by the Free Software Foundation; either version 3 of
7     the License, or (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 class SettingsUtils {
19     static storage() {
20         return (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync);
21     }
22
23     static get() {
24         return new Promise((resolve, reject) => {
25             SettingsUtils.storage().get(null /* get all */, (items) => {
26                 const error = chrome.runtime.lastError;
27                 if (error) {
28                     return reject(error);
29                 }
30
31                 // Passing a default Object get() only returns defaults for the top level of the object,
32                 // so we need to merge the level underneath manually.
33                 const addObjectInto = (obj, add) => {
34                     if (typeof add !== "object" || Array.isArray(add)) {
35                         return;
36                     }
37
38                     Object.keys(add).forEach((key) => {
39                         if (obj.hasOwnProperty(key)) {
40                             addObjectInto(obj[key], add[key]);
41                         } else {
42                             obj[key] = add[key];
43                         }
44                     });
45                 };
46
47                 addObjectInto(items, DEFAULT_EXTENSION_SETTINGS);
48                 resolve(items);
49             });
50         });
51     }
52
53     static set(settings) {
54         return new Promise((resolve, reject) => {
55             try {
56                 SettingsUtils.storage().set(settings, () => {
57                     const error = chrome.runtime.lastError;
58                     if (error) {
59                         return reject(error);
60                     }
61
62                     resolve();
63                 });
64             } catch (e) {
65                 reject(e);
66             }
67         });
68     }
69
70     static onChanged() {
71         return chrome.storage.onChanged;
72     }
73 }