-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessageHandlers.js
104 lines (91 loc) · 3.5 KB
/
messageHandlers.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
import db from "./db.js";
import utils from "./utils.js";
import { getPageInfo, askPageForInfo } from "./pageInfo.js";
import { stringTranslations, translateString } from "./stringTranslations.js";
export default {
async isDBReady() {
await db.getDB();
},
async pageInfo(args) {
var tab = args.tab;
var pageInfo = await getPageInfo(tab);
return pageInfo;
},
async translatePage(args) {
var tab = args.tab;
var pageInfo = await getPageInfo(tab);
if (args.update) {
chrome.scripting.executeScript({
target: {tabId: tab.id},
func: injections.updatePageContent,
args: [pageInfo, args.language, stringTranslations]
});
}
return pageInfo;
},
async searchPageContent(args) {
return await db.searchPageContent(args.query, args.language);
},
async installedLanguages(args) {
let idb = await db.getDB();
let cursor = await idb.transaction("pages").store.index("type").openCursor(IDBKeyRange.only("entry"));
let installedLanguages = Object.keys(cursor.value.title);
return installedLanguages;
},
async languageModification(args) {
let idb = await db.getDB();
if (args.action == "add")
await db.addLanguage(idb, args.language);
else
await db.deleteLanguage(idb, args.language);
},
async requestElementsTranslation(args) {
let language = args.language;
let promises = [];
args.elements.forEach((element) => {
if (["entry", "card", "book", "season"].includes(element.type)) {
// These are pages in our database, we find them and update the URL too
var query = {
type: element.type,
title: element.title
};
if (element.type != "season") {
query.url = utils.normalizeURL(element.url);
}
promises.push(
db.getStoredPage(query)
.then((dbPage) => {
if (!dbPage) throw {error: "Error retrieving page", query};
if (dbPage.type != "season") {
// we don't store the url for seasons
return db.setStoredPage(Object.assign(dbPage, {url: utils.normalizeURL(element.url)}));
} else {
return dbPage;
}
})
.catch((ex) => {
// We didn't find the page, try translating as string
utils.debugLog(ex);
return {
type: element.type,
title: {
en: element.title,
[language]: translateString(element.title, language)
}
}
})
);
} else {
// We try to find a string translation
promises.push({
type: "string",
title: {
en: element.title,
[language]: translateString(element.title, language)
}
});
}
});
return await Promise.all(promises);
}
}