-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
602 lines (559 loc) · 18.7 KB
/
background.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* Copyright (C) 2024 [Ayama].
* Author: Ayama (https://github.com/1818180)
* This file is part of [contextsaver].
*
* [contextsaver] 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.
*
* [contextsaver] 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 [contextsaver]. If not, see <https://www.gnu.org/licenses/>.
*/
// 监听标签页激活
chrome.tabs.onActivated.addListener((activeInfo) => {
sendTabActivatedMessage(activeInfo.tabId);
});
// 向指定 tab 发送激活消息
function sendTabActivatedMessage(tabId) {
chrome.tabs.sendMessage(tabId, { action: "tabActivated" }, (response) => {
if (chrome.runtime.lastError) {
console.warn("消息发送失败,可能目标标签页没有注入 content.js");
}
});
}
//创建打开全局数据库
let db;
let transaction;
let objectStore;
//获取(创建)标注数据库
function openDatabase() {
return new Promise((resolve, reject) => {
const request = indexedDB.open("ContextSaver", 1);
request.onupgradeneeded = (event) => {
db = event.target.result;
if (!db.objectStoreNames.contains("annotations")) {
const store = db.createObjectStore("annotations", { keyPath: "date"});
store.createIndex("context", "context", { unique: false });
store.createIndex("annotation", "annotation", { unique: false });
store.createIndex("url", "url", { unique: false });
store.createIndex("title", "title", { unique: false });
store.createIndex("color", "color", { unique: false });
store.createIndex("note", "note", { unique: false });
store.createIndex("star", "star", { unique: false });
store.createIndex("location", "location", { unique: false });
store.createIndex("date", "date", { unique: true });
store.createIndex("index", "index", { unique: false });
}
};
request.onsuccess = (event) => {
db = event.target.result;
resolve(db);
};
request.onerror = (event) => {
console.error("Failed to store annotation:", event.target.error);
reject(event.target.error);
}
});
}
//获取(创建)网站信息库
//objectname ['readlater','webinfo']
function openContentDatabase() {
return new Promise((resolve, reject) => {
const request = indexedDB.open("ContentDB", 1);
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains('readlater')) {
const store = db.createObjectStore('readlater', { keyPath: "url"});
store.createIndex("url", "url", { unique: true });
store.createIndex("title", "title", { unique: false });
store.createIndex("date", "date", { unique: false });
store.createIndex("icon", "icon", { unique: false });
store.createIndex("img", "img", { unique: false });
store.createIndex("lan", "lan", { unique: false });
}
if (!db.objectStoreNames.contains('webinfo')) {
const store = db.createObjectStore('webinfo', { keyPath: "url"});
store.createIndex("url", "url", { unique: true });
store.createIndex("title", "title", { unique: false });
store.createIndex("date", "date", { unique: false });
store.createIndex("icon", "icon", { unique: false });
store.createIndex("img", "img", { unique: false });
store.createIndex("lan", "lan", { unique: false });
}
}
request.onsuccess = (event) => {
const db = event.target.result;
resolve(db);
};
request.onerror = (event) => {
console.error("Failed to open ContentDB:", event.target.error);
reject(event.target.error);
}
});
}
openDatabase();
openContentDatabase();
//获取所有数据
function getAllAnnotations(callback) {
const getallobjectStore = db
.transaction("annotations", "readonly")
.objectStore("annotations");
const getallRequest = getallobjectStore.getAll();
getallRequest.onsuccess = (event) => {
const allAnnotations = event.target.result;
callback(allAnnotations);
};
getallRequest.onerror = (event) => {
console.error("获取数据失败:", event.target.error);
return `获取数据失败:${event.target.error}`
};
}
function getAnnonamelist(aimtype) {
return new Promise((resolve, reject) => {
const getallobjectStore = db
.transaction("annotations", "readonly")
.objectStore("annotations");
const nameIndex = getallobjectStore.index(aimtype);
const names = [];
const cursorRequest = nameIndex.openCursor();
cursorRequest.onsuccess = function(event) {
const cursor = event.target.result;
if (cursor) {
const anno = cursor.value[aimtype];
if (anno && !names.includes(anno)) {
names.push(cursor.value[aimtype]);
}
cursor.continue();
} else {
resolve(names);
}
};
cursorRequest.onerror = function(event) {
reject('Error retrieving customer names from index');
};
});
}
//由id找anno
function getAnnoFromID(annoID) {
return new Promise((resolve, reject) => {
const transaction = db.transaction("annotations", "readonly");
const objectStore = transaction.objectStore("annotations");
const request = objectStore.get(annoID);
request.onsuccess = function(event) {
const result = event.target.result;
if (result) {
resolve(result.annotation);
} else {
reject("没有找到对应的 annotation");
}
};
request.onerror = function(event) {
reject("查询过程中发生错误");
};
});
}
//通过排序返回单个anno数据,为了获得插件的最早使用时间
function getFirstAnno() {
return new Promise((resolve, reject) => {
const transaction = db.transaction("annotations", "readonly");
const objectStore = transaction.objectStore("annotations");
const firstRequest = objectStore.openCursor();
let firstAnno = null;
firstRequest.onsuccess = function(event) {
const cursor = event.target.result;
if (cursor) {
firstAnno = cursor.value;
resolve(firstAnno);
} else {
reject("没有找到任何 annotation");
}
};
});
}
//找到一个单词所有的语境信息
function getOneInfo(condition, content) {
return new Promise((resolve, reject) => {
const transaction = db.transaction("annotations", "readonly");
const objectStore = transaction.objectStore("annotations");
const annoIndex = objectStore.index(condition);
const oneinfo = [];
const cursorRequest = annoIndex.openCursor(IDBKeyRange.only(content));
cursorRequest.onsuccess = function(event) {
const cursor = event.target.result;
if (cursor) {
oneinfo.push(cursor.value);
cursor.continue();
} else {
resolve(oneinfo);
}
};
cursorRequest.onerror = function() {
reject("Error retrieving annotation information");
};
});
}
//找到网页的第一个anno信息
function getUrlFirstAnno(condition,content) {
return new Promise((resolve, reject) => {
const transaction = db.transaction("annotations", "readonly");
const objectStore = transaction.objectStore("annotations");
const aimIndex = objectStore.index(condition);
const range = IDBKeyRange.only(content);
const request = aimIndex.get(range);
request.onsuccess = function(event) {
const cursor = event.target.result;
if (cursor) {
resolve(cursor);
} else {
resolve('');
}
};
request.onerror = function() {
reject(`getUrlFirstAnno 失败${error}`);
};
});
}
//找到某事件范围内,某一type的内容list
function getOneBound(bound, aimtype, uniquelist) {
return new Promise((resolve, reject) => {
const transaction = db.transaction("annotations", "readonly");
const objectStore = transaction.objectStore("annotations");
const annoIndex = objectStore.index('date');
const oneinfo = [];
const cursorRequest = annoIndex.openCursor(IDBKeyRange.bound(bound[0], bound[1]));
cursorRequest.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
const aimvalue = cursor.value[aimtype];
if (uniquelist === true && !oneinfo.includes(aimvalue)) {
oneinfo.push(aimvalue);
}
if (uniquelist === false) {
oneinfo.push(aimvalue);
}
cursor.continue();
} else {
resolve(oneinfo);
}
};
cursorRequest.onerror = function() {
reject("Error retrieving annotation information");
};
});
}
//更新anno信息
function updateAnno(annoID, annotype, newvalue) {
return new Promise((resolve, reject) => {
const transaction = db.transaction("annotations", "readwrite");
const objectStore = transaction.objectStore("annotations");
const request = objectStore.get(annoID * 1);
request.onsuccess = function(event) {
const oldanno = event.target.result;
if (oldanno) {
oldanno[annotype] = newvalue;
const updateRequest = objectStore.put(oldanno);
updateRequest.onsuccess = function() {
resolve("更新成功!(●'◡'●)");
};
updateRequest.onerror = function(event) {
reject("更新过程中发生错误");
};
} else {
reject("没有找到对应的 annotation");
}
};
request.onerror = function(event) {
reject("查询过程中发生错误");
};
});
}
//删除anno信息
function deleteAnno(annoID) {
return new Promise((resolve, reject) => {
const transaction = db.transaction("annotations", "readwrite");
const objectStore = transaction.objectStore("annotations");
const request = objectStore.delete(annoID * 1);
request.onsuccess = function(event) {
resolve("删除成功!(●'◡'●)");
};
request.onerror = function(event) {
reject("查询过程中发生错误");
};
});
}
//objectname ['readlater','webinfo'] 用url返回web信息
function contentDBProceer(webdb, objname, processtype, processcontent) {
return new Promise((resolve, reject) => {
if (processtype === 'save') {
const transaction = webdb.transaction(objname, "readwrite");
const objectStore = transaction.objectStore(objname);
const request = objectStore.add(processcontent);
request.onsuccess = function(event) {
resolve("添加成功!(●'◡'●)");
};
request.onerror = function(event) {
reject("查询过程中发生错误");
};
} else if (processtype === 'saveall') {
const transaction = webdb.transaction(objname, "readwrite");
const objectStore = transaction.objectStore(objname);
const promises = processcontent.map(anno => {
return new Promise((resolve, reject) => {
const request = objectStore.add(anno);
request.onsuccess = function(event) {
resolve();
};
request.onerror = function(event) {
reject("查询过程中发生错误");
};
});
});
Promise.all(promises)
.then(() => {
resolve("所有数据已成功添加!(●'◡'●)");
})
.catch(err => {
reject(err);
});
} else if (processtype === 'delete') {
const transaction = webdb.transaction(objname, "readwrite");
const objectStore = transaction.objectStore(objname);
const request = objectStore.delete(processcontent);
request.onsuccess = function(event) {
resolve("删除成功!(●'◡'●)");
};
request.onerror = function(event) {
reject("查询过程中发生错误");
};
//获取所有网站数据
} else if (processtype === 'get') {
const transaction = webdb.transaction(objname, "readonly");
const objectStore = transaction.objectStore(objname);
const request = objectStore.getAll();
request.onsuccess = function(event) {
const laterList = event.target.result;
resolve(laterList);
};
request.onerror = function(event) {
reject("查询过程中发生错误");
};
//返回特定url的webinfo
} else if (processtype === 'getsome') {
const transaction = webdb.transaction(objname, "readonly");
const objectStore = transaction.objectStore(objname);
const urlIndex = objectStore.index('url');
const request = urlIndex.openCursor(IDBKeyRange.only(processcontent));
request.onsuccess = function(event) {
const cursor = event.target.result;
if (cursor) {
resolve(cursor.value);
} else {
resolve('');
}
};
request.onerror = function(event) {
reject("查询过程中发生错误");
};
}
});
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.type === "getData") {
openDatabase().then((db) => {
if (db) {
getAllAnnotations(function(allAnnotations) {
sendResponse(allAnnotations);
});
}
}).catch((error) => {
sendResponse("getData链接数据库失败");
});
return true;
}
//返回所有不重复的标注的列表
if (request.type === "getAnnonamelist") {
const aimtype = request.aimtype;
(async function handleRequest() {
try {
const db = await openDatabase();
if (db) {
const list = await getAnnonamelist(aimtype);
sendResponse(list);
}
} catch (error) {
sendResponse(`getAnnonamelist失败${error}`)
}
})();
return true;
}
//用date id找到一个anno所有的语境信息
if (request.type === "getOneAnnoInfo") {
const annoid = request.anno;
const condition = 'annotation';
(async function handleRequest() {
try {
const db = await openDatabase();
if (db) {
const anno = await getAnnoFromID(annoid);
const oneannolist = await getOneInfo(condition,anno);
sendResponse(oneannolist);
}
} catch (error) {
sendResponse(`getOneAnnoInfo失败${error}`)
}
})();
return true;
}
//查找一个网站的所有的语境信息
if (request.type === "getOneInfo") {
const webURL = request.content;
const condition = request.condition;
(async function handleRequest() {
try {
const db = await openDatabase();
if (db) {
const oneannolist = await getOneInfo(condition,webURL);
sendResponse(oneannolist);
}
} catch (error) {
sendResponse(`getOneInfo失败${error}`)
}
})();
return true;
}
//查找所有网站的第一个anno信息,返回[]
if (request.type === "getUrlFirstAnno") {
const aimtype = request.aimtype;
(async function handleRequest() {
try {
const db = await openDatabase();
if (db) {
const urllist = await getAnnonamelist(aimtype);
let urlannolist = [];
if (urllist) {
for (const url of urllist) {
const urlanno = await getUrlFirstAnno('url',url);
urlannolist.push(urlanno);
}
}
sendResponse(urlannolist);
}
} catch (error) {
sendResponse(`getUrlFirstAnno 失败${error}`)
}
})();
return true;
}
//筛查一个范围内的所有anno信息
if (request.type === "getOneBound") {
const bound = request.bound;
const aimtype = request.aimtype;
const uniquelist = request.uniquelist;
(async function handleRequest() {
try {
const db = await openDatabase();
if (db) {
const oneannolist = await getOneBound(bound,aimtype,uniquelist);
sendResponse(oneannolist);
}
} catch (error) {
sendResponse(`getOneBound失败${error}`)
}
})();
return true;
}
//筛查一个范围内的所有anno信息
if (request.type === "getFirstAnno") {
(async function handleRequest() {
try {
const db = await openDatabase();
if (db) {
const firstanno = await getFirstAnno();
sendResponse(firstanno);
}
} catch (error) {
sendResponse(`getFirstAnno 失败${error}`)
}
})();
return true;
}
if (request.type === "updateAnno") {
const annoID = request.anno;
const annotype = request.annotype;
const newvalue = request.newvalue;
(async function handleRequest() {
try {
const db = await openDatabase();
if (db) {
const result = await updateAnno(annoID, annotype, newvalue);
sendResponse(result);
}
} catch (error) {
sendResponse(`getOneInfo失败${error}`)
}
})();
return true;
}
if (request.type === "deleteAnno") {
const annoID = request.annoID;
(async function handleRequest() {
try {
const db = await openDatabase();
if (db) {
const result = await deleteAnno(annoID);
sendResponse(result);
}
} catch (error) {
sendResponse(`deleteAnno失败${error}`)
}
})();
return true;
}
if (request.type === "addData") {
openDatabase().then((db) => {
if (db) {
const addObjectStore = db
.transaction("annotations", "readwrite")
.objectStore("annotations");
const annoname = request.annoinfo.annotation;
const addRequest = addObjectStore.add(request.annoinfo);
addRequest.onsuccess = (event) => {
sendResponse(`已经存入${annoname}`);
};
addRequest.onerror = (event) => {
console.error("Error adding data:", event.target.error);
sendResponse(`存入${annoname}失败,问题:${event.target.error}`);
};
}
}).catch((error) => {
sendResponse(`Database not opened yet, unable to add data. ${error}`);
});
return true;
}
//处理稍后读数据库
//objectname ['readlater','webinfo']
if (request.type === "contentDBProceer") {
const objname = request.objName;
const processtype = request.processType;
const processcontent = request.processContent;
(async function handleRequest() {
try {
const contentdb = await openContentDatabase();
if (contentdb) {
const result = await contentDBProceer(contentdb,objname, processtype, processcontent);
sendResponse(result);
}
} catch (error) {
sendResponse(`contentDBProceer 失败${error}`)
}
})();
return true;
}
});