-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
278 lines (237 loc) · 9.45 KB
/
popup.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
import utils from "./utils.js";
var treeConfig = {
searchable: true,
showEmptyGroups: false,
groupOpenIconClass: "bi",
groupOpenIcon: "bi-chevron-down",
groupCloseIconClass: "bi",
groupCloseIcon: "bi-chevron-right",
linkIconClass: "bi",
linkIcon: "bi-link",
searchPlaceholderText: "",
};
// Promise wrapper for chrome.tabs.sendMessage
function sendMessage() {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(...arguments, response => {
if (response?.name == "BungieAPIError") {
reject(response);
} else {
resolve(response);
}
});
});
}
// Returns a Promise
function getInstalledLanguages() {
return sendMessage({
request: "installedLanguages",
args: {
tab: {id: currentTab.id, url: currentTab.url}
}
});
}
// Returns a Promise
function requestLanguageModification(language, action) {
return sendMessage({
request: "languageModification",
args: {
tab: {id: currentTab.id, url: currentTab.url},
language,
action
}
});
}
function ucfirst(str) {
return str[0].toUpperCase() + str.slice(1);
}
// Returns a Promise
function askForPageInfo() {
return sendMessage({
request: "pageInfo",
args: {
tab: {id: currentTab.id, url: currentTab.url}
}
});
}
// Returns a Promise
function translatePage(language = "en") {
return sendMessage({
request: "translatePage",
args: {
language,
tab: {id: currentTab.id, url: currentTab.url}
}
});
}
async function buildTree(language) {
document.getElementById("tree_loading").classList.remove("d-none");
document.getElementById("tree").classList.add("d-none");
var {loreTree} = await chrome.storage.local.get("loreTree");
loreTree = loreTree[language];
var treeRoot = document.getElementById("tree");
var catPlaceholder = document.querySelector("#tree_placeholders .placeholder-element:not(.leaf)");
var leafPlaceholder = document.querySelector("#tree_placeholders .placeholder-element.leaf");
treeRoot.innerHTML = "";
function fillTree(root, data) {
if (data[0].children) {
for (let child of data) {
let newCategory = catPlaceholder.cloneNode(true);
newCategory.id = utils.slugify(child.name);
newCategory.dataset.name = child.name;
if (child.url) {
newCategory.dataset.url = child.url;
}
newCategory.querySelector("a").innerHTML = child.name;
newCategory.querySelector("a").setAttribute("title", child.name);
newCategory.classList.remove("placeholder-element");
root.append(newCategory);
fillTree(newCategory.querySelector("ul"), child.children);
}
} else {
for (let child of data) {
let newLeaf = leafPlaceholder.cloneNode(true);
newLeaf.id = utils.slugify(child.name);
newLeaf.dataset.name = child.name;
newLeaf.dataset.url = child.url;
newLeaf.querySelector("a").innerHTML = child.name;
newLeaf.querySelector("a").setAttribute("title", child.name);
newLeaf.querySelector("a").href = "http://www.ishtar-collective.net" + child.url;
newLeaf.classList.remove("placeholder-element");
root.append(newLeaf);
}
}
}
fillTree(treeRoot, loreTree);
NavTree.createBySelector("#tree", treeConfig);
document.getElementById("tree_loading").classList.add("d-none");
document.getElementById("tree").classList.remove("d-none");
}
function expandTree(pageInfo) {
var navTree = NavTree.getOrCreateInstance(document.getElementById("tree"));
navTree.search({url: pageInfo.url, exact: true, showSiblings: true});
}
async function updateLanguageList(preferredLanguage) {
var installedLanguages = await getInstalledLanguages();
var oldLanguages = document.querySelectorAll("#language_manager .language:not(.placeholder-element)");
// Remove all languages
for (let i = 0; i < oldLanguages.length; i++) {
const elem = oldLanguages[i];
elem.parentNode.removeChild(elem);
}
// Get placeholder language to be cloned
var placeholder = document.querySelector("#language_manager .placeholder-element");
// Loop through all available Bungie languages and place it correctly
for (let [name, description] of Object.entries(allLanguages)) {
// Clone the placeholder
let languageElement = placeholder.cloneNode(true);
if (preferredLanguage == name) {
// This is the preferred language, it goes in the dropdown button and it's done
document.querySelector("#selected_language").innerHTML = description;
} else {
// Otherwise we place it in the correct dropdown with all needed event listeners
let targetDivId = installedLanguages.includes(name) ? "#installed_languages" : "#notinstalled_languages";
document.querySelector(targetDivId).appendChild(languageElement);
languageElement.setAttribute("data-language", name);
languageElement.querySelector(".language-name").innerHTML = description;
languageElement.querySelector(".language-add").addEventListener("click", async (evt) => {
evt.target.classList.add("hidden");
var el = evt.target.closest(".language");
el.querySelector(".language-loading").classList.remove("hidden");
var language = el.dataset.language;
await requestLanguageModification(language, "add");
await updateLanguageList(preferredLanguage);
});
languageElement.querySelector(".language-delete").addEventListener("click", async (evt) => {
evt.target.classList.add("hidden");
var el = evt.target.closest(".language");
el.querySelector(".language-loading").classList.remove("hidden");
var language = el.dataset.language;
await requestLanguageModification(language, "delete");
await updateLanguageList(preferredLanguage);
});
if (installedLanguages.includes(name)) {
languageElement.querySelector(".language-name").addEventListener("click", async (evt) => {
var language = evt.target.closest(".language").dataset.language;
await chrome.storage.sync.set({language});
await buildTree(language);
await updateLanguageList(language);
if (currentTab.url?.includes("ishtar-collective.net/entries/")) {
let pageInfo = await askForPageInfo();
if (pageInfo) {
expandTree(pageInfo);
}
}
});
}
languageElement.classList.remove("placeholder-element");
}
}
}
var currentTab;
var allLanguages = {
"en": "English",
"fr": "Français",
"es": "Español",
"es-mx": "Español (México)",
"de": "Deutsch",
"it": "Italiano",
"ja": "日本語",
"pt-br": "Português (Brasil)",
"ru": "Русский",
"pl": "Polski",
"ko": "한국어",
"zh-cht": "繁體中文",
"zh-chs": "简体中文"
};
function openLink(url) {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
var tab = tabs[0];
chrome.tabs.update(tab.id, {url});
});
}
main();
async function main() {
var tabs = await chrome.tabs.query({active: true, currentWindow: true});
currentTab = tabs[0];
var preferredLanguage = await chrome.storage.sync.get("language");
var language = preferredLanguage.language || "en";
try {
await sendMessage({request: "isDBReady"});
await updateLanguageList(language);
var pageInfo;
if (currentTab.url && currentTab.url.includes("ishtar-collective.net")) {
pageInfo = await askForPageInfo();
if (pageInfo.currentLanguage != language) {
translatePage(language);
}
}
await buildTree(language);
if (pageInfo) {
expandTree(pageInfo);
}
document.querySelector("header a").addEventListener("click", (evt) => {
evt.preventDefault();
openLink(evt.target.closest("a").href);
});
document.querySelector("#tree").addEventListener("click", (evt) => {
let closestLi = evt.target.closest("li");
if (closestLi?.classList.contains("leaf")) {
evt.preventDefault();
openLink(evt.target.href);
}
});
document.querySelector('#nav-tree-search').addEventListener("keypress", (evt) => {
if (evt.key === 'Enter') {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
var tab = tabs[0];
chrome.tabs.update(tab.id, {url: `http://www.ishtar-collective.net/search/${evt.target.value}`});
});
}
});
} catch (ex) {
if (ex.name == "BungieAPIError") {
document.getElementById("api_error_alert").classList.remove("d-none");
}
}
}