-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmangadex-downloader.user.js
276 lines (257 loc) · 13.7 KB
/
mangadex-downloader.user.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
// ==UserScript==
// @name MangaDex Downloader
// @version 0.19
// @description A userscript to add download-buttons to mangadex
// @author icelord
// @homepage https://github.com/xicelord/mangadex-scripts
// @updateURL https://raw.githubusercontent.com/xicelord/mangadex-scripts/master/mangadex-downloader.user.js
// @downloadURL https://raw.githubusercontent.com/xicelord/mangadex-scripts/master/mangadex-downloader.user.js
// @match https://mangadex.org/settings
// @match https://www.mangadex.org/settings
// @match https://mangadex.org/title/*
// @match https://www.mangadex.org/title/*
// @icon https://mangadex.org/favicon.ico
// @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.3/FileSaver.min.js
// @grant GM_xmlhttpRequest
// @grant GM.setValue
// @grant GM.getValue
// ==/UserScript==
(function() {
'use strict';
//Required to retrieve iso_codes
let language_iso = {
'Arabic': 'ara',
'Bengali': 'ben',
'Bulgarian': 'bul',
'Catalan': 'cat',
'Chinese': 'chi',
'Czech': 'cze',
'Danish': 'dan',
'Dutch': 'dut',
'English': 'eng',
'Filipino': 'fil',
'Finnish': 'fin',
'French': 'fre',
'German': 'ger',
'Greek': 'gre',
'Hungarian': 'hun',
'Indonesian': 'ind',
'Italian': 'ita',
'Japanese': 'jpn',
'Korean': 'kor',
'Malaysian': 'may',
'Mongolian': 'mon',
'Persian': 'per',
'Polish': 'pol',
'Portuguese (Brazil)': 'por',
'Portuguese (Portugal)': 'por',
'Romanian': 'rum',
'Russian': 'rus',
'Serbo-Croatian': 'hrv',
'Spanish (LATAM)': 'spa',
'Spanish (Spain)': 'spa',
'Swedish': 'swe',
'Thai': 'tha',
'Turkish': 'tur',
'Vietnamese': 'vie'
};
//Settings or download
if (document.URL === 'https://mangadex.org/settings') {
//Add tab
$('.nav-item').last().after('<li class="nav-item">' +
'<a class="nav-link" href="#download_settings" aria-controls="download_settings" data-toggle="tab">' +
'<span class="fas fa-download fa-fw" aria-hidden="true"></span>' +
'<span class="d-none d-lg-inline"> Downloads</span>' +
'</a>' +
'</li>');
//Add options
$('#supporter_settings').after('<div role="tabpanel" class="tab-pane fade" id="download_settings">' +
'<div class="form-horizontal">' +
'<div class="form-group row">' +
'<label for="file-extension" class="col-sm-3 control-label">Extension:</label>' +
'<div class="col-sm-9">' +
'<select class="form-control selectpicker" id="file-extension">' +
'<option ' + (((localStorage.getItem("file-extension") || '.zip') === '.zip' ) ? 'selected ' : '') + 'value=".zip">.zip</option>' +
'<option ' + (((localStorage.getItem("file-extension") || '.zip') === '.cbz' ) ? 'selected ' : '') + 'value=".cbz">.cbz</option>' +
'</select>' +
'</div>' +
'</div>' +
'<div class="form-group row">' +
'<label for="chapter-info" class="col-sm-3 control-label">Save release info:</label>' +
'<div class="col-sm-9">' +
'<select class="form-control selectpicker" id="chapter-info">' +
'<option ' + ((localStorage.getItem("chapter-info") === '0' ) ? 'selected ' : '') + 'value="0">Disabled</option>' +
'<option ' + ((localStorage.getItem("chapter-info") === '1' ) ? 'selected ' : '') + 'value="1">Text file</option>' +
'<option ' + ((localStorage.getItem("chapter-info") === '2' ) ? 'selected ' : '') + 'value="2">JSON</option>' +
'</select>' +
'</div>' +
'</div>' +
'<div class="form-group row">' +
'<label for="parallel-downloads" class="col-sm-3 control-label">Parallel Downloads:</label>' +
'<div class="col-sm-9">' +
'<input type="numbers" class="form-control" id="parallel-downloads" value="' + (localStorage.getItem("parallel-downloads") || 3) + '" />' +
'</div>' +
'</div>' +
'<div class="form-group row">' +
'<div class="col-sm-offset-3 col-sm-9">' +
'<button type="submit" class="btn btn-default" id="save_downloader_settings"><span class="fas fa-save fa-fw" aria-hidden="true"></span> Save</button>' +
'</div>' +
'</div>' +
'</div>' +
'</div>');
//Add handler to save options
document.getElementById('save_downloader_settings').addEventListener('click', () => {
localStorage.setItem('file-extension', document.getElementById('file-extension').value);
localStorage.setItem('parallel-downloads', parseInt(document.getElementById('parallel-downloads').value));
localStorage.setItem('chapter-info', document.getElementById('chapter-info').value);
alert('Updated settings!');
}, false);
return;
}
//Inject download-buttons
$('div.chapter-row').find('div.order-lg-2').each((i, element) => {
if (i === 0) return;
let id = $(element).find('a')[0].href.split('/').pop();
$('<span title="Download" id="dl-' + id + '" class="fas fa-download" style="color: rgb(102, 102, 102); cursor: pointer; margin: 0px 5px;"></span>').prependTo(element);
document.getElementById('dl-' + id).addEventListener('click', () => { downloadChapter(id); }, false);
});
//Function to download a chapter (called by download-buttons)
function downloadChapter(id) {
//Inject progressbar
$(
'<div id="progress-out-' + id + '" style="width: 50px; margin-bottom: 2px; background-color: grey;">' +
'<div id="progress-in-' + id + '" style="width: 0%; height: 7px; background-color: green;">' +
'</div>' +
'</div>').insertBefore($('#dl-' + id));
//Fetch page-urls and download them
getChapterData(id, (err, chapter_data) => {
if (err) {
alert('The page-urls could not be fetched. Check the console for more details.');
setProgress(id, -1);
console.error(err);
} else {
//Prepare
let link = $('a[href="/chapter/' + id + '"]');
const chapterInfo = {
manga: $("h6.card-header .mx-1").text().trim(),
altnames: $('.fa-book').map((i, book) => {
if (i > 2)
return $(book).parent().text().trim();
}).get(),
link: 'https://mangadex.org/chapter/' + chapter_data.id,
chapter: chapter_data.chapter,
volume: chapter_data.volume || null,
title: chapter_data.title,
groups: link.parent().parent().find('div:nth-child(7) > a').map((i, group) => {
return group.innerText;
}).get(),
genres: $('.genre').map((i, genre) => {
return genre.text;
}).get(),
uploader: {
id: parseInt(link.parent().parent().find('div:nth-child(8) > a').attr('href').replace(/\/user\//, '')),
username: link.parent().parent().find('div:nth-child(8)').text().trim()
},
posted: new Date(chapter_data.timestamp * 1000),
language: chapter_data.lang_name,
translated: (chapter_data.lang_name !== $('h6 > img').attr('title')),
images: chapter_data.page_array.map(function(filename) {
return chapter_data.server + chapter_data.hash + '/' + filename;
})
};
//Fetch all pages using JSZip
let zip = new JSZip();
let zipFilename =
chapterInfo.manga +
(chapterInfo.language == "English" ? "" : " [" + language_iso[chapterInfo.language] + "]") +
" - c" + (chapterInfo.chapter < 100 ? chapterInfo.chapter < 10 ? '00' + chapterInfo.chapter : '0' + chapterInfo.chapter : chapterInfo.chapter) +
(chapterInfo.volume ? " (v" + (chapterInfo.volume < 10 ? '0' + chapterInfo.volume : chapterInfo.volume) + ")" : "") +
" [" + chapterInfo.groups + "]" +
(localStorage.getItem("file-extension") || '.zip');
let page_count = chapterInfo.images.length;
let active_downloads = 0;
let failed = false;
//Build metadata-file based on setting
if (localStorage.getItem("chapter-info") == '1') {
let textFile = '';
textFile += chapterInfo.manga + '\n';
textFile += chapterInfo.altnames.join(', ') + '\n';
textFile += chapterInfo.link + '\n\n';
textFile += 'Chapter: ' + chapterInfo.chapter + '\n';
textFile += 'Volume: ' + (chapterInfo.volume !== null ? chapterInfo.volume : 'Unknown') + '\n';
textFile += 'Title: ' + chapterInfo.title + '\n';
textFile += 'Groups: ' + chapterInfo.groups + '\n';
textFile += 'Genres: ' + chapterInfo.genres.join(', ') + '\n';
textFile += 'Uploader: ' + chapterInfo.uploader.username + ' (ID: ' + chapterInfo.uploader.id + ')\n';
textFile += 'Posted: ' + chapterInfo.posted + '\n';
textFile += 'Language: ' + chapterInfo.language + (chapterInfo.translated ? ' (TL) \n' : '\n');
textFile += 'Length: ' + chapterInfo.images.length + '\n\n';
chapterInfo.images.forEach((image, i) => {
textFile += 'Image ' + (i +1) + ': ' + image + '\n';
});
textFile += '\n\nDownloaded at ' + (new Date()) + '\n';
textFile += 'Generated by MangaDex Downloader. https://github.com/xicelord/mangadex-scripts';
zip.file('info.txt', textFile.replace(/\n/gi, '\r\n'));
} else if (localStorage.getItem("chapter-info") == '2') {
zip.file('info.json', JSON.stringify(chapterInfo, null, 4));
}
let page_urls = chapterInfo.images;
let interval = setInterval(() => {
if (active_downloads < (localStorage.getItem("parallel-downloads") || 3) && page_urls.length > 0) {
let to_download = page_urls.shift();
let current_page = page_count - page_urls.length;
let page_filename =
(chapterInfo.manga +
(chapterInfo.language == "English" ? "" : " [" + language_iso[chapterInfo.language] + "]") +
" - c" + (chapterInfo.chapter < 100 ? chapterInfo.chapter < 10 ? '00' + chapterInfo.chapter : '0' + chapterInfo.chapter : chapterInfo.chapter) +
(chapterInfo.volume ? " (v" + (chapterInfo.volume < 10 ? '0' + chapterInfo.volume : chapterInfo.volume) + ")" : "") +
" - p" + (current_page < 100 ? current_page < 10 ? '00' + current_page : '0' + current_page : current_page) +
" [" + chapterInfo.groups + "]" +
'.' + to_download.split('.').pop())
.replace(/[\/\?<>\\:\*\|":\x00-\x1f\x80-\x9f]/gi, '_')
active_downloads++;
GM_xmlhttpRequest({
method: 'GET',
url: to_download,
responseType: 'arraybuffer',
onload: function (data) {
zip.file(page_filename, data.response, { binary: true });
if (!failed) { setProgress(id, ((page_count -page_urls.length) /page_count) * 100); }
active_downloads--;
},
onerror: function (data) {
alert('A page-download failed. Check the console for more details.');
console.error(data);
clearInterval(interval);
setProgress(id, -1);
}
});
} else if (active_downloads === 0 && page_urls.length === 0) {
clearInterval(interval);
zip.generateAsync({ type: "blob" }).then((zipFile) => {
saveAs(zipFile, zipFilename);
setProgress(id, -1);
});
}
}, 500);
}
});
}
//Get all page-urls of chapter
function getChapterData(id, cb) {
$.get('https://mangadex.org/api/?id=' + id + '&type=chapter', (chapter_data) => {
cb(null, chapter_data);
}).fail((jqXHR, textStatus, error) => {
cb({ textStatus: textStatus, error: error }, null);
});
}
//Set progress of download for id
function setProgress(id, progress) {
if (progress !== -1) {
$('#progress-in-' + id).width(progress + '%');
} else {
$('#progress-out-' + id).remove();
}
}
})();