-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathbackground.js
More file actions
More file actions
Latest commit
1381 lines (1119 loc) · 27.7 KB
/
Copy pathbackground.js
File metadata and controls
1381 lines (1119 loc) · 27.7 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
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
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
"use strict";
const api = globalThis.browser ?? globalThis.chrome;
const META_DB_NAME = "serp-domain-label-meta";
const META_DB_VERSION = 1;
const META_STORE = "metadata";
const STATE_KEY = "state";
const IMPORT_KEY = "import-state";
const GENERATION_DB_PREFIX = "serp-domain-label-generation-";
const GENERATION_DB_VERSION = 1;
const LABEL_STORE = "labels";
const LOOKUP_LIMIT = 256;
const IMPORT_BATCH_LIMIT = 5000;
const LOOKUP_CACHE_LIMIT = 20000;
const MAX_LABEL_LENGTH = 63;
const MAX_SOURCE_COUNT = 100;
const DEFAULT_STATE = Object.freeze({
activeGeneration: null,
count: 0,
enabled: true,
revision: 0,
importedAt: null
});
let metaDatabasePromise = null;
let state = { ...DEFAULT_STATE };
let stateLoaded = false;
const generationDatabasePromises = new Map();
const pendingGenerationDeletions = new Set();
class LruCache {
constructor(limit) {
this.limit = limit;
this.values = new Map();
}
get(key) {
if (!this.values.has(key)) {
return undefined;
}
const value = this.values.get(key);
this.values.delete(key);
this.values.set(key, value);
return value;
}
set(key, value) {
if (this.values.has(key)) {
this.values.delete(key);
}
this.values.set(key, value);
if (this.values.size > this.limit) {
const oldestKey = this.values.keys().next().value;
this.values.delete(oldestKey);
}
}
clear() {
this.values.clear();
}
}
const lookupCache = new LruCache(LOOKUP_CACHE_LIMIT);
function requestAsPromise(request) {
return new Promise((resolve, reject) => {
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = () => {
reject(
request.error ??
new Error("An IndexedDB request failed.")
);
};
});
}
function transactionAsPromise(transaction) {
return new Promise((resolve, reject) => {
transaction.oncomplete = () => {
resolve();
};
transaction.onerror = () => {
reject(
transaction.error ??
new Error("An IndexedDB transaction failed.")
);
};
transaction.onabort = () => {
reject(
transaction.error ??
new Error("An IndexedDB transaction was aborted.")
);
};
});
}
function openMetaDatabase() {
if (metaDatabasePromise) {
return metaDatabasePromise;
}
metaDatabasePromise = new Promise((resolve, reject) => {
const request = indexedDB.open(
META_DB_NAME,
META_DB_VERSION
);
request.onupgradeneeded = () => {
const database = request.result;
if (!database.objectStoreNames.contains(META_STORE)) {
database.createObjectStore(META_STORE);
}
};
request.onsuccess = () => {
const database = request.result;
database.onversionchange = () => {
database.close();
metaDatabasePromise = null;
};
resolve(database);
};
request.onerror = () => {
metaDatabasePromise = null;
reject(
request.error ??
new Error("Could not open the metadata database.")
);
lookupCache.clear();
notifyFilteringChanged();
if (importState?.generation) {
scheduleGenerationDeletion(
importState.generation
);
}
if (activeGeneration) {
scheduleGenerationDeletion(
activeGeneration
);
}
return publicStatus(null);
}
api.runtime.onMessage.addListener((message) => {
if (!message || typeof message !== "object") {
return undefined;
}
switch (message.type) {
case "lookup":
return lookupHostnames(
message.hostnames
).then((blocked) => ({
blocked,
generation:
state.activeGeneration,
revision: state.revision
}));
case "get-status":
return getStatus();
case "set-settings":
return updateSettings(message);
case "begin-import":
return beginImport(message);
case "resume-import":
return resumeImport(message);
case "append-import-batch":
return appendImportBatch(message);
case "complete-import-source":
return completeImportSource(message);
case "finish-import":
return finishImport(message);
case "abandon-import":
case "cancel-import":
return abandonImport(message);
case "clear-data":
return clearAllData();
default:
return undefined;
}
});
ensureStateLoaded().catch((error) => {
console.error(
"Could not initialize SERP Domain Label Index:",
error
);
});