-
Notifications
You must be signed in to change notification settings - Fork 50
/
script.js
339 lines (298 loc) · 11.1 KB
/
script.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
const TXT_PROCESSING = 'Processing...'
const TXT_DONE = 'Finished processing all files.'
const TXT_NO_ERROR = 'No errors detected. Perhaps there are other errors?<br>Output file is available for download anyway.'
const TXT_SYS_ERROR = 'The program encountered an internal error.'
const mainStatusDiv = document.getElementById('main_status')
const outputDiv = document.getElementById('output')
const btnDlAll = document.getElementById('btnDlAll')
const keepOriginalFilename = document.getElementById('keepOriginalFilename')
const filePicker = document.getElementById('file')
let filenames = [], fixedBlobs = [], dlfilenames = []
function build_output_html(idx, status) {
const statusDiv = document.createElement('div')
const dlBtn = document.createElement('button')
statusDiv.style.margin = '1em 0'
dlBtn.style.margin = '1em 0'
dlBtn.innerHTML = 'Download'
dlBtn.addEventListener('click', () => {
saveAs(fixedBlobs[idx], dlfilenames[idx])
})
let btn = false
if (status === TXT_NO_ERROR) {
statusDiv.innerHTML = status
statusDiv.style.color = 'blue'
btn = true
} else if (status === TXT_SYS_ERROR) {
statusDiv.innerHTML = status
statusDiv.style.color = 'red'
btn = false
} else {
statusDiv.innerHTML = `<ul class="scroll">${status.map(x => `<li>${x}</li>`).join('')}</ul>`
statusDiv.style.color = 'green'
btn = 'block'
}
const section = document.createElement('section')
section.style.margin = '2em 0'
section.innerHTML = `<h3>${filenames[idx]}</h3>`
section.appendChild(statusDiv)
if (btn) {
section.appendChild(dlBtn)
}
return section
}
function setMainStatus(type) {
if (type === '') {
mainStatusDiv.style.display = 'none'
mainStatusDiv.style.display = 'none'
} else {
mainStatusDiv.style.display = 'block'
if (type === TXT_PROCESSING) {
mainStatusDiv.innerHTML = type
mainStatusDiv.style.color = 'blue'
} else if (type === TXT_DONE) {
mainStatusDiv.innerHTML = type
mainStatusDiv.style.color = 'blue'
}
}
}
function basename(path) {
return path.split('/').pop()
}
function simplify_language(lang) {
return lang.split('-').shift().toLowerCase()
}
class EPUBBook {
fixedProblems = []
// Add UTF-8 encoding declaration if missing
fixEncoding() {
const encoding = '<?xml version="1.0" encoding="utf-8"?>'
const regex = /^<\?xml\s+version=["'][\d.]+["']\s+encoding=["'][a-zA-Z\d-.]+["'].*?\?>/i
for (const filename in this.files) {
const ext = filename.split('.').pop()
if (ext === 'html' || ext === 'xhtml') {
let html = this.files[filename]
html = html.trimStart()
if (!regex.test(html)) {
html = encoding + '\n' + html
this.fixedProblems.push(`Fixed encoding for file ${filename}`)
}
this.files[filename] = html
}
}
}
// Fix linking to body ID showing up as unresolved hyperlink
fixBodyIdLink() {
const bodyIDList = []
const parser = new DOMParser()
// Create list of ID tag of <body>
for (const filename in this.files) {
const ext = filename.split('.').pop()
if (ext === 'html' || ext === 'xhtml') {
let html = this.files[filename]
const dom = parser.parseFromString(html, 'text/html')
const bodyID = dom.getElementsByTagName('body')[0].id
if (bodyID.length > 0) {
const linkTarget = basename(filename) + '#' + bodyID
bodyIDList.push([linkTarget, basename(filename)])
}
}
}
// Replace all
for (const filename in this.files) {
for (const [src, target] of bodyIDList) {
if (this.files[filename].includes(src)) {
this.files[filename] = this.files[filename].replaceAll(src, target)
this.fixedProblems.push(`Replaced link target ${src} with ${target} in file ${filename}.`)
}
}
}
}
// Fix language field not defined or not available
fixBookLanguage() {
const parser = new DOMParser()
// From https://kdp.amazon.com/en_US/help/topic/G200673300
// Retrieved: 2022-Sep-13
const allowed_languages = [
// ISO 639-1
'af', 'gsw', 'ar', 'eu', 'nb', 'br', 'ca', 'zh', 'kw', 'co', 'da', 'nl', 'stq', 'en', 'fi', 'fr', 'fy', 'gl',
'de', 'gu', 'hi', 'is', 'ga', 'it', 'ja', 'lb', 'mr', 'ml', 'gv', 'frr', 'nb', 'nn', 'pl', 'pt', 'oc', 'rm',
'sco', 'gd', 'es', 'sv', 'ta', 'cy',
// ISO 639-2
'afr', 'ara', 'eus', 'baq', 'nob', 'bre', 'cat', 'zho', 'chi', 'cor', 'cos', 'dan', 'nld', 'dut', 'eng', 'fin',
'fra', 'fre', 'fry', 'glg', 'deu', 'ger', 'guj', 'hin', 'isl', 'ice', 'gle', 'ita', 'jpn', 'ltz', 'mar', 'mal',
'glv', 'nor', 'nno', 'por', 'oci', 'roh', 'gla', 'spa', 'swe', 'tam', 'cym', 'wel',
]
// Find OPF file
if (!('META-INF/container.xml' in this.files)) {
console.error('Cannot find META-INF/container.xml')
return
}
const meta_inf_str = this.files['META-INF/container.xml']
const meta_inf = parser.parseFromString(meta_inf_str, 'text/xml')
let opf_filename = ''
for (const rootfile of meta_inf.getElementsByTagName('rootfile')) {
if (rootfile.getAttribute('media-type') === 'application/oebps-package+xml') {
opf_filename = rootfile.getAttribute('full-path')
}
}
// Read OPF file
if (!(opf_filename in this.files)) {
console.error('Cannot find OPF file!')
return
}
const opf_str = this.files[opf_filename]
try {
const opf = parser.parseFromString(opf_str, 'text/xml')
const language_tags = opf.getElementsByTagName('dc:language')
let language = 'en'
let original_language = 'undefined'
if (language_tags.length === 0) {
language = prompt('E-book does not have language tag. Please specify the language of the book in RFC 5646 format, e.g. en, fr, ja.', 'en') || 'en'
} else {
language = language_tags[0].innerHTML
original_language = language
}
if (!allowed_languages.includes(simplify_language(language))) {
language = prompt(`Language ${language} is not supported by Kindle. Documents may fail to convert. Continue or specify new language of the book in RFC 5646 format, e.g. en, fr, ja.`, language) || language
}
if (language_tags.length === 0) {
const language_tag = opf.createElement('dc:language')
language_tag.innerHTML = language
opf.getElementsByTagName('metadata')[0].appendChild(language_tag)
} else {
language_tags[0].innerHTML = language
}
if (language !== original_language) {
this.files[opf_filename] = new XMLSerializer().serializeToString(opf)
this.fixedProblems.push(`Change document language from ${original_language} to ${language}.`)
}
} catch (e) {
console.error(e)
console.error('Error trying to parse OPF file as XML.')
}
}
fixStrayIMG() {
const parser = new DOMParser()
for (const filename in this.files) {
const ext = filename.split('.').pop()
if (ext === 'html' || ext === 'xhtml') {
let html = parser.parseFromString(this.files[filename], ext === 'xhtml' ? 'application/xhtml+xml' : 'text/html')
let strayImg = []
for (const img of html.getElementsByTagName('img')) {
if (!img.getAttribute('src')) {
strayImg.push(img)
}
}
if (strayImg.length > 0) {
for (const img of strayImg) {
img.parentElement.removeChild(img)
}
this.fixedProblems.push(`Remove stray image tag(s) in ${filename}`)
this.files[filename] = new XMLSerializer().serializeToString(html)
}
}
}
}
async readEPUB(blob) {
const reader = new zip.ZipReader(new zip.BlobReader(blob))
this.entries = await reader.getEntries()
this.files = {}
this.binary_files = {}
for (const entry of this.entries) {
const filename = entry.filename
const ext = filename.split('.').pop()
if (filename === 'mimetype' || ['html', 'xhtml', 'htm', 'xml', 'svg', 'css', 'opf', 'ncx'].includes(ext)) {
this.files[filename] = await entry.getData(new zip.TextWriter('utf-8'))
} else {
this.binary_files[filename] = await entry.getData(new zip.Uint8ArrayWriter())
}
}
}
async writeEPUB() {
const blobWriter = new zip.BlobWriter('application/epub+zip')
// EPUB Zip cannot have extra attributes, so no extended timestamp
const writer = new zip.ZipWriter(blobWriter, { extendedTimestamp: false })
// First write mimetype file
if ('mimetype' in this.files) {
await writer.add('mimetype', new zip.TextReader(this.files['mimetype']), { level: 0 })
}
// Add text file
for (const file in this.files) {
if (file === 'mimetype') {
// We have already added mimetype file
continue
}
await writer.add(file, new zip.TextReader(this.files[file]))
}
// Add binary file
for (const file in this.binary_files) {
await writer.add(file, new zip.Uint8ArrayReader(this.binary_files[file]))
}
// Finalize file
await writer.close()
return blobWriter.getData()
}
}
filePicker.addEventListener('change', async (e) => {
const selectedFile = e.target.files[0]
setMainStatus(TXT_PROCESSING)
outputDiv.innerHTML = ''
btnDlAll.style.display = 'none'
for (const file of e.target.files) {
await processEPUB(file, file.name)
}
setMainStatus(TXT_DONE)
if (e.target.files.length > 1) {
btnDlAll.style.display = 'block'
}
})
async function processEPUB (inputBlob, name) {
try {
// Load EPUB
const epub = new EPUBBook()
await epub.readEPUB(inputBlob)
// Run fixing procedure
epub.fixBodyIdLink()
epub.fixBookLanguage()
epub.fixStrayIMG()
epub.fixEncoding()
// Write EPUB
const blob = await epub.writeEPUB()
const idx = filenames.length
filenames.push(name)
fixedBlobs.push(blob)
if (epub.fixedProblems.length > 0) {
keepOriginalFilename.checked ? dlfilenames.push(name) : dlfilenames.push("(fixed) " + name)
outputDiv.appendChild(build_output_html(idx, epub.fixedProblems))
} else {
keepOriginalFilename.checked ? dlfilenames.push(name) : dlfilenames.push("(repacked) " + name)
outputDiv.appendChild(build_output_html(idx, TXT_NO_ERROR))
}
} catch (e) {
console.error(e)
const idx = filenames.length
filenames.push(name)
while (fixedBlobs.length !== filenames.length) {
fixedBlobs.push(null)
}
while (dlfilenames.length !== filenames.length) {
dlfilenames.push(null)
}
outputDiv.appendChild(build_output_html(idx, TXT_SYS_ERROR))
}
}
async function downloadAll() {
const old = mainStatusDiv.innerHTML
mainStatusDiv.innerHTML = 'Preparing download...'
const blobWriter = new zip.BlobWriter('application/zip')
const writer = new zip.ZipWriter(blobWriter, { extendedTimestamp: false })
for (let i = 0; i < fixedBlobs.length; i++) {
if (fixedBlobs[i])
await writer.add(dlfilenames[i], new zip.BlobReader(fixedBlobs[i]))
}
await writer.close()
const blob = blobWriter.getData()
saveAs(blob, 'fixed-epubs.zip')
mainStatusDiv.innerHTML = old
}
btnDlAll.addEventListener('click', downloadAll)