-
Notifications
You must be signed in to change notification settings - Fork 5
/
GentleViewer.user.js
163 lines (141 loc) · 5.08 KB
/
GentleViewer.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
// ==UserScript==
// @name Gentle Viewer
// @namespace http://knowlet3389.blogspot.tw/
// @version 1.1
// @description Auto load hentai pic.
// @icon http://e-hentai.org/favicon.ico
// @author KNowlet
// @include /^http[s]?:\/\/e-hentai.org\/g\/.*$/
// @include /^http[s]?:\/\/exhentai.org\/g\/.*$/
// @grant none
// @downloadURL https://github.com/knowlet/Gentle-Viewer/raw/dev/GentleViewer.user.js
// ==/UserScript==
'use strict';
// Simple configuration object
const CONFIG = {
SCROLL_THRESHOLD: 0.8,
PAGE_DELAY: 2000,
LOADING_GIF: '//ehgt.org/g/roller.gif'
};
class GentleViewer {
constructor(pageNum = 0, imgNum = 0) {
this.pageNum = pageNum;
this.imgNum = imgNum;
this.imgList = [];
if (this.isValid()) {
this.initialize();
} else {
window.alert("Invalid configuration. Please report this issue on Github.");
window.open("https://github.com/knowlet/Gentle-Viewer/issues");
}
}
isValid() {
return (this.imgNum > 41 && this.pageNum < 2) || this.imgNum !== 0;
}
async initialize() {
try {
// Gather URLs before cleaning
const initialUrls = Array.from(
window.gdt.querySelectorAll('a[href]'),
link => link.href
);
// Clean and setup
this.cleanGallery();
await this.setupImages();
await this.loadUrls(initialUrls);
// Start next page loading if needed
if (this.pageNum) {
this.getNextPage(1);
}
} catch (error) {
console.error('Initialization failed:', error);
}
}
cleanGallery() {
while (window.gdt.firstChild) {
window.gdt.removeChild(window.gdt.firstChild);
}
// remove grid display class
window.gdt.className = "";
window.gdt.style = "display: block;";
}
async setupImages() {
for (let i = 0; i < this.imgNum; i++) {
const img = new Image();
img.src = CONFIG.LOADING_GIF;
this.imgList.push(img);
window.gdt.appendChild(img);
}
}
async loadUrls(urls) {
const fetchPromises = urls.map(url =>
fetch(url, { credentials: 'include' })
.then(res => res.text())
.then(html => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const imgNo = Number(html.match(/startpage=(\d+)/)?.[1] || 0);
const imgSrc = doc.getElementById('img')?.src;
if (imgNo && imgSrc && this.imgList[imgNo - 1]) {
this.imgList[imgNo - 1].src = imgSrc;
}
})
.catch(error => console.error(`Failed to load image from ${url}:`, error))
);
await Promise.all(fetchPromises);
}
async loadPage(pageNum) {
try {
const response = await fetch(`${location.href}?p=${pageNum}`, {
credentials: 'include'
});
const html = await response.text();
const doc = new DOMParser().parseFromString(html, 'text/html');
const gallery = doc.getElementById('gdt');
if (gallery) {
const urls = Array.from(
gallery.querySelectorAll('a[href]'),
link => link.href
);
await this.loadUrls(urls);
}
if (pageNum < this.pageNum) {
setTimeout(() => this.getNextPage(pageNum + 1), CONFIG.PAGE_DELAY);
}
} catch (error) {
console.error(`Failed to load page ${pageNum}:`, error);
}
}
getNextPage(pageNum) {
const checkScroll = () => {
const { scrollTop, scrollHeight, clientHeight } = document.documentElement;
return scrollTop >= (scrollHeight - clientHeight) * CONFIG.SCROLL_THRESHOLD;
};
if (checkScroll()) {
this.loadPage(pageNum);
return;
}
const scrollHandler = () => {
if (checkScroll()) {
window.removeEventListener('scroll', scrollHandler);
this.loadPage(pageNum);
}
};
window.addEventListener('scroll', scrollHandler);
}
}
// Initialize the viewer
try {
const pageNumElement = [...document.querySelectorAll('table.ptt td')].slice(-2)[0];
const imageCountElement = window.gdd.querySelector('#gdd tr:nth-child(n+6) td.gdt2');
if (pageNumElement && imageCountElement) {
const pageNum = Number(pageNumElement.textContent);
const imgNum = Number(imageCountElement.textContent.split(' ')[0]);
new GentleViewer(pageNum, imgNum);
} else {
throw new Error('Required elements not found');
}
} catch (error) {
console.error('Failed to initialize viewer:', error);
window.alert('Failed to initialize. Please check the console for details.');
}