-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad57_sources.js
More file actions
310 lines (265 loc) · 13.3 KB
/
ad57_sources.js
File metadata and controls
310 lines (265 loc) · 13.3 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
// ==UserScript==
// @name AD57 SOURCES
// @namespace http://tampermonkey.net/
// @version 0.5
// @description Modifies the register source with link to full quality image.
// @author A2
// @match https://num.archives57.com/visualiseur/index.php/docnumViewer/calculHierarchieDocNum/*
// @grant none
// @require https://code.jquery.com/jquery-3.6.0.min.js
// ==/UserScript==
(function() {
'use strict';
// Robust Storage Manager
const StorageManager = {
setItem(key, data) {
try {
localStorage.setItem(key, JSON.stringify(data));
sessionStorage.setItem(key, JSON.stringify(data));
return true; // Indiquer le succès
} catch (error) {
console.error('Error storing data:', error);
return false;
}
},
getItem(key) {
try {
const localData = localStorage.getItem(key);
if (localData) return JSON.parse(localData);
const sessionData = sessionStorage.getItem(key);
if (sessionData) return JSON.parse(sessionData);
console.log(`No data found for key: ${key}`); // Log plus explicite
return null;
} catch (error) {
console.error('Error retrieving data:', error);
return null;
}
},
// Ajout d'une méthode pour vérifier si une clé existe
hasItem(key) {
return localStorage.getItem(key) !== null || sessionStorage.getItem(key) !== null;
}
};
// Check and warn if storage is empty
function checkStoragePersistence() {
try {
const ad57Keys = Object.keys(localStorage)
.filter(key => key.startsWith('AD57-'));
if (ad57Keys.length === 0) {
console.warn('No AD57 storage data found');
const warningDiv = document.createElement('div');
Object.assign(warningDiv.style, {
position: 'fixed',
top: '0',
left: '0',
width: '100%',
backgroundColor: 'red',
color: 'white',
padding: '10px',
zIndex: '9999',
textAlign: 'center'
});
warningDiv.innerText = 'Storage data has been lost. Please return to the search results page and try again.';
document.body.insertBefore(warningDiv, document.body.firstChild);
return false;
}
return true;
} catch (error) {
console.error('Error checking storage persistence:', error);
return false;
}
}
function getCurrentDocumentId() {
const currentUrl = window.location.href;
const match = currentUrl.match(/\/(\d+:\d+:\d+)\//);
return match ? match[1] : null;
}
let localStorageData = null;
let cheminsArray = [];
let totalPages = 0;
const documentId = getCurrentDocumentId();
if (documentId) {
const localStorageKey = `AD57-${documentId}`;
localStorageData = StorageManager.getItem(localStorageKey);
if (localStorageData) {
console.log('Successfully retrieved storage key', localStorageKey,'data', localStorageData);
} else {
console.error('NO DATA COULD BE RETRIEVED');
}
}
function getFullQualityImageUrl(pageNumber, withSize = false) {
if (!cheminsArray[pageNumber - 1]) return null;
const baseUrl = localStorageData.imageSrc.split('@@')[0];
const url = baseUrl + '@@' + cheminsArray[pageNumber - 1];
return withSize ? url + '/N/T0/100/100/Y' : url;
}
function createMultiPageSource(startPage, pageCount, noLinks = false) {
const pages = [];
const links = [];
for (let i = 0; i < pageCount; i++) {
const pageNum = startPage + i;
if (pageNum > totalPages) break;
const imageUrl = getFullQualityImageUrl(pageNum, true);
if (!imageUrl) continue;
pages.push(pageNum);
if (noLinks) {
links.push(pageNum.toString());
} else {
links.push(`<a href="${imageUrl}">${pageNum}</a>`);
}
}
if (pages.length === 0) return null;
return `${localStorageData.regSrc} i. ${links.join('-')}/${totalPages}`;
}
function copyToClipboard(text) {
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
} catch (err) {
console.error('Failed to copy text: ', err);
}
document.body.removeChild(textarea);
}
function handleClipboardContent(text) {
const number = parseInt(text);
if (!isNaN(number) && number > 0 && number <= totalPages) {
const input = document.querySelector('input[type="text"]');
if (input) {
input.value = number;
const inputEvent = new Event('input', { bubbles: true });
input.dispatchEvent(inputEvent);
}
}
}
function setupClipboardListener() {
document.addEventListener('paste', (e) => {
const text = e.clipboardData.getData('text');
handleClipboardContent(text);
});
document.addEventListener('visibilitychange', () => {
if (document.hidden && clipboardMonitorInterval) {
clearInterval(clipboardMonitorInterval);
clipboardMonitorInterval = null;
}
});
window.addEventListener('focus', () => {
if (!clipboardMonitorInterval) {
navigator.clipboard.readText()
.then(handleClipboardContent)
.catch(() => {});
}
});
}
function updateNavigationVisibility(currentPage) {
const showPlus1 = currentPage < totalPages;
const showPlus2 = currentPage < totalPages - 1;
// Éléments de visualisation
const viewPlus1Elements = document.querySelectorAll('#view-plus-1, #view-plus-1-separator');
const viewPlus2Elements = document.querySelectorAll('#view-plus-2, #view-plus-2-separator');
// Éléments de copie
const copyPlus1Elements = document.querySelectorAll('#copy-plus-1, #copy-plus-1-separator');
const copyPlus2Elements = document.querySelectorAll('#copy-plus-2, #copy-plus-2-separator');
viewPlus1Elements.forEach(el => el.style.display = showPlus1 ? 'inline' : 'none');
viewPlus2Elements.forEach(el => el.style.display = showPlus2 ? 'inline' : 'none');
copyPlus1Elements.forEach(el => el.style.display = showPlus1 ? 'inline' : 'none');
copyPlus2Elements.forEach(el => el.style.display = showPlus2 ? 'inline' : 'none');
}
function createInfoBar() {
// Ces constantes restent nécessaires pour le reste du script
const mainInitFunction = Array.from(document.querySelectorAll('script'))
.find(script => script.textContent.includes('main({'))
.textContent;
const docsArray = JSON.parse(mainInitFunction.match(/docs\s*:\s*(\[.*?\])/)[1]);
cheminsArray = docsArray.map(doc => doc.chemin);
totalPages = cheminsArray.length;
const infoBar = document.createElement('div');
// Les styles restent identiques
infoBar.style.position = 'fixed';
infoBar.style.top = '0';
infoBar.style.left = '0';
infoBar.style.width = '100%';
infoBar.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
infoBar.style.color = 'white';
infoBar.style.padding = '10px';
infoBar.style.zIndex = '9999';
infoBar.style.textAlign = 'center';
if (localStorageData) {
const initialImageUrl = getFullQualityImageUrl(1);
const fullQualityInitialUrl = initialImageUrl + '/N/T0/100/100/Y';
infoBar.innerHTML = `
${localStorageData.regSrc} i.<span id="page-number" style="user-select: text">1</span>/${totalPages}
<span id="navigation-container">
(<a id="view-current" href="${fullQualityInitialUrl}" target="_blank" style="color: white; text-decoration: underline;" title="Visualiser l'image en pleine qualité">voir image</a>
<span id="view-plus-1-separator"> / </span><a id="view-plus-1" href="${getFullQualityImageUrl(2) + '/N/T0/100/100/Y'}" target="_blank" style="color: white; text-decoration: underline;" title="Visualiser l'image suivante en pleine qualité">+1</a>
<span id="view-plus-2-separator"> / </span><a id="view-plus-2" href="${getFullQualityImageUrl(3) + '/N/T0/100/100/Y'}" target="_blank" style="color: white; text-decoration: underline;" title="Visualiser l'image deux positions après celle-ci en pleine qualité">+2</a> |
<a id="copy-current" href="#" style="color: white; text-decoration: underline;" class="copy-btn" title="Copier la source dans le presse papier">copier source</a>
<span id="copy-plus-1-separator"> / </span><a id="copy-plus-1" href="#" style="color: white; text-decoration: underline;" class="copy-plus-1-btn" title="Copier la source dans le presse papier en incluant l'image suivante">+1</a>
<span id="copy-plus-2-separator"> / </span><a id="copy-plus-2" href="#" style="color: white; text-decoration: underline;" class="copy-plus-2-btn" title="Copier la source dans le presse papier en incluant les deux images suivantes">+2</a>)
<input type="checkbox" id="no-links-checkbox" style="margin-left: 5px; vertical-align: middle;" title="Source sans hyperlien">
</span><br>
<div style="color: #ccc; font-size: 0.9em;">${localStorageData.descText}</div>
`;
// Nouveau code pour monitorer la pagination avec mise à jour de la visibilité
const checkJQPagination = setInterval(() => {
const $pagination = $('.pagination');
if ($pagination.length) {
try {
const currentPage = $pagination.jqPagination('option', 'current_page');
if (currentPage) {
const pageSpan = document.querySelector('#page-number');
const navContainer = document.querySelector('#navigation-container');
if (pageSpan && navContainer) {
pageSpan.textContent = currentPage;
// Mise à jour des liens
const viewCurrentLink = document.querySelector('#view-current');
const viewPlus1Link = document.querySelector('#view-plus-1');
const viewPlus2Link = document.querySelector('#view-plus-2');
if (viewCurrentLink) viewCurrentLink.href = getFullQualityImageUrl(currentPage) + '/N/T0/100/100/Y';
if (viewPlus1Link) viewPlus1Link.href = getFullQualityImageUrl(currentPage + 1) + '/N/T0/100/100/Y';
if (viewPlus2Link) viewPlus2Link.href = getFullQualityImageUrl(currentPage + 2) + '/N/T0/100/100/Y';
// Mise à jour de la visibilité des boutons
updateNavigationVisibility(currentPage);
}
}
} catch (e) {
console.error('Error updating page:', e);
}
}
}, 100);
// Gestionnaires d'événements modifiés pour gérer la case à cocher
const copyBtn = infoBar.querySelector('.copy-btn');
const copyPlus1Btn = infoBar.querySelector('.copy-plus-1-btn');
const copyPlus2Btn = infoBar.querySelector('.copy-plus-2-btn');
const noLinksCheckbox = infoBar.querySelector('#no-links-checkbox');
copyBtn?.addEventListener('click', (e) => {
e.preventDefault();
const currentPage = parseInt(document.querySelector('#page-number').textContent);
const source = createMultiPageSource(currentPage, 1, noLinksCheckbox.checked);
if (source) copyToClipboard(source);
});
copyPlus1Btn?.addEventListener('click', (e) => {
e.preventDefault();
const currentPage = parseInt(document.querySelector('#page-number').textContent);
const source = createMultiPageSource(currentPage, 2, noLinksCheckbox.checked);
if (source) copyToClipboard(source);
});
copyPlus2Btn?.addEventListener('click', (e) => {
e.preventDefault();
const currentPage = parseInt(document.querySelector('#page-number').textContent);
const source = createMultiPageSource(currentPage, 3, noLinksCheckbox.checked);
if (source) copyToClipboard(source);
});
// Initialisation de la visibilité des boutons
updateNavigationVisibility(1);
}
document.body.insertBefore(infoBar, document.body.firstChild);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', createInfoBar);
} else {
createInfoBar();
}
})();