forked from gianghd/userscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metruyenchu.net.user.js
296 lines (259 loc) · 8.87 KB
/
metruyenchu.net.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// ==UserScript==
// @name MeTruyen downloader
// @name:vi MeTruyen downloader
// @namespace http://tampermonkey.net/
// @description Tải truyện từ MeTruyen định dạng EPUB.
// @description:vi Tải truyện từ MeTruyen định dạng EPUB.
// @version 1
// @icon https://i.imgur.com/FQY8btq.png
// @author FixBug
// @match https://metruyenchu.net/*
// @match https://truyenchu.vn/*
// @require https://code.jquery.com/jquery-3.5.1.min.js
// @require https://unpkg.com/[email protected]/dist/jszip.min.js
// @require https://unpkg.com/[email protected]/dist/FileSaver.min.js
// @require https://unpkg.com/[email protected]/ejs.min.js
// @require https://unpkg.com/[email protected]/dist/jepub.min.js
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?v=a834d46
// @noframes
// @connect self
// @connect 8cache.com
// @run-at document-idle
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// ==/UserScript==
(function ($, window, document) {
'use strict';
/**
* Nhận cảnh báo khi có chương bị lỗi
*/
var errorAlert = false;
/**
* Thời gian giãn cách giữa 2 lần tải
* @type {Number}
*/
var downloadDelay = 0;
function cleanHtml(str) {
str = str.replace(/\s*Chương\s*\d+\s?:[^<\n]/, '');
str = str.replace(/[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF]+/gm, ''); // eslint-disable-line
return '<div>' + str + '</div>';
}
function downloadError(mess, err, server) {
downloadStatus('danger');
if (errorAlert) errorAlert = confirm('Lỗi! ' + mess + '\nBạn có muốn tiếp tục nhận cảnh báo?');
if (err) console.error(mess);
if (server) {
if (downloadDelay > 700) {
if (chapTitle) titleError.push(chapTitle);
saveEbook();
return;
}
downloadStatus('warning');
downloadDelay += 100;
setTimeout(function () {
getContent();
}, downloadDelay);
return;
}
if (!chapTitle) return;
titleError.push(chapTitle);
return '<p class="no-indent"><a href="' + referrer + chapId + '">' + mess + '</a></p>';
}
function genEbook() {
jepub
.generate('blob', function (metadata) {
$download.html('Đang nén <strong>' + metadata.percent.toFixed(2) + '%</strong>');
})
.then(function (epubZipContent) {
document.title = '[⇓] ' + ebookTitle;
$win.off('beforeunload');
$download
.attr({
href: window.URL.createObjectURL(epubZipContent),
download: ebookFilename,
})
.text('Hoàn thành')
.off('click');
if (status !== 'danger') downloadStatus('success');
saveAs(epubZipContent, ebookFilename);
})
.catch(function (err) {
downloadStatus('danger');
console.error(err);
});
}
function saveEbook() {
if (endDownload) return;
endDownload = true;
$download.html('Bắt đầu tạo EPUB');
if (titleError.length) {
titleError = '<p class="no-indent"><strong>Các chương lỗi: </strong>' + titleError.join(', ') + '</p>';
} else {
titleError = '';
}
beginEnd = '<p class="no-indent">Nội dung từ <strong>' + begin + '</strong> đến <strong>' + end + '</strong></p>';
jepub.notes(beginEnd + titleError + '<br /><br />' + credits);
GM.xmlHttpRequest({
method: 'GET',
url: ebookCover,
responseType: 'arraybuffer',
onload: function (response) {
try {
jepub.cover(response.response);
} catch (err) {
console.error(err);
}
genEbook();
},
onerror: function (err) {
console.error(err);
genEbook();
},
});
}
function getContent() {
if (endDownload) return;
chapId = chapList[count];
var lastChar = pathname.charAt(pathname.length - 1);
pathname = lastChar != '/' ? pathname + '/' : pathname;
$.get(pathname + chapId + '/')
.done(function (response) {
var $data = $(response),
$chapter = $data.find('.chapter-c'),
$notContent = $chapter.find('script, style, a'),
$referrer = $chapter.find('[style]').filter(function () {
return this.style.fontSize === '1px' || this.style.fontSize === '0px' || this.style.color === 'white';
}),
chapContent;
if (endDownload) return;
chapTitle = $data.find('.chapter-title').text().trim();
if (chapTitle === '') chapTitle = 'Chương ' + chapId.match(/\d+/)[0];
if (!$chapter.length) {
chapContent = downloadError('Không có nội dung');
} else {
var $img = $chapter.find('img');
if ($img.length) {
$img.replaceWith(function () {
return '<br /><a href="' + this.src + '">Click để xem ảnh</a><br />';
});
}
if ($notContent.length) $notContent.remove();
if ($referrer.length) $referrer.remove();
if ($chapter.text().trim() === '') {
chapContent = downloadError('Nội dung không có');
} else {
if (status !== 'danger') downloadStatus('warning');
chapContent = cleanHtml($chapter.html());
}
}
jepub.add(chapTitle, chapContent);
if (count === 0) begin = chapTitle;
end = chapTitle;
$download.html('Đang tải <strong>' + count + '/' + chapListSize + '</strong>');
count++;
document.title = '[' + count + '] ' + pageName;
if (count >= chapListSize) {
saveEbook();
} else {
setTimeout(function () {
getContent();
}, downloadDelay);
}
})
.fail(function (err) {
chapTitle = null;
downloadError('Kết nối không ổn định', err, true);
});
}
var pageName = document.title,
$win = $(window),
$download = $('<a>', {
class: 'btn btn-primary',
href: '#download',
text: 'Tải xuống',
}),
status,
downloadStatus = function (label) {
status = label;
$download.removeClass('btn-primary btn-success btn-info btn-warning btn-danger').addClass('btn-' + status);
},
$novelId = $('#truyen-id'),
chapList = [],
chapListSize = 0,
chapId = '',
chapTitle = '',
count = 0,
begin = '',
end = '',
endDownload = false,
ebookTitle = $('h1').text().trim(),
ebookAuthor = $('.info a[itemprop="author"]').text().trim(),
ebookCover = $('.books img').attr('src'),
ebookDesc = $('.desc-text').html(),
ebookType = [],
beginEnd = '',
titleError = [],
host = location.host,
pathname = location.pathname,
referrer = location.protocol + '//' + host + pathname,
ebookFilename = pathname.slice(1, -1) + '.epub',
credits =
'<p>Truyện được tải từ <a href="' +
referrer +
'">metruyenchu.net</a></p><p>Userscript được viết bởi: FixBug</p>',
jepub;
if (!$novelId.length) return;
var $ebookType = $('.info a[itemprop="genre"]');
if ($ebookType.length) {
$ebookType.each(function () {
ebookType.push($(this).text().trim());
});
}
jepub = new jEpub();
jepub
.init({
title: ebookTitle,
author: ebookAuthor,
publisher: host,
description: ebookDesc,
tags: ebookType,
})
.uuid(referrer);
$download.insertAfter('.info');
$download.wrap('<div class="panel-group books"></div>');
$download.one('click contextmenu', function (e) {
e.preventDefault();
document.title = '[...] Vui lòng chờ trong giây lát';
$.get('/api/services/chapter-option', {
type: 'chapter_option',
data: $novelId.val(),
})
.done(function (data) {
chapList = data.match(/(?:value=")[^"]+(?=")/g).map(function (val) {
return val.slice(7);
});
if (e.type === 'contextmenu') {
$download.off('click');
var startFrom = prompt('Nhập ID chương truyện bắt đầu tải:', chapList[0]);
startFrom = chapList.indexOf(startFrom);
if (startFrom !== -1) chapList = chapList.slice(startFrom);
} else {
$download.off('contextmenu');
}
chapListSize = chapList.length;
if (chapListSize > 0) {
$win.on('beforeunload', function () {
return 'Truyện đang được tải xuống...';
});
$download.one('click', function (e) {
e.preventDefault();
saveEbook();
});
getContent();
}
})
.fail(function (jqXHR, textStatus) {
downloadError(textStatus);
});
});
})(jQuery, window, document);