forked from xicelord/mangadex-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmangadex-bulk-cover-uploader.user.js
121 lines (105 loc) · 4.56 KB
/
mangadex-bulk-cover-uploader.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
// ==UserScript==
// @name Mangadex Bulk Cover Uploader
// @namespace https://github.com/xicelord
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Upload all the covers!
// @author icelord
// @homepage https://github.com/xicelord/mangadex-scripts
// @updateURL https://raw.githubusercontent.com/xicelord/mangadex-scripts/master/mangadex-bulk-cover-uploader.user.js
// @downloadURL https://raw.githubusercontent.com/xicelord/mangadex-scripts/master/mangadex-bulk-cover-uploader.user.js
// @match https://mangadex.org/title/*
// @icon https://mangadex.org/favicon.ico
// @grant none
// ==/UserScript==
(function() {
'use strict';
let total_files = 0
let finished_files = 0
let bcu_form = `
<div class="form-group row">
<div class="col-lg-9">
<span class="btn btn-secondary btn-file">
<span class="far fa-folder-open fa-fw " aria-hidden="true"></span> <span>Select files</span>
<input type="file" id="bcu-cover_files" accept=".jpg,.jpeg,.png,.gif" multiple />
</span>
</div>
</div>
<div class="form-group row">
<div id="bcu-preview" class="col-lg-9"></div>
</div>
<div class="form-group">
<div class="progress">
<div id="bcu-progress" class="progress-bar" role="progressbar" style="width: 0%"></div>
</div>
</div>
<div class="form-group row">
<div class="col-lg-9">
<button id="bcu-submit" class="btn btn-secondary disabled">
<span class="far fa-save fa-fw " aria-hidden="true"></span> <span>Upload</span>
</span>
</div>
</div>`
// Inject
document.getElementById('manga_cover_upload_form').parentNode.innerHTML = bcu_form
// Events
document.getElementById('bcu-cover_files').onchange = function() {
let files = document.querySelector('#bcu-cover_files').files
// Reset stats
total_files = 0
finished_files = 0
document.getElementById('bcu-progress').style.width = '0%'
document.getElementById('bcu-progress').classList.remove('bg-danger')
document.getElementById('bcu-submit').classList.remove('disabled')
let files_filtered = []
for (let i = 0; i < files.length; i++) {
const match = files[i].name.match(/(\d+)/)
if (match !== null) {
files_filtered.push(match[1] + ': ' + files[i].name)
total_files++
}
}
document.getElementById('bcu-preview').innerText = files_filtered.join('\n')
}
document.getElementById('bcu-submit').onclick = async function () {
// Disable logic
if (document.getElementById('bcu-submit').classList.contains('disabled')) {
return
} else {
document.getElementById('bcu-submit').classList.add('disabled')
}
let files = document.querySelector('#bcu-cover_files').files
for (let i = 0; i < files.length; i++) {
const match = files[i].name.match(/(\d+)/)
if (match !== null) {
await addCover(match[1], document.getElementById('bcu-cover_files').files[i])
}
}
}
async function addCover(volume, cover) {
return new Promise(function (resolve, reject) {
let formData = new FormData();
let req = new XMLHttpRequest();
formData.append('volume', volume)
formData.append('old_file', cover.name)
formData.append('file', cover)
req.addEventListener('load', function() {
finished_files++
console.log(`${total_files}/${finished_files}`)
document.getElementById('bcu-progress').style.width = ((finished_files / total_files) * 100) + '%'
if (this.responseText.length > 0) {
document.getElementById('bcu-progress').classList.add('bg-danger')
console.log('Error at: ' + cover.name)
console.error(this.responseText)
}
resolve()
})
req.addEventListener('error', function() {
reject()
})
req.open('POST', `https://mangadex.org/ajax/actions.ajax.php?function=manga_cover_upload&id=${document.location.href.match(/title\/(\d+)/)[1]}`)
req.setRequestHeader('x-requested-with', 'XMLHttpRequest')
req.send(formData);
})
}
})();