forked from hoothin/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadAllContentSavaAsZIP.user.js
219 lines (211 loc) · 7.9 KB
/
DownloadAllContentSavaAsZIP.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
// ==UserScript==
// @name DownloadAllContent ZIP addon
// @name:zh-CN 怠惰小说下载器 ZIP 扩展
// @name:zh-TW 怠惰小説下載器 ZIP 擴充
// @namespace hoothin
// @version 0.6
// @description Save content as ZIP for DownloadAllContent
// @description:zh-CN 下载时分章节保存 TXT 并打包为 ZIP
// @description:zh-TW 下載時分章節儲存 TXT 並打包為 ZIP
// @author hoothin
// @match *://*/*
// @grant unsafeWindow
// @grant GM_xmlhttpRequest
// @require https://unpkg.com/[email protected]/dist/jszip.js
// ==/UserScript==
(function() {
'use strict';
const threadNum = 20;//圖片下載綫程數
const ocrApi = "";
//const ocrApi = "http://127.0.0.1:416/?img=%t";//ocr 識別 api
var _unsafeWindow = (typeof unsafeWindow == 'undefined') ? window : unsafeWindow;
var zipTips, loadingImgs = [], loadingIndex = 0, loadedNum = 0, ocrNum = 0, zip, blobDict = {}, ocrDict = {};
async function dataURLToBlob(dataurl, ext = "jpeg") {
return await new Promise((resolve) => {
if (!dataurl) return resolve(null);
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var img = new Image();
img.setAttribute("crossOrigin", "anonymous");
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(var i = 0; i < imageData.data.length; i += 4) {
if(imageData.data[i + 3] == 0) {
imageData.data[i] = 255;
imageData.data[i + 1] = 255;
imageData.data[i + 2] = 255;
imageData.data[i + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0);
canvas.toBlob(blob => {
resolve(blob);
}, `image/${ext}`);
};
img.onerror = function() {
resolve(null);
};
img.src = dataurl;
});
}
async function blobToDataURL(blob) {
return await new Promise((resolve) => {
var a = new FileReader();
a.readAsDataURL(blob);
a.onload = function (e) {
resolve(e.target.result);
};
a.onerror = function (e) {
resolve(null);
};
});
}
async function urlToBlob(url) {
return await new Promise((resolve) => {
GM_xmlhttpRequest({
method: 'GET',
url: url,
responseType:'blob',
timeout:20000,
headers: {
origin: location.origin,
referer: location.href,
accept: "*/*"
},
onload: async function(d) {
resolve(d.response);
},
onerror: function() {
resolve(null);
},
ontimeout: function() {
resolve(null);
}
});
})
}
async function downloadImage(cb) {
if (loadedNum >= loadingImgs.length || loadingIndex >= loadingImgs.length) return;
let i = loadingIndex;
loadingIndex++;
let src = loadingImgs[i];
let blob = await urlToBlob(src);
if (blob) {
zip.file("imgs/" + i + ".jpg", blob);
zipTips.innerText = `Download images ${loadedNum + "/" + loadingImgs.length}...`;
blobDict[src] = blob;
}
loadedNum++;
if (loadedNum >= loadingImgs.length) {
if (ocrApi) {
loadingIndex = 0;
let length = Math.min(loadingImgs.length, threadNum);
for (let i = 0; i < length; i++) {
recognizeImage(cb);
}
} else {
cb();
}
} else downloadImage(cb);
}
async function recognizeImage(cb) {
if (ocrNum >= loadingImgs.length || loadingIndex >= loadingImgs.length) return;
let i = loadingIndex;
loadingIndex++;
let src = loadingImgs[i];
let dataURL = await blobToDataURL(blobDict[src]);
let blob = await dataURLToBlob(dataURL, "jpeg");
dataURL = await blobToDataURL(blob);
let result = await new Promise((resolve) => {
GM_xmlhttpRequest({
method: 'GET',
url: ocrApi.replace("%t", encodeURIComponent(dataURL.replace("data:image/jpeg;base64,", ""))),
timeout:20000,
onload: function(d) {
resolve(d.response);
},
onerror: function() {
resolve(null);
},
ontimeout: function() {
resolve(null);
}
});
});
ocrNum++;
zipTips.innerText = `Recognize images ${ocrNum + "/" + loadingImgs.length}...`;
if (result) {
ocrDict[i] = result;
}
if (ocrNum >= loadingImgs.length) {
cb();
} else recognizeImage(cb);
}
function downloadImages(cb) {
let length = Math.min(loadingImgs.length, threadNum);
if (length == 0) {
return cb();
}
for (let i = 0; i < length; i++) {
downloadImage(cb);
}
}
const mdImgReg = /!\[img\]\((http.+?)\)/;
_unsafeWindow.downloadAllContentSaveAsZip = async (rCats, info, callback) => {
loadingIndex = 0;
loadedNum = 0;
ocrNum = 0;
if (!zipTips) {
zipTips = document.createElement("div");
}
if (!zipTips.parentNode) {
let txtDownWords = _unsafeWindow.txtDownWords;
if (txtDownWords) {
txtDownWords.appendChild(zipTips);
}
}
console.log("Begin compress to ZIP...");
zipTips.innerText = "Begin compress to ZIP...";
zip = new JSZip();
zip.file("readme.txt", info);
let zipTemp = [];
for (let i = 0; i < rCats.length; i++) {
let cat = rCats[i];
if (!cat) continue;
let catTitle = cat.match(/.*?\r\n/);
if (!catTitle) continue;
catTitle = catTitle[0].trim();
cat = cat.replace(catTitle, "").replace(/^[\n\r]+/, "");
let imgMatch = cat.match(mdImgReg), hasImg = false;
while (imgMatch) {
let index = loadingImgs.indexOf(imgMatch[1]);
if (index == -1) {
index = loadingImgs.length;
loadingImgs.push(imgMatch[1]);
}
cat = cat.replace(imgMatch[0], `![img](imgs/${index}.jpg)`);
imgMatch = cat.match(mdImgReg);
hasImg = true;
}
zipTemp.push({title: (i + 1) + " - " + catTitle.replace(/[\*\/:<>\?\\\|\r\n]/g, "_").slice(0, 50), hasImg: hasImg, content: cat});
}
downloadImages(() => {
zipTemp.forEach(d => {
if (ocrApi) {
d.hasImg = false;
Object.keys(ocrDict).forEach(key => {
d.content = d.content.replace(new RegExp(`!\\[img\\]\\(imgs/${key}\\.jpg\\)`, 'g'), ocrDict[key]);
});
}
zip.file(d.title + (d.hasImg ? ".md" : ".txt"), d.content);
});
zip.generateAsync({type: "blob", compression: "DEFLATE"}, meta => {zipTips.innerText = "percent: " + ((meta && meta.percent && meta.percent.toFixed(2)) || "100") + "%"}).then(function(content){
callback(content);
zipTips.innerText = "";
})
});
}
})();