-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathad57_stack.js
306 lines (270 loc) · 12 KB
/
ad57_stack.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
// ==UserScript==
// @name AD57 STACK
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Modifies the page layout
// @author A2
// @match https://num.archives57.com/visualiseur/index.php/rechercheTheme/requeteConstructor/1/*
// @grant none
// @require https://code.jquery.com/jquery-3.6.0.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery.blockUI/2.70/jquery.blockUI.min.js
// ==/UserScript==
(function() {
'use strict';
// Enhanced localStorage logging function
function logLocalStorageEntries(prefix = 'AD57-') {
console.log('--- LocalStorage Entries ---');
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
if (key.startsWith(prefix)) {
try {
const value = localStorage.getItem(key);
console.log(`Key: ${key}`);
console.log('Value:', JSON.parse(value));
} catch (error) {
console.error(`Error parsing localStorage item for key ${key}:`, error);
}
}
}
console.log('--- End of LocalStorage Entries ---');
}
function clearLocalStorageWithPrefix(prefix) {
console.log(`Clearing localStorage with prefix: ${prefix}`);
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
if (key.startsWith(prefix)) {
console.log(`Removing key: ${key}`);
localStorage.removeItem(key);
}
}
console.log('LocalStorage clearing complete');
}
//console.log('All localStorage cleared');
clearLocalStorageWithPrefix('AD57-');
if (window.location.href.includes('/1/1/A/')) {
window.location.href = 'https://num.archives57.com/visualiseur/index.php/rechercheTheme/requeteConstructor/1/2/A/0/0';
return;
}
if (window.location.href.includes('/1/1/R/')) {
$('#menu').remove();
$('#content').remove();
}
$('html').css({'border': 'none'});
$('ul.dvpt-liste_items li a[onclick^="winopen"]').each(function() {
$(this).removeAttr('onclick').removeAttr('href').html('•');
});
$('body').css({ 'font-family': 'Noto Sans', 'font-size': 'x-large', 'line-height': '1.5'
});
// Remove "Choix de la commune" useless text
$('ul.dvpt-liste_resultat li:contains("Choix de la commune")').each(function() {
const html = $(this).html();
const newHtml = html.replace(/Choix de la commune\s*<br>/, '');
$(this).html(newHtml);
});
let totalResults = null;
function getTotalResults() {
const match = $('.t.bt td:last-child').text().match(/(\d+)/);
return match ? parseInt(match[1]) : 0;
}
// Function to apply all cleanup and formatting
function applyCleanupAndFormatting() {
// Remove the original #resultat div
document.getElementById('resultat').style.display = 'none';
$('.bouton_84px_type1, .bouton_176px_type1, .bouton_298px_type1').css({
'width': 'auto',
'height': 'auto',
'padding': 'revert'
});
$('#content').removeClass('main_contener');
// Process commune name and description
var commDesc = '';
var commune = $('.dvpt-liste_resultat li').text().trim().replace(/:.*/, '').toLowerCase();
var text = $('.dvpt-liste_resultat li').text().trim();
if (text.includes(':')) {
commDesc = text.split(':')[1].trim();
}
var words = commune.split('-');
for (var i = 0; i < words.length; i++) {
switch (words[i].trim()) {
case 'etangs (les)':
words[i] = 'Les Étangs';
break;
case 'l\'hopital':
words[i] = 'L\'Hôpital';
break;
case 'l orme':
words[i] = 'l’Orme';
break;
case 'les':
words[i] = 'lès';
break;
case 'a':
words[i] = 'à';
break;
case 'la':
case 'le':
case 'aux':
case 'sur':
break;
default:
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
}
commune = words.join('-').trim();
// Add header with commune name and result count
$('body').prepend('<h1>' + commune + '</h1><div>' + totalResults + ' réponses' + '</div>')
.css({ 'font-family': 'Noto Sans', 'font-size': 'x-large'});
$('#resultats_complets, #resultats_complets a').css({
'font-family': 'Noto Sans',
'font-size': 'x-large',
'line-height': '1.5'
});
// Fix width of date spans
$('span.reponsemot > span:last-child').css('width', '160px');
// Image handling
$('#resultats_complets img[id^="img_"]').each(function() {
$(this).css({
'width': '120px',
'height': 'auto',
'position': 'relative',
'z-index': '1',
'transition': ''
});
});
// Remove extra whitespace
$('.reponsemot').each(function() {
let html = $(this).html();
html = html.replace(/ {2,}/g, ' ');
$(this).html(html);
});
const elementsToRemove = ['#menu', 'div:has(> span.titre_ecran)', 'h3', '.espace_bandeau', '.message', '#misePanier', 'form', '#content > *:lt(3)', 'img[src*="basket-sm.png"]', '#requete'];
elementsToRemove.forEach(selector => {
$(selector).remove();
});
$('#resultats_complets div:last').nextAll('br').addBack().remove();
// Process all openDetail links
$('a[id^="openDetail"]').each(function() {
let imageSrc = '';
let uniqueId = '';
const onclickStr = $(this).attr('onclick');
const baseUrl = onclickStr.match(/window\.open\('([^']+)'\+screen\.height\+'\/'?\+screen\.width/)[1];
const completeUrl = `${baseUrl}${screen.height}/${screen.width}`;
uniqueId = baseUrl.match(/\/(\d+:\d+:\d+)\/$/)[1];
const imageContainer = $(this).closest('div').prev('a').find('div img');
imageSrc = imageContainer.attr('src');
$(this).removeAttr('onclick')
.attr('href', completeUrl)
.attr('target', '_blank')
.attr('rel', 'noopener');
const container = $(this).closest('span').parent();
const spans = container.find('span').not(':has(a)');
if (spans.length >= 2) {
let referenceText = spans.eq(1).text().trim();
let periodText = spans.eq(2).text().trim();
let type = (periodText.includes('An')) ? "NMD" :
((parseInt(periodText.split('-')[0]) < 1793) ? "BMS" : "NMD");
let descText = container.find('a').text();
if (/^Naissances\.?$/.test(descText)) {
type = "N";
} else if (/^Mariages\.?$/.test(descText)) {
type = "M";
} else if (/^Décès\.?$/.test(descText)) {
type = "D";
} else if (descText === "Baptêmes, mariages.") {
type = "BM";
} else if (descText === "Mariages, décès.") {
type = "MD";
} else if (descText === "Sépultures.") {
type = "S";
}
const regSrc = `AD57 ${type} ${commune} ${periodText} ${referenceText}`;
const localStorageKey = `AD57-${uniqueId}`;
if (localStorageKey && imageSrc && regSrc && descText) {
const localStorageData = {
imageSrc: imageSrc,
regSrc: regSrc,
descText: descText
};
console.log('Attempting to set localStorage:', {
key: localStorageKey, // Use the new key format
data: localStorageData
});
try {
if (!localStorage.getItem(localStorageKey)) {
localStorage.setItem(localStorageKey, JSON.stringify(localStorageData));
console.log(`Successfully set localStorage for key: ${localStorageKey}`);
} else {
console.log(`Key ${localStorageKey} already exists in localStorage`);
}
} catch (error) {
console.error('Error setting localStorage:', error);
}
}
spans.not(':first').remove();
container.find('br').remove();
const $regSrc = $('<span>')
.text(regSrc)
.css({
'user-select': 'all',
});
container.append($regSrc);
}
});
}
// Add a function to log localStorage at the end of processing
function finalLocalStorageLog() {
console.log('Final LocalStorage State:');
logLocalStorageEntries();
}
function copyCurrentPageResults(registersProcessed) {
const resultatsDiv = document.getElementById('resultat');
let resultatsCompletsDiv = document.getElementById('resultats_complets');
if (!resultatsCompletsDiv) {
resultatsCompletsDiv = document.createElement('div');
resultatsCompletsDiv.id = 'resultats_complets';
document.body.prepend(resultatsCompletsDiv);
}
const resultatsPageDiv = document.createElement('div');
resultatsPageDiv.id = `resultats${registersProcessed}`;
resultatsCompletsDiv.appendChild(resultatsPageDiv);
let currentResult = resultatsDiv.querySelector('#zoom ~ *');
let registersOnThisPage = Math.min(20, totalResults - registersProcessed);
for (let i = 0; i < registersOnThisPage && currentResult; i++) {
let elementsAddedOnThisRegister = 0;
while (elementsAddedOnThisRegister < 4 && currentResult) {
if (currentResult.tagName.toLowerCase() !== 'script') {
resultatsPageDiv.appendChild(currentResult.cloneNode(true));
elementsAddedOnThisRegister++;
}
currentResult = currentResult.nextElementSibling;
}
}
return registersOnThisPage;
}
// Modify the existing processPagination function to call finalLocalStorageLog
function processPagination(registersProcessed = 0) {
const processedOnThisPage = copyCurrentPageResults(registersProcessed);
if (!processedOnThisPage) return;
const remainingRegisters = totalResults - (registersProcessed + processedOnThisPage);
if (remainingRegisters > 0) {
const nextOffset = registersProcessed + 20;
$.blockUI('<h1><img src=/visualiseur/application/views/images/busy.gif /> <b>Recherche en cours</b></h1>');
$('#resultat').load(`/visualiseur/index.php/rechercheTheme/paginer/${nextOffset}`, function() {
$.unblockUI();
setTimeout(() => processPagination(nextOffset), 500);
});
} else {
applyCleanupAndFormatting();
finalLocalStorageLog(); // Add this line to log localStorage at the end
}
}
(function checkBlockUI() {
const blockUiElements = document.querySelectorAll('.blockUI');
if (blockUiElements.length === 0) {
if (!totalResults) totalResults = getTotalResults();
processPagination(0);
} else {
setTimeout(checkBlockUI, 500);
}
})();
})();