This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
/
sharedFunctions.js
349 lines (322 loc) · 15.5 KB
/
sharedFunctions.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
/**
* Author: Glagan
* See <https://github.com/Glagan/MyMangaDex> for more informations
*/
/**
* source: https: //stackoverflow.com/a/34491287/7794671
* Count the number of properties of an object to test if it's empty
* Could add more tests but it was enough for this extension
* @param {Object} obj The object to test
*/
function isEmpty(obj) {
for (let x in obj) { return false; }
return true;
}
/**
* Fetch data from the storage and directly return the real value
* Return undefined if the data isn't stored or the data as it was stored if it exist
* @param {string} key The key of the data to fetch in the storage
*/
async function storageGet(key) {
try {
if (key !== null) {
key = key + "";
}
let res;
if (CHROME) {
await browser.storage.local.get((key === null) ? null : key, function(result) {
res = (key === null) ? result : result[key];
});
return res;
} else {
res = await browser.storage.local.get((key === null) ? null : key);
return (key === null) ? res : res[key];
}
} catch(error) {
console.error(error);
}
}
/**
* Insert the data argument directly with the key argument, to avoid creationg an object when calling storage.set()
* If key is null, the data object just replace everything in storage
* @param {*} key The key of the data to insert or null
* @param {Object} data The data to insert
*/
async function storageSet(key, data) {
try {
return await browser.storage.local.set((key === null) ? data : {[key]:data});
} catch(error) {
console.error(error);
}
}
/**
* Get the options from local storage and put them in the global object MMD_options
* Also manage the updates and apply the modifications if there is
*/
async function loadOptions() {
let data = await storageGet("options");
// If there is nothing in the storage, default options
if (data === undefined) {
SimpleNotification.info(
"First time using MyMangaDex",
"It looks like it is your first time using MyMangaDex, for any help go to https://github.com/Glagan/MyMangaDex, and don't forget to look at the settings !",
{
sticky: true
}
);
await storageSet("options", defaultOptions);
return JSON.parse(JSON.stringify(defaultOptions));
} else {
// If options < last
if (data.version < defaultOptions.version) {
// I saw people using using 1.6.3 ??? So just in case...
if (data.version < 1.7) {
data.auto_md_list = false;
}
if (data.version < 1.8) {
// Options object rework
data = {
lastReadColor: data.colors.last_read,
lowerChaptersColor: data.colors.lower_chapter,
lastOpenColors: data.colors.last_open,
openedChaptersColor: data.colors.opened_chapters,
hideLowerChapters: data.hide_lower,
saveOnlyHigher: data.last_only_higher,
saveAllOpened: data.save_all_opened,
maxChapterSaved: data.max_save_opened,
updateMDList: data.auto_md_list,
showTooltips: true // New options to default
};
}
if (data.version < 1.9) {
data.highlightChapters = defaultOptions.highlightChapters;
data.showNotifications = defaultOptions.showNotifications;
data.showErrors = defaultOptions.showErrors;
data.version = 1.9;
SimpleNotificatin.info(
"MyMangaDex as been updated to 1.9",
"You can see the changelog on https://github.com/Glagan/MyMangaDex, new options have been added, you should check them out !"
);
}
if (data.version < 2.0) {
data.onlineSave = defaultOptions.onlineSave; // New options to default
data.onlineURL = defaultOptions.onlineURL;
data.username = defaultOptions.username;
data.password = defaultOptions.password;
data.isLoggedIn = defaultOptions.isLoggedIn;
data.token = defaultOptions.token;
data.version = 2.0;
SimpleNotificatin.info(
"MyMangaDex as been updated to 2.0",
"Online Save as been added, if you wish to use it you need to manually enable it. You can see the changelog on https://github.com/Glagan/MyMangaDex."
);
}
await storageSet("options", data);
} // Easy to add updates here, on another if and push the promise in the updates array
// Sub version option fix
if (data.showNoMal === undefined) {
data.showNoMal = defaultOptions.showNoMal;
await storageSet("options", data);
}
return data;
}
}
async function updateLocalStorage(manga, options) {
if (options.saveOnlyHigher) {
manga.lastMangaDexChapter = Math.max(manga.lastMangaDexChapter, manga.currentChapter.chapter);
} else {
manga.lastMangaDexChapter = manga.currentChapter.chapter;
}
await storageSet(manga.mangaDexId, {
mal: manga.myAnimeListId,
last: manga.lastMangaDexChapter,
chapters: manga.chapters
});
// Show a notification for updated last opened if there is no MyAnimeList id
if (options.showNotifications && manga.myAnimeListId == 0 && manga.currentChapter.chapter > manga.lastMangaDexChapter) {
SimpleNotificatin.success(
"Manga updated",
manga.name + " last open Chapter as been updated to " + manga.lastMangaDexChapter,
{
image: "https://mangadex.org/images/manga/" + manga.mangaDexId + ".thumb.jpg"
}
);
}
// Update online
if (options.onlineSave && options.isLoggedIn) {
// Build body
let body = {
mal: manga.myAnimeListId,
last: manga.lastMangaDexChapter,
options: {
"saveAllOpened": options.saveAllOpened,
"maxChapterSaved": options.maxChapterSaved
}
};
// Send the request
try {
let response = await fetch(options.onlineURL + "user/self/title/" + manga.mangaDexId, {
method: "POST",
mode: "cors",
headers: {
"Accept": "application/json",
"Content-Type": "application/json; charset=utf-8",
"X-Auth-Token": options.token
},
body: JSON.stringify(body)
});
if (response.status != 200) {
options.isLoggedIn = false;
}
} catch (error) {
options.isLoggedIn = false;
console.error(error);
}
if (options.isLoggedIn == false) {
if (options.showErrors) {
SimpleNotification.error(
"Couldn't save Online",
"The Online Service might have a problem, or your credentials has been changed. Go to the options to update it."
);
}
await storageSet("options", options);
}
}
}
function clearDomNode(node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
}
function uniqueGUID() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
function processMyAnimeListResponse(manga, text) {
manga.is_approved = !/class="badresult"/.test(text);
manga.exist = !/id="queryTitle"/.test(text);
// Comments
manga.comments = /add_manga_comments.+>(.*)</.exec(text)[1];
// Ask to discuss
manga.ask_to_discuss = /add_manga_is_asked_to_discuss.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text);
manga.ask_to_discuss = (manga.ask_to_discuss === null) ? 0 : parseInt(manga.ask_to_discuss[1]);
// Last read chapter
manga.lastMyAnimeListChapter = /add_manga_num_read_chapters.+value="(\d+)?"/.exec(text);
manga.lastMyAnimeListChapter = (manga.lastMyAnimeListChapter === null) ? 0 : parseInt(manga.lastMyAnimeListChapter[1]);
// Total times re-read
manga.total_reread = /add_manga_num_read_times.+value="(\d+)?"/.exec(text);
manga.total_reread = (manga.total_reread === null) ? 0 : parseInt(manga.total_reread[1]);
// Last read volume
manga.last_volume = /add_manga_num_read_volumes.+value="(\d+)?"/.exec(text);
manga.last_volume = (manga.last_volume === null) ? 0 : parseInt(manga.last_volume[1]);
// Retail volumes
manga.retail_volumes = /add_manga_num_retail_volumes.+value="(\d+)?"/.exec(text);
manga.retail_volumes = (manga.retail_volumes === null) ? 0 : parseInt(manga.retail_volumes[1]);
// Priority
manga.priority = /add_manga_priority.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text);
manga.priority = (manga.priority === null) ? 0 : parseInt(manga.priority[1]);
// Re-read value
manga.reread_value = /add_manga_reread_value.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text);
manga.reread_value = (manga.reread_value === null) ? "" : manga.reread_value[1];
// Score
manga.score = /add_manga_score.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text);
manga.score = (manga.score === null) ? "" : parseInt(manga.score[1]);
// SNS Post type
manga.sns_post_type = /add_manga_sns_post_type.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text);
manga.sns_post_type = (manga.sns_post_type === null) ? 0 : parseInt(manga.sns_post_type[1]);
// Start date
manga.start_date = {};
manga.start_date.month = (parseInt(/add_manga_start_date_month.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text)[1]) || "");
manga.start_date.day = (parseInt(/add_manga_start_date_day.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text)[1]) || "");
manga.start_date.year = (parseInt(/add_manga_start_date_year.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text)[1]) || "");
// Finish date
manga.finish_date = {};
manga.finish_date.month = (parseInt(/add_manga_finish_date_month.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text)[1]) || "");
manga.finish_date.day = (parseInt(/add_manga_finish_date_day.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text)[1]) || "");
manga.finish_date.year = (parseInt(/add_manga_finish_date_year.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text)[1]) || "");
// Status
manga.status = /add_manga_status.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text);
manga.status = (manga.status === null) ? 0 : parseInt(manga.status[1]);
// Storage type
manga.storage_type = /add_manga_storage_type.+\s.+value="(\d+)?"\s*selected="selected"/.exec(text);
manga.storage_type = (manga.storage_type === null) ? "" : manga.storage_type[1];
// Tags
manga.tags = /add_manga_tags.+>(.*)*</.exec(text)[1] || "";
// Is re-reading ?
manga.is_rereading = /name="add_manga\[is_rereading\]"\s*value="\d*"\s*checked="checked"/.test(text);
// Bonus : total volume and chapter
manga.total_volume = parseInt(/id="totalVol">(.*)?<\//.exec(text)[1]) || 0;
manga.total_chapter = parseInt(/id="totalChap">(.*)?<\//.exec(text)[1]) || 0;
// Is in the list
manga.in_list = (manga.status > 0);
}
function buildMyAnimeListBody(usePepper, manga, csrf, status = 1) {
let requestURL = "https://myanimelist.net/ownlist/manga/" + manga.myAnimeListId + "/edit?hideLayout";
if (usePepper) {
// Status is always set to reading, or we complet it if it's the last chapter, and so we fill the finish_date
manga.status = (manga.status == 2 || (manga.total_chapter > 0 && manga.currentChapter.chapter >= manga.total_chapter)) ? 2 : status;
// Set the start only if it's not already set and if we don't add it to PTR and if it was in ptr or not in the list
if (!manga.in_list && manga.status != 6 && manga.start_date.year == "") {
let MyDate = new Date();
manga.start_date.year = MyDate.getFullYear();
manga.start_date.month = MyDate.getMonth() + 1;
manga.start_date.day = MyDate.getDate();
manga.start_today = true;
}
// Set the finish date if it's the last chapter and not set
if (manga.status == 2 && manga.finish_date.year == "") {
let MyDate = new Date();
manga.finish_date.year = MyDate.getFullYear();
manga.finish_date.month = MyDate.getMonth() + 1;
manga.finish_date.day = MyDate.getDate();
manga.end_today = true;
}
// Start reading manga if it's the first chapter
if (!manga.in_list) {
// We have to change the url if we're adding the manga to the list, not editing
requestURL = "https://myanimelist.net/ownlist/manga/add?selected_manga_id=" + manga.myAnimeListId + "&hideLayout";
manga.in_list = true;
manga.started = true;
}
if (manga.is_rereading && manga.total_chapter > 0 && manga.currentChapter.chapter >= manga.total_chapter) {
manga.completed = true;
manga.is_rereading = false;
manga.total_reread++;
}
} else {
manga.status = status;
}
// Update
manga.lastMyAnimeListChapter = Math.floor(manga.currentChapter.chapter);
// Prepare the body
let body = "entry_id=0&";
body += "manga_id=" + manga.myAnimeListId + "&";
body += encodeURIComponent("add_manga[status]") + "=" + manga.status + "&";
body += encodeURIComponent("add_manga[num_read_volumes]") + "=" + manga.currentChapter.volume + "&";
body += "last_completed_vol=&";
body += encodeURIComponent("add_manga[num_read_chapters]") + "=" + manga.lastMyAnimeListChapter + "&";
body += encodeURIComponent("add_manga[score]") + "=" + manga.score + "&";
body += encodeURIComponent("add_manga[start_date][day]") + "=" + manga.start_date.day + "&";
body += encodeURIComponent("add_manga[start_date][month]") + "=" + manga.start_date.month + "&";
body += encodeURIComponent("add_manga[start_date][year]") + "=" + manga.start_date.year + "&";
body += encodeURIComponent("add_manga[finish_date][day]") + "=" + manga.finish_date.day + "&";
body += encodeURIComponent("add_manga[finish_date][month]") + "=" + manga.finish_date.month + "&";
body += encodeURIComponent("add_manga[finish_date][year]") + "=" + manga.finish_date.year + "&";
body += encodeURIComponent("add_manga[tags]") + "=" + encodeURIComponent(manga.tags) + "&";
body += encodeURIComponent("add_manga[priority]") + "=" + manga.priority + "&";
body += encodeURIComponent("add_manga[storage_type]") + "=" + manga.storage_type + "&";
body += encodeURIComponent("add_manga[num_retail_volumes]") + "=" + manga.retail_volumes + "&";
body += encodeURIComponent("add_manga[num_read_times]") + "=" + manga.total_reread + "&";
body += encodeURIComponent("add_manga[reread_value]") + "=" + manga.reread_value + "&";
body += encodeURIComponent("add_manga[comments]") + "=" + encodeURIComponent(manga.comments) + "&";
body += encodeURIComponent("add_manga[is_asked_to_discuss]") + "=" + manga.ask_to_discuss + "&";
body += encodeURIComponent("add_manga[sns_post_type]") + "=" + manga.sns_post_type + "&";
if (manga.is_rereading) {
body += encodeURIComponent("add_manga[is_rereading]") + "=1&";
}
body += "submitIt=0&";
body += encodeURIComponent("csrf_token") + "=" + csrf;
return {
requestURL: requestURL,
body: body
};
}