-
Notifications
You must be signed in to change notification settings - Fork 8
/
ReloadStructure.js
363 lines (339 loc) · 14.2 KB
/
ReloadStructure.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
function showImgFrame() {
if (document.getElementById('mainFrame').style.display != 'none') {
window.originHref = document.getElementById('mainFrame').contentWindow.document.location.href
}
document.getElementById('mainFrame').style.display = 'none'
document.getElementById('imgFrame').style.display = 'block'
document.getElementById('topBar').style.display = 'flex'
}
function showMainFrame() {
document.getElementById('imgFrame').contentWindow.document.body.innerHTML = ""
document.getElementById('mainFrame').style.display = 'block'
document.getElementById('imgFrame').style.display = 'none'
document.getElementById('topBar').style.display = 'none'
}
function inExhentai() {
var href = document.location.href
if (href == 'https://exhentai.org/' || href.indexOf('https://exhentai.org/?') > -1 || href.indexOf('https://exhentai.org/tag/') > -1) {
return true
} else {
return false
}
}
function hideElement(element) {
element.style.opacity -= 0.05
if (element.style.opacity > 0) {
requestAnimationFrame(function () {
hideElement(element)
})
}
}
function barComplete(i) {
hideElement(window.progressBars[i])
}
function imgLoadSuccess() {
window.loaded++
var imgFrame = document.getElementById('imgFrame').contentWindow
var order = this.getAttribute('order')
barComplete(order)
if (window.loaded >= window.pageUrls.length) {
hideElement(imgFrame.document.getElementById('barBox'))
}
}
function imgLoadFailed() {
this.setAttribute('src', this.src)
}
function loadImg(imgFrame, i, first) {
var xhr = new XMLHttpRequest()
xhr.open('GET', window.pageUrls[i], true)
xhr.onload = function () {
if (this.status === 200) {
var imgUrl = this.responseText.match(/<img id=\"img\" src=\"(.+?)\"/i)[1]
var nl = this.responseText.match(/onclick=\"return nl\(\'(.+?)\'\)\"/i)[1]
window.imgUrls[i] = imgUrl
window.nls[i] = nl
var img = imgFrame.document.getElementById('img' + i)
if (first) {
var h = document.createElement('hr')
h.setAttribute('style', 'width:100%;height:8px;background-color:yellow;margin:0px;')
img.parentElement.insertBefore(h, img)
}
img.setAttribute('src', imgUrl)
}
}
xhr.send()
}
function runReader(imgFrame) {
var container = imgFrame.document.getElementById('gdt')
if (!container) {
return
}
var pageElements = container.getElementsByTagName('a')
window.pageUrls = new Array()
window.imgUrls = new Array()
window.imgElements = new Array()
window.progressBars = new Array()
window.nls = new Array()
window.loaded = 0
var readerContainer = document.createElement('div')
readerContainer.setAttribute('id', 'readerContainer')
container.parentElement.insertBefore(readerContainer, container)
var barBox = document.createElement('div')
barBox.setAttribute('id', 'barBox')
barBox.setAttribute('style', 'opacity:1;background-color: #7bed9f;z-index: 99; position: fixed; border-radius: 5px; border: none; width: 100%; height: 10px; left: 0px; top: 0px; display: flex; flex-flow: row nowrap; justify-content: start; align-items: center;')
readerContainer.appendChild(barBox)
for (var i = 0; i < pageElements.length; i++) {
window.pageUrls.push(pageElements[i].href)
var img = document.createElement('img')
img.setAttribute('id', 'img' + i)
img.setAttribute('name', 'anchor' + i)
img.setAttribute('order', i)
img.setAttribute('style', 'width:100%')
img.onload = imgLoadSuccess
img.onerror = imgLoadFailed
readerContainer.append(img)
window.imgElements[i] = img
var bar = document.createElement('a')
bar.setAttribute('href', '#anchor' + i)
bar.setAttribute('style', 'opacity:1;z-index: 100;display: flex; flex-grow: 1; background-color: #ff6b81;height: 100%;')
barBox.appendChild(bar)
window.progressBars[i] = bar
}
container.parentElement.removeChild(container)
window.initReader = true
for (var i = 0; i < window.pageUrls.length; i++) {
loadImg(imgFrame, i, true)
}
}
function refresh() {
console.log('refreshImgFrame')
window.loaded = 0
for (var i = 0; i < window.pageUrls.length; i++) {
if (!window.imgElements[i].complete) {
window.progressBars[i].style.backgroundColor = '#ffa502'
if (!window.imgUrls[i]) {
loadImg(i, false)
} else {
if (window.pageUrls[i].indexOf('?nl') < 0) {
window.pageUrls[i] += '?nl=' + window.nls[i]
}
var imgFrame = document.getElementById('imgFrame').contentWindow
loadImg(imgFrame, i, false)
}
} else {
window.loaded++
}
}
if (window.loaded >= window.pageUrls.length) {
hideElement(document.getElementById('barBox'))
}
}
function back() {
showMainFrame()
}
function toTop(){
document.getElementById('imgFrame').contentWindow.document.documentElement.scrollTop = 0
document.getElementById('imgFrame').contentWindow.document.body.scrollTop = 0
}
function toBottom(){
document.getElementById('imgFrame').contentWindow.document.documentElement.scrollTop = document.getElementById('imgFrame').contentWindow.document.documentElement.scrollHeight
document.getElementById('imgFrame').contentWindow.document.body.scrollTop = document.getElementById('imgFrame').contentWindow.document.body.scrollHeight
}
function exFrameLoaded() {
window.myframe = document.getElementById('myframe').contentWindow
var href = window.myframe.document.location.href
console.log('onload')
window.history.pushState(null, null, href)
runReader()
}
window.onpopstate = function () {
console.log('back')
showMainFrame()
}
function aClick(event) {
console.log('aclick')
var e = window.event || event
e.preventDefault()
if (this.href.indexOf('https://exhentai.org/g/') >= 0) {
if (document.getElementById('imgFrame').style.display == 'block') {
$('#imgFrame').empty()
}
listenImgFrame()
$('#imgFrame').attr('src', this.href)
showImgFrame()
} else {
document.getElementById('mainFrame').contentWindow.document.body.innerHTML = ''
listenMainFrame()
$('#mainFrame').attr('src', this.href)
showMainFrame()
}
}
function showBar(){
console.log('showbar')
var bar = document.getElementById('topBar')
bar.style.top = '0px'
}
function hideBar(){
console.log('hidebar')
var bar = document.getElementById('topBar')
bar.style.top = '-200px'
}
function listenImgFrame(old) {
console.log('listenImgFrame')
var imgFrame = document.getElementById('imgFrame').contentWindow
var href = imgFrame.document.location.href
if (imgFrame.document.getElementById('gdt') && old != href) {
// var as = imgFrame.document.body.getElementsByTagName('a')
// for (var i = 0; i < as.length; i++) {
// as[i].onclick = aClick
// }
// window.history.pushState(null, null, document.location.href)
imgFrame.document.documentElement.scrollTop = 0
window.barTop = 0
imgFrame.document.body.onscroll = function(){
var y = imgFrame.document.documentElement.scrollTop
console.log(y)
if(y - window.barTop >20){
window.barTop = y
hideBar()
}
if(y-window.barTop < -150){
window.barTop = y
showBar()
}
}
imgFrame.document.body.touchstart = function(){
console.log('touchstart')
}
imgFrame.document.documentElement.touchstart = function(){
console.log('touchstart2')
}
imgFrame.document.touchstart = function(){
console.log('touchstart3')
}
$('#imgFrame').contents().find('td').each(function () {
$(this).removeAttr('onclick')
})
$('#imgFrame').contents().find('a').each(function () {
$(this).removeAttr('onclick')
$(this).click(function (event) {
var e = window.event || event
e.preventDefault()
e.stopPropagation()
console.log(this.href)
if (this.href.indexOf('https://exhentai.org/g/') >= 0) {
if (document.getElementById('imgFrame').style.display == 'block') {
document.getElementById('imgFrame').contentWindow.document.body.innerHTML = ''
}
listenImgFrame(href)
$('#imgFrame').attr('src', this.href)
showImgFrame()
} else {
document.getElementById('mainFrame').contentWindow.document.body.innerHTML = ''
listenMainFrame(href)
$('#mainFrame').attr('src', this.href)
showMainFrame()
}
})
})
runReader(imgFrame)
} else {
requestAnimationFrame(function () {
listenImgFrame(old)
})
}
}
function listenMainFrame(old) {
console.log('listenMainFrame')
var mainFrame = document.getElementById('mainFrame').contentWindow
var href = mainFrame.document.location.href
if (mainFrame.document.getElementsByClassName('gld')[0] && old != href) {
$('#mainFrame').contents().find('a').each(function () {
console.log($(this))
$(this).removeAttr('onclick')
if ($(this).attr('href').indexOf('https://exhentai.org/g/') >= 0) {
$(this).click(function (event) {
var e = window.event || event
e.preventDefault()
e.stopPropagation()
console.log(this.href)
if (document.getElementById('imgFrame').style.display != 'none') {
document.getElementById('imgFrame').contentWindow.document.body.innerHTML = ''
}
listenImgFrame('a')
$('#imgFrame').attr('src', this.href)
showImgFrame()
})
}
})
$('#mainFrame').contents().find('td').each(function () {
console.log($(this))
var a = $(this).find('a:eq(0)')
if (a.length > 0) {
if (a.attr('href').indexOf('?page=') >= 0 || $(this).find('a:eq(0)').text() == 1) {
$(this).removeAttr('onclick')
$(this).click(function (event) {
var e = window.event || event
e.preventDefault()
e.stopPropagation()
document.getElementById('mainFrame').contentWindow.document.body.innerHTML = ''
listenMainFrame(href)
$('#mainFrame').attr('src', a.attr('href'))
showMainFrame()
})
}
}
})
// } else {
// document.getElementById('mainFrame').contentWindow.document.body.innerHTML = ''
// listenMainFrame(href)
// $('#mainFrame').attr('src', this.href)
// showMainFrame()
// }
} else {
requestAnimationFrame(function () {
listenMainFrame(old)
})
}
}
var j = document.createElement('script')
j.setAttribute('src', 'https://cdn.staticfile.org/jquery/3.4.1/jquery.js')
j.setAttribute('id', 'jquery')
j.setAttribute('onload', 'loadReader()')
document.head.appendChild(j)
function loadReader() {
if (!inExhentai()) {
if (confirm('需要先打开exhentai主页再运行脚本,是否打开exhentai主页?')) {
document.location.href = "https://exhentai.org/"
}
} else {
document.body.innerHTML = ""
var mystyle1 = document.createElement('style')
mystyle1.innerText = 'html,body,iframe{height:100%;width:100%;}.ibox{background-color: rgba(50,50,50,0.8);display: flex; flex-grow: 1;font-size:30px;justify-content: center; align-items: center;height: 100%;}'
document.head.appendChild(mystyle1)
var mystyle2 = $('<link rel="stylesheet" type="text/css" href="https://at.alicdn.com/t/font_1345377_p8vtkk60btq.css">')
$('head').append(mystyle2)
var mainFrame = document.createElement('iframe')
mainFrame.setAttribute('id', 'mainFrame')
mainFrame.setAttribute('frameborder', '0')
mainFrame.setAttribute('src', document.location.href)
document.body.appendChild(mainFrame)
var imgFrame = document.createElement('iframe')
imgFrame.setAttribute('id', 'imgFrame')
imgFrame.setAttribute('frameborder', '0')
imgFrame.setAttribute('style', 'display:none;')
document.body.appendChild(imgFrame)
var topBar = $('<div id="topBar" style="display: none; flex-flow: row nowrap;z-index:20;width:100%;height:140px;position:fixed;top:0px;"></div>')
var refreshTool = $('<div onclick="refresh()" id="refreshTool" class="ibox"><i style="font-size:100px;" class="iconfont icon-refresh"></i></div>')
var topTool = $('<div onclick="toTop()" id="topTool" class="ibox"><i style="font-size:100px;" class="iconfont icon-top"></i></div>')
var bottomTool = $('<div onclick="toBottom()" id="bottomTool" class="ibox"><i style="font-size:100px;" class="iconfont icon-bottom"></i></div>')
var backTool = $('<div onclick="back()" id="backTool" class="ibox"><i style="font-size:100px;" class="iconfont icon-Back"></i></div>')
topBar.append(refreshTool)
topBar.append(topTool)
topBar.append(bottomTool)
topBar.append(backTool)
$('body').append(topBar)
listenMainFrame('a')
}
}
// javascript:var s = document.createElement('script'); s.setAttribute('step',20); s.setAttribute('src','https://manakanemu.oss-cn-beijing.aliyuncs.com/vscode/ExHentaiReader/reloadStructure.js?'+parseInt(Date.parse(new Date())/1000)); s.setAttribute('id','exReader'); document.body.appendChild(s);