This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhex.user.js
299 lines (248 loc) · 8.4 KB
/
hex.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
296
297
298
299
// ==UserScript==
// @name hex
// @version 1.14.0
// @author imtbl
// @description Userscript for hex, a hydrus API plugin to download ExH archives
// @website https://github.com/imtbl/hex
// @updateURL https://github.com/imtbl/hex/raw/master/hex.user.js
// @downloadURL https://github.com/imtbl/hex/raw/master/hex.user.js
// @supportURL https://github.com/imtbl/hex/issues/new
// @match *://exhentai.org/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// ==/UserScript==
let hexBaseUrl = window.GM_getValue('hexBaseUrl', '')
let hexAccessKey = window.GM_getValue('hexAccessKey', '')
const fetchOrPrompt = (value, prompt, key, defaultValue = '') => {
if (!value) {
value = window.prompt(`${prompt} not set, please enter it:`, defaultValue)
window.GM_setValue(key, value)
}
return value
}
const promptAndChangeStoredValue = (value, prompt, key) => {
value = window.prompt(`Change ${prompt}:`, value)
window.GM_setValue(key, value)
}
const changeBaseUrl = () => {
promptAndChangeStoredValue(hexBaseUrl, 'hex base URL', 'hexBaseUrl')
}
const changeAccessKey = () => {
promptAndChangeStoredValue(hexAccessKey, 'hex access key', 'hexAccessKey')
}
window.GM_registerMenuCommand('Change hex base URL', changeBaseUrl)
window.GM_registerMenuCommand('Change hex access key', changeAccessKey)
hexBaseUrl = fetchOrPrompt(
hexBaseUrl, 'hex base URL', 'hexBaseUrl', 'http://localhost:8000'
)
hexAccessKey = fetchOrPrompt(hexAccessKey, 'hex access key', 'hexAccessKey')
const elementFromHtml = html => {
const template = document.createElement('template')
html = html.trim()
template.innerHTML = html
return template.content.firstChild
}
const getHexInfo = () => {
return window.fetch(`${hexBaseUrl}`)
}
const getHexSettings = () => {
return window.fetch(`${hexBaseUrl}/settings`, {
headers: {
Authorization: `Bearer ${hexAccessKey}`
}
})
}
const sendToHex = data => {
return window.fetch(`${hexBaseUrl}/import`, {
method: 'post',
headers: {
Authorization: `Bearer ${hexAccessKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(Object.fromEntries(data))
})
}
const getDelimitedString = arr => {
return arr.length
? arr.join('###')
: ''
}
const getDelimitedNamespaceReplacements = namespaceReplacements => {
const delimitedNamespaceReplacements = []
Object.entries(namespaceReplacements).forEach(([key, value]) => {
delimitedNamespaceReplacements.push(`${key}|||${value}`)
})
return delimitedNamespaceReplacements.join('###')
}
const start = async () => {
if (!document.getElementById('gd5')) {
return
}
let info, settings
try {
let infoResponse = await getHexInfo()
infoResponse = await infoResponse.json()
info = infoResponse.hex
} catch (err) {
console.error(err)
return
}
if (info.apiVersion !== 2) {
window.alert(
'This userscript supports API version 2, while your hex ' +
`installation is using version ${info.apiVersion}. Please adjust ` +
'your installation or change the userscript manually.'
)
return
}
try {
let settingsResponse = await getHexSettings()
settingsResponse = await settingsResponse.json()
settings = settingsResponse.settings
} catch (err) {
console.error(err)
return
}
const hexBar = elementFromHtml(
`<div class="hex-bar">
<style>
.hex-bar {
align-items: center;
background-color: #4f535b;
border-bottom: 1px solid #000;
box-sizing: border-box;
display: flex;
height: 50px;
left: 0;
padding: 0 20px;
position: fixed;
top: 0;
width: 100%;
z-index: 1000;
}
.hex-bar__title {
font-size: 1rem;
font-weight: 700;
margin-right: auto;
}
.hex-bar__form,
.hex-bar__form__settings {
align-items: center;
display: flex;
}
.hex-bar__form {
margin-left: 20px;
}
.hex-bar__form__settings label {
margin-left: 10px;
}
.hex-bar__form__settings label:first-child {
margin-left: 0;
}
.hex-bar__form__settings input[type="checkbox"] {
top: 0;
}
.hex-bar__form__settings input[type="text"] {
width: 150px;
}
.hex-bar__form__download {
margin-left: 20px;
}
.hex-bar__form__download button {
-moz-appearance: none;
-webkit-appearance: none;
background: #5fa9cf;
border: 1px solid #5fa9cf;
border-radius: 5px;
color: #f1f1f1;
cursor: pointer;
outline: 0;
padding: 8px;
}
.hex-bar__form__download button:hover {
background-color: #32789c;
border-color: #32789c;
}
</style>
<div class="hex-bar__title">hex</div>
<form id="hex-form" class="hex-bar__form" action="#">
<div class="hex-bar__form__settings">
<label>Skip import <input type="checkbox" name="skipImport"${settings.skipImport ? ' checked' : ''}></label>
<label>Skip known files <input type="checkbox" name="skipKnownFiles"${settings.skipKnownFiles ? ' checked' : ''}></label>
<label>Delete archives after import <input type="checkbox" name="deleteArchivesAfterImport"${settings.deleteArchivesAfterImport ? ' checked' : ''}></label>
<label>Add unique identifier tag <input type="checkbox" name="addUniqueIdentifierTag"${settings.addUniqueIdentifierTag ? ' checked' : ''}></label>
<label>Skip tags <input type="checkbox" name="skipTags"${settings.skipTags ? ' checked' : ''}></label>
<label>
Blacklisted namespaces<br>
<input type="text" name="blacklistedNamespaces" placeholder="Blacklisted namespaces" value="${getDelimitedString(settings.blacklistedNamespaces)}">
</label>
<label>
Namespace replacements<br>
<input type="text" name="namespaceReplacements" placeholder="Namespace replacements" value="${getDelimitedNamespaceReplacements(settings.namespaceReplacements)}">
</label>
<label>
Additional tags<br>
<input type="text" name="additionalTags" placeholder="Additional tags" value="${getDelimitedString(settings.additionalTags)}">
</label>
<label>
Unique identifier namespace<br>
<input type="text" name="uniqueIdentifierNamespace" placeholder="Unique identifier namespace" value="${settings.uniqueIdentifierNamespace}">
</label>
</div>
<div class="hex-bar__form__download">
<button id="hex-download-button" type="submit">Download</button>
</div>
</form>
</div>`
)
document.getElementById('nb').style.cssText = 'margin-top: 60px;'
document.body.insertAdjacentElement(
'afterbegin', hexBar
)
const hexForm = document.getElementById('hex-form')
const hexSubmitButton = document.getElementById('hex-download-button')
hexForm.addEventListener('submit', event => {
event.preventDefault()
const data = new window.FormData(hexForm)
data.set('skipImport', data.has('skipImport'))
data.set('skipKnownFiles', data.has('skipKnownFiles'))
data.set(
'deleteArchivesAfterImport',
data.has('deleteArchivesAfterImport')
)
data.set('addUniqueIdentifierTag', data.has('addUniqueIdentifierTag'))
data.set('skipTags', data.has('skipTags'))
data.set('cookies', document.cookie)
data.set('url', window.location.href)
sendToHex(data)
.then(response => Promise.all([response.ok, response.json()]))
.then(([responseOk, body]) => {
if (!responseOk) {
throw new Error(body.error)
}
hexSubmitButton.textContent = 'Started'
hexSubmitButton.style.cssText =
'background: #6a936d; border-color: #617c63;'
})
.catch(err => {
console.error('Error trying to send this gallery to hex:\n', err)
hexSubmitButton.textContent = 'Error'
hexSubmitButton.style.cssText =
'background: #9e2720; border-color: #9e2720;'
})
})
}
(() => {
if (
document.readyState === 'complete' ||
document.readyState === 'loaded' ||
document.readyState === 'interactive'
) {
start()
return
}
window.addEventListener('DOMContentLoaded', () => {
start()
})
})()