-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdatabase.js
151 lines (123 loc) · 3.7 KB
/
database.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import Dexie from 'dexie';
// Wrapper using Dexie.js to use IndexedDB.
const updateTime = 1 * 60 * 1000;
// Create database.
const db = new Dexie('localSheriff');
const dbCookie = new Dexie('localSheriffCookieTable');
db.version(1).stores({
reftp: '++id, ref, tp',
refhtml: '++id, ref, html',
tpurl: '++id, tpurl',
inputFields: '++id, value, sender, details',
inputFieldsCache: '++id, value, summary',
tpURLFP: '++id, tpurl, details'
});
// Save cookies
dbCookie.version(1).stores({
cookietable: '++id, url, details'
});
db.open().then(function (db) {
// Database opened successfully
console.log(" DB connection successful.");
loadFromDatabase();
}).catch(function (err) {
// Error occurred
console.log("Error occurred while opening DB." + err);
});
// Access cookie table:
dbCookie.open().then(function (dbCookie) {
// Database opened successfully
console.log(" DB connection successful.");
loadCookiesFromDatabase();
}).catch(function (err) {
// Error occurred
console.log("Error occurred while opening DB." + err);
});
// Currently dumping complete objects every 2 minutes.
function dump() {
console.log("Dumping data to disk");
db.reftp.add(JSON.stringify(refTP), 'raw');
db.refhtml.add({ refhtml: JSON.stringify(refHTML) });
db.tpurl.add({ tpurl: JSON.stringify(tpURL) });
}
// When extension starts try to load data from disk.
function load(obj, key) {
db[key].get(key).then(e => {
if (e) obj = JSON.parse(e);
});
}
export function saveInDB(ref, tp) {
db.reftp.add({ ref: ref, tp: tp });
}
export function saveHTML(ref, html) {
db.refhtml.add({ ref: ref, html: html });
}
export function savetpList(tpurl) {
db.tpurl.add({ tpurl: tpurl });
}
export function saveInputFields(value, sender, details) {
db.inputFields.add({ value: value, sender: sender, details: details });
}
export function saveInputFieldsCache(value, summary) {
db.inputFieldsCache.add({ value: value, summary: summary });
}
export function savetpURLFP(tpurl, details) {
db.tpURLFP.add({ tpurl: tpurl, details: details });
}
export function saveCookies(url, details) {
dbCookie.cookietable.add({ url: url, details: details });
}
function loadFromDatabase() {
db.reftp.each(row => { addToDict(row.ref, row.tp, 'load') });
db.refhtml.each(row => {
if (!refHTML.hasOwnProperty(row.ref)) {
// Otherwise it will send multiple requests to the same url.
refHTML[row.ref] = row.html;
}
});
db.tpurl.each(row => {
tpURL.add(row.tpurl);
});
db.inputFields.each(row => {
if (!inputFields.hasOwnProperty(row.value)) inputFields[row.value] = {};
if (!inputFields[row.value].hasOwnProperty(row.sender)) inputFields[row.value][row.sender] = {};
inputFields[row.value][row.sender] = row.details;
});
db.inputFieldsCache.each(row => {
inputFieldsCache[row.value] = row.summary;
});
db.tpURLFP.each(row => {
thirdPartyFP[row.tpurl] = row.details;
});
}
function loadCookiesFromDatabase() {
dbCookie.cookietable.each(row => {
cookieTable[row.url] = row.details;
});
}
function cleanLSDB() {
// Clean local-sheriff DB.
return db.delete().then(() => {
console.log("Local-sheriff Database successfully deleted");
Promise.resolve(true);
}).catch((err) => {
console.error("Could not delete cookie database: " + err);
Promise.reject(false);
});
}
function cleanCookieDB() {
// Clean local-sheriff DB.
return dbCookie.delete().then(() => {
console.log("Cookie Database successfully deleted");
Promise.resolve(true);
}).catch((err) => {
console.error("Could not delete cookie database: " + err);
Promise.reject(false);
});
}
export function cleanStorage() {
// Clean databases and then re-load the extension.
Promise.all([cleanLSDB(), cleanCookieDB()]).then(status => {
chrome.runtime.reload();
}).catch(console.log);
}