-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreader-shell.js
More file actions
335 lines (306 loc) · 19.2 KB
/
Copy pathreader-shell.js
File metadata and controls
335 lines (306 loc) · 19.2 KB
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
/**
* Lightweight reader shell. It renders the first frame before the heavy reader
* controller is injected, then hands the existing DOM to content.js.
*/
(function() {
'use strict';
if (window.MGR_READER_SHELL) return;
let mountPromise = null;
function translate(key, params) {
const i18n = window.MGR_I18N;
let text = i18n && typeof i18n.t === 'function' ? i18n.t(key, params) : key;
if (params && (!i18n || typeof i18n.t !== 'function')) {
for (const [name, value] of Object.entries(params)) {
text = String(text).replace(new RegExp(`\\{${name}\\}`, 'g'), String(value));
}
}
return String(text || key);
}
function escapeHtml(value) {
return String(value == null ? '' : value)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function text(key, params) {
return escapeHtml(translate(key, params));
}
function stopNativeReader() {
try {
document.querySelectorAll('script[src*="ehg_mpv"], link[href*="ehg_mpv"]').forEach((node) => node.remove());
document.querySelectorAll('script:not([src])').forEach((node) => {
const source = node.textContent || '';
if (/mpvkey|preload_scroll_images|load_image/.test(source)) node.remove();
});
window.stop();
} catch (error) {
console.warn('[Gallery Reader] Failed to stop the native reader:', error);
}
try {
window.preload_generic = function() {};
window.preload_scroll_images = function() {};
window.load_image = function() {};
} catch {}
}
function ensureCriticalStyle() {
if (document.getElementById('eh-reader-critical-style')) return;
const style = document.createElement('style');
style.id = 'eh-reader-critical-style';
style.textContent = [
'html, body.eh-modern-reader { margin: 0; width: 100%; height: 100%; overflow: hidden; background: #111317; }',
'#eh-reader-container { position: fixed; inset: 0; z-index: 2147483000; display: grid; grid-template-rows: 56px minmax(0, 1fr) auto; background: #111317; color: #f4f5f7; font-family: Arial, sans-serif; }',
'#eh-header { display: flex; align-items: center; justify-content: space-between; min-width: 0; padding: 0 12px; background: #1b1e23; }',
'#eh-header > div { display: flex; align-items: center; min-width: 0; }',
'#eh-title { margin: 0 0 0 8px; overflow: hidden; font-size: 16px; text-overflow: ellipsis; white-space: nowrap; }',
'#eh-main, #eh-viewer, #eh-page-slider { min-width: 0; min-height: 0; width: 100%; height: 100%; }',
'.eh-page-track { display: flex; width: 300%; height: 100%; transform: translateX(-33.333%); }',
'.eh-page-slide { position: relative; display: flex; flex: 0 0 33.333%; width: 33.333%; height: 100%; align-items: center; justify-content: center; overflow: hidden; }',
'.eh-slide-current { position: relative; }',
'.eh-image-loading-overlay { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; }',
'.eh-icon-btn, .eh-nav-btn { color: inherit; border: 0; background: transparent; }',
'#eh-bottom-menu { min-height: 76px; background: #1b1e23; }',
'#eh-settings-panel.eh-hidden { opacity: 0; visibility: hidden; pointer-events: none; }'
].join('\n');
(document.head || document.documentElement).appendChild(style);
}
function ensureReaderStyles() {
const existing = document.getElementById('eh-reader-styles');
if (existing && existing.dataset.loaded === 'true') return Promise.resolve();
return new Promise((resolve) => {
const link = existing || document.createElement('link');
let settled = false;
const finish = () => {
if (settled) return;
settled = true;
link.dataset.loaded = 'true';
resolve();
};
link.addEventListener('load', finish, { once: true });
link.addEventListener('error', finish, { once: true });
if (!existing) {
link.id = 'eh-reader-styles';
link.rel = 'stylesheet';
link.href = chrome.runtime.getURL('style/reader.css');
(document.head || document.documentElement).appendChild(link);
}
setTimeout(finish, 1500);
});
}
function buildReaderHtml(pageData) {
const pageCount = Math.max(1, Number.parseInt(pageData && pageData.pagecount, 10) || 1);
const title = escapeHtml((pageData && pageData.title) || translate('loadingTitle'));
return `
<div id="eh-reader-container">
<header id="eh-header">
<div class="eh-header-left">
<button id="eh-close-btn" class="eh-icon-btn" title="${text('backToGallery')}">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M19 12H5M12 19l-7-7 7-7"/>
</svg>
</button>
<h1 id="eh-title">${title}</h1>
</div>
<div class="eh-header-center">
<span id="eh-page-info" title="${text('shortcutsHint')}">1 / ${pageCount}</span>
</div>
<div class="eh-header-right">
<button id="eh-reverse-btn" class="eh-icon-btn" title="${text('reverseReading')}">
<svg id="eh-reverse-icon" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="m16 3 4 4-4 4"/><path d="M4 7h16"/>
<path d="m8 13-4 4 4 4"/><path d="M20 17H4"/>
</svg>
</button>
<button id="eh-auto-btn" class="eh-icon-btn" title="${text('autoPage')}">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 3"/>
</svg>
</button>
<button id="eh-fullscreen-btn" class="eh-icon-btn" title="${text('fullscreen')}">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"/>
</svg>
</button>
<button id="eh-theme-btn" class="eh-icon-btn" title="${text('toggleTheme')}">
<svg id="eh-theme-icon" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
</svg>
</button>
<button id="eh-settings-btn" class="eh-icon-btn" type="button" title="${text('readerSettings')}" aria-label="${text('readerSettings')}" aria-controls="eh-settings-panel" aria-expanded="false">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="3"></circle>
<path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l0 0a2 2 0 1 1-2.83 2.83l0 0a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09a1.65 1.65 0 0 0-1-1.51 1.65 1.65 0 0 0-1.82.33l0 0a2 2 0 1 1-2.83-2.83l0 0a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09c.67 0 1.28-.39 1.51-1 .23-.6.1-1.26-.33-1.82l0 0a2 2 0 1 1 2.83-2.83l0 0c.56.43 1.22.56 1.82.33.61-.23 1-.84 1-1.51V3a2 2 0 0 1 4 0v.09c0 .67.39 1.28 1 1.51.6.23 1.26.1 1.82-.33l0 0a2 2 0 1 1 2.83 2.83l0 0c-.43.56-.56 1.22-.33 1.82.23.61.84 1 1.51 1H21a2 2 0 0 1 0 4h-.09c-.67 0-1.28.39-1.51 1Z"></path>
</svg>
</button>
</div>
</header>
<main id="eh-main">
<section id="eh-viewer">
<div id="eh-page-slider" class="eh-page-slider">
<div id="eh-page-track" class="eh-page-track">
<div class="eh-page-slide eh-slide-prev" data-slide="prev">
<img class="eh-slide-image" alt="${text('previousPage')}" />
</div>
<div class="eh-page-slide eh-slide-current" data-slide="current">
<div id="eh-image-loading-overlay" class="eh-image-loading-overlay" style="display: flex;">
<div class="eh-circular-progress">
<svg width="80" height="80" viewBox="0 0 80 80">
<circle cx="40" cy="40" r="32" fill="none" stroke="rgba(255,255,255,0.15)" stroke-width="8"/>
<circle id="eh-progress-ring" cx="40" cy="40" r="32" fill="none" stroke="#FF6B9D" stroke-width="8" stroke-linecap="round" stroke-dasharray="201.06 201.06" stroke-dashoffset="201.06" transform="rotate(-90 40 40)" style="transition: stroke-dashoffset 0.15s ease"/>
</svg>
<div id="eh-progress-text" class="eh-progress-text">0%</div>
</div>
<div class="eh-loading-hint">${text('loading')}</div>
<div id="eh-loading-page-number" class="eh-loading-page-number">${text('page', { page: 1 })}</div>
</div>
<img id="eh-current-image" class="eh-slide-image" alt="${text('currentPage')}" />
</div>
<div class="eh-page-slide eh-slide-next" data-slide="next">
<img class="eh-slide-image" alt="${text('nextPage')}" />
</div>
</div>
</div>
<button id="eh-prev-btn" class="eh-nav-btn eh-nav-prev" title="${text('previousPageTitle')}">
<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M15 18l-6-6 6-6"/></svg>
</button>
<button id="eh-next-btn" class="eh-nav-btn eh-nav-next" title="${text('nextPageTitle')}">
<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 18l6-6-6-6"/></svg>
</button>
</section>
</main>
<footer id="eh-bottom-menu" class="eh-bottom-menu">
<div id="eh-thumbnails-container" class="eh-thumbnails-container">
<div id="eh-thumbnails" class="eh-thumbnails-horizontal"></div>
</div>
<div class="eh-slider-container">
<span id="eh-progress-current" class="eh-progress-number">1</span>
<div class="eh-slider-track" id="eh-slider-track">
<div class="eh-slider-fill" id="eh-slider-fill"></div>
<input type="range" id="eh-progress-bar" min="1" max="${pageCount}" value="1" class="eh-progress-slider" />
</div>
<span id="eh-progress-total" class="eh-progress-number">${pageCount}</span>
</div>
</footer>
<div id="eh-settings-panel" class="eh-panel eh-hidden" aria-hidden="true">
<div class="eh-panel-content" role="dialog" aria-modal="true" aria-labelledby="eh-settings-title" tabindex="-1">
<div class="eh-panel-header">
<h3 id="eh-settings-title">${text('readerSettings')}</h3>
<button id="eh-settings-close" class="eh-panel-close" type="button" title="${text('closeSettings')}" aria-label="${text('closeSettings')}">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 6L6 18M6 6l12 12"/></svg>
</button>
</div>
<div class="eh-settings-body">
<section class="eh-setting-group" aria-labelledby="eh-layout-heading">
<h4 id="eh-layout-heading" class="eh-setting-label-group">${text('layoutMode')}</h4>
<div class="eh-radio-group" role="radiogroup" aria-labelledby="eh-layout-heading">
<label class="eh-radio-label">
<input type="radio" name="eh-read-mode-radio" value="single" checked>
<svg class="eh-mode-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" aria-hidden="true"><rect x="3" y="6" width="18" height="12" rx="1.5"/></svg>
<span>${text('singleHorizontal')}</span>
</label>
<label class="eh-radio-label">
<input type="radio" name="eh-read-mode-radio" value="single-vertical">
<svg class="eh-mode-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" aria-hidden="true"><rect x="7" y="3" width="10" height="18" rx="1.5"/></svg>
<span>${text('singleVertical')}</span>
</label>
<label class="eh-radio-label">
<input type="radio" name="eh-read-mode-radio" value="continuous-horizontal">
<svg class="eh-mode-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" aria-hidden="true"><rect x="2" y="5" width="8" height="14" rx="1.5"/><rect x="14" y="5" width="8" height="14" rx="1.5"/></svg>
<span>${text('continuousHorizontal')}</span>
</label>
<label class="eh-radio-label">
<input type="radio" name="eh-read-mode-radio" value="continuous-vertical">
<svg class="eh-mode-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" aria-hidden="true"><rect x="5" y="2" width="14" height="8" rx="1.5"/><rect x="5" y="14" width="14" height="8" rx="1.5"/></svg>
<span>${text('continuousVertical')}</span>
</label>
</div>
</section>
<section id="eh-continuous-settings" class="eh-setting-group" aria-labelledby="eh-continuous-heading">
<h4 id="eh-continuous-heading" class="eh-setting-label-group">${text('continuousSettings')}</h4>
<div class="eh-setting-item">
<div class="eh-setting-heading"><label for="eh-vertical-padding">${text('verticalPadding')}</label><output for="eh-vertical-padding" class="eh-slider-value"><span id="eh-vertical-padding-value">0</span><span class="eh-setting-unit"> px</span></output></div>
<div class="eh-slider-wrapper"><input type="range" id="eh-vertical-padding" min="0" max="1000" step="4" value="0" class="eh-slider"></div>
</div>
<div class="eh-setting-item">
<div class="eh-setting-heading"><label for="eh-horizontal-gap">${text('horizontalGap')}</label><output for="eh-horizontal-gap" class="eh-slider-value"><span id="eh-horizontal-gap-value">0</span><span class="eh-setting-unit"> px</span></output></div>
<div class="eh-slider-wrapper"><input type="range" id="eh-horizontal-gap" min="0" max="100" step="2" value="0" class="eh-slider"></div>
</div>
<div class="eh-setting-item">
<div class="eh-setting-heading"><label for="eh-vertical-gap">${text('verticalGap')}</label><output for="eh-vertical-gap" class="eh-slider-value"><span id="eh-vertical-gap-value">0</span><span class="eh-setting-unit"> px</span></output></div>
<div class="eh-slider-wrapper"><input type="range" id="eh-vertical-gap" min="0" max="100" step="2" value="0" class="eh-slider"></div>
</div>
</section>
<section class="eh-setting-group" aria-labelledby="eh-loading-heading">
<h4 id="eh-loading-heading" class="eh-setting-label-group">${text('performance')}</h4>
<div class="eh-setting-item">
<div class="eh-setting-heading"><label for="eh-preload-count">${text('preloadCount')}</label><output for="eh-preload-count" class="eh-slider-value"><span id="eh-preload-count-value">2</span><span class="eh-setting-unit"> ${text('pagesUnit')}</span></output></div>
<div class="eh-slider-wrapper"><input type="range" id="eh-preload-count" min="0" max="10" step="1" value="2" class="eh-slider"></div>
</div>
</section>
<section class="eh-setting-group" aria-labelledby="eh-automation-heading">
<h4 id="eh-automation-heading" class="eh-setting-label-group">${text('automation')}</h4>
<div class="eh-setting-item">
<div class="eh-setting-heading"><label for="eh-auto-interval">${text('autoInterval')}</label><output for="eh-auto-interval" class="eh-slider-value"><span id="eh-auto-interval-value">3.0</span><span class="eh-setting-unit"> ${text('secondsUnit')}</span></output></div>
<div class="eh-slider-wrapper"><input type="range" id="eh-auto-interval" min="1" max="60" step="0.5" value="3" class="eh-slider"></div>
</div>
<div class="eh-setting-item">
<div class="eh-setting-heading"><label for="eh-scroll-speed">${text('scrollSpeed')}</label><output for="eh-scroll-speed" class="eh-slider-value"><span id="eh-scroll-speed-value">0.5</span><span class="eh-setting-unit"> ${text('pxPerFrameUnit')}</span></output></div>
<div class="eh-slider-wrapper"><input type="range" id="eh-scroll-speed" min="0.1" max="5" step="0.1" value="0.5" class="eh-slider"></div>
</div>
</section>
</div>
<div class="eh-panel-footer">
<button id="eh-reset-settings" class="eh-reset-btn" type="button">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/><path d="M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16"/><path d="M3 21v-5h5"/></svg>
${text('resetSettings')}
</button>
</div>
</div>
</div>
</div>
`;
}
function mount(pageData) {
if (mountPromise) return mountPromise;
mountPromise = new Promise((resolve, reject) => {
const render = () => {
if (!document.body) {
mountPromise = null;
reject(new Error('Reader shell requires document.body'));
return;
}
const existing = document.getElementById('eh-reader-container');
if (existing) {
ensureReaderStyles().then(() => resolve(existing));
return;
}
stopNativeReader();
ensureCriticalStyle();
const stylesReady = ensureReaderStyles();
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
const initialTheme = window.MGR_THEME && typeof window.MGR_THEME.get === 'function'
? window.MGR_THEME.get()
: 'dark';
document.body.className = initialTheme === 'light'
? 'eh-modern-reader'
: 'eh-modern-reader eh-dark-mode';
document.body.insertAdjacentHTML('beforeend', buildReaderHtml(pageData || {}));
try {
performance.mark('mgr:shell-mounted');
performance.measure('mgr:start-to-shell', 'mgr:reader-start', 'mgr:shell-mounted');
} catch {}
stylesReady.then(() => resolve(document.getElementById('eh-reader-container')));
};
if (typeof requestAnimationFrame === 'function') {
requestAnimationFrame(render);
} else {
setTimeout(render, 0);
}
});
return mountPromise;
}
window.MGR_READER_SHELL = Object.freeze({ mount });
})();