-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
18comic_edited.user.js
186 lines (170 loc) · 5.58 KB
/
18comic_edited.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
// ==UserScript==
// @name 18comic漫画下载edited
// @namespace http://github.com/eternalphane/Userscripts/
// @updateURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/18comic_edited.user.js
// @version 1.0.5.9
// @description 从18comic上下载cbz格式(整话阅读)或webp格式(分页阅读)的漫画
// @author eternalphane (edit by x94fujo6)
// @license MIT
// @match https://18comic.vip/photo/*
// @match https://18comic.org/photo/*
// @match https://18comic.bet/photo/*
// @match https://18comic1.one/photo/*
// @match https://18comic2.one/photo/*
// @connect cdn-msp.18comic.vip
// @connect cdn-msp.18comic.org
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @require https://unpkg.com/jszip@3.5.0/dist/jszip.min.js
// @resource css https://raw.githubusercontent.com/eternalphane/UserScripts/master/18comic%20Downloader/overlay.css
// @resource html https://raw.githubusercontent.com/eternalphane/UserScripts/master/18comic%20Downloader/overlay.html
// @grant GM_getResourceText
// @grant GM.getResourceText
// @grant GM_addStyle
// @grant GM.addStyle
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// ==/UserScript==
(async () => {
let btn = document.querySelector('.menu-bolock i.fa-download').parentElement;
if (location.search) {
btn.addEventListener('click', async e => {
e.preventDefault();
const page = document.querySelector('div[id*=".jpg"],div[id*=".webp"]');
save(
await download(page.querySelector('img').getAttribute('data-original')),
`${page.id.slice(0, -4)}.webp`
);
});
return;
}
GM.addStyle(await GM.getResourceText('css'));
let overlay = document.createElement('div');
overlay.id = 'dl-overlay';
overlay.innerHTML = await GM.getResourceText('html');
overlay.hidden = true;
document.body.appendChild(overlay);
let circle = overlay.querySelectorAll('circle')[1],
number = overlay.querySelector('span'),
msg = overlay.querySelector('h2'),
updateProgress = (percent, text) => {
circle.style.strokeDasharray = `${percent} 100`;
number.innerText = Math.round(percent);
if (text != undefined) msg.innerText = text;
};
btn.addEventListener('click', async e => {
e.preventDefault();
if (!overlay.hidden) {
return;
}
overlay.hidden = false;
document.body.classList.add('noscroll');
const pages = [...document.querySelectorAll('div[id*=".jpg"],div[id*=".webp"]')];
const total = pages.length;
let progress = 0;
updateProgress(0, 'Downloading...');
const zip = new JSZip();
await Promise.all(pages.map(async page => {
zip.file(
`${page.id.slice(0, -4)}.webp`,
await download(page.querySelector('img').getAttribute('data-original'))
);
updateProgress(++progress * 100 / total);
}));
updateProgress(0, 'Compressing...');
// TODO: Select output format? (cbz, cbt, pdf)
save(
await zip.generateAsync(
{
type: 'blob',
compression: 'DEFLATE',
compressionOptions: { level: 9 },
mimeType: 'application/vnd.comicbook+zip'
}, (meta) => updateProgress(meta.percent)
),
`${document.querySelector('.panel-heading .pull-left').textContent.trim()}.cbz`
);
document.body.classList.remove('noscroll');
overlay.hidden = true;
});
(() => {
let class_name = "scramble-page",
ele = document.querySelector(`.${class_name}`);
if (ele) {
[...ele.parentNode.children]
.forEach(e => {
if (!(e.classList.contains(class_name))) {
e.remove();
}
});
}
})();
function get_num(aid, img_index) {
let n = md5(`${aid}${img_index}`).slice(-1).charCodeAt(0);
if (aid < 268850) return 10;
if (aid >= 268850 && aid <= 421925) n %= 10;
if (aid >= 421926) n %= 8;
return ((n % 10) + 1) * 2;
}
function get_img(url) {
url = url.replace("-msp2", "-msp");
return new Promise((resolve, reject) => {
GM.xmlHttpRequest(
{
url,
method: 'GET',
responseType: 'blob',
onload: resolve,
onerror: reject
}
);
});
}
const download = async url => {
let img = new Image(),
img_id = url.match(/\d+\..+$/)[0],
img_index = img_id.split(".")[0],
img_data,
num = get_num(aid, img_index),
canvas = document.createElement('canvas'),
canvas_2d = canvas.getContext('2d'),
img_nwidth = 0,
img_nheight = 0,
sWidth = 0;
img_data = await get_img(url);
img_data = img_data.response;
img.src = URL.createObjectURL(img_data);
await new Promise((resolve, reject) => (img.onload = resolve, img.onerror = reject));
img_nwidth = img.naturalWidth;
img_nheight = img.naturalHeight;
sWidth = img_nwidth;
canvas.width = img_nwidth;
canvas.height = img_nheight;
// `aid`, `scramble_id` and `md5` are both global variables
if (url.includes('.gif') || aid < scramble_id) {
canvas_2d.drawImage(img, 0, 0);
} else {
let naturalHeight = parseInt(img_nheight % num);
for (i = 0; i < num; i++) {
let sHeight = Math.floor(img_nheight / num),
dy = sHeight * i,
sy = img_nheight - sHeight * (i + 1) - naturalHeight;
if (i == 0) {
sHeight += naturalHeight;
} else {
dy += naturalHeight;
}
canvas_2d.drawImage(img, 0, sy, sWidth, sHeight, 0, dy, sWidth, sHeight);
}
}
URL.revokeObjectURL(img.src);
// TODO: Select image type? Change quality?
return new Promise(resolve => canvas.toBlob(resolve, 'image/webp', 0.9));
};
const save = (blob, filename) => {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = filename;
a.click();
URL.revokeObjectURL(a.href);
};
})();