-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserscriptCode.ts
More file actions
1266 lines (1190 loc) · 56.3 KB
/
Copy pathuserscriptCode.ts
File metadata and controls
1266 lines (1190 loc) · 56.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Userscript de PRODUCCIÓ — Venus (eina interna Reus Refugi) per Mercurio.
*
* Diferent del MVP (mock/userscript/template.user.js) en què:
* - No té payloads hardcoded; cerca i llegeix d'Airtable via Worker
* - @updateURL apunta al Worker per auto-update via Tampermonkey
* - Inclou caixa de cerca per nom/cognom/passaport
*
* Es serveix des de la ruta `GET /mercurio.user.js` del Worker, que
* substitueix els placeholders `__WORKER_URL__` i `__SHARED_SECRET__`
* a runtime amb la URL pública del Worker i el secret.
*
* El userscript s'injecta a quatre pantalles de Mercurio:
* - seleccionModelo-XX.html → MODE INFO: clica cas, diu si EX31/EX32
* - nuevaSolicitud-EX31/EX32.html → MODE OMPLIR: clica cas, omple 144 camps
* - presentacionTelematicaDocumentacion → MODE PUJAR: clica cas, puja els docs
* d'Airtable seqüencialment (1500ms
* entre cada un — anti-bot Mercurio).
* NO clica el "Continuar" final;
* el voluntari revisa i submet.
*
* Nota seguretat: el SHARED_SECRET viatja embedded al userscript.
* El nostre threat model: voluntaris RECEX de confiança + audit logs
* Cloudflare. Si això canvia, considerar OAuth per voluntari.
*/
export const USERSCRIPT_TEMPLATE = `// ==UserScript==
// @name Venus — Auto-Fill Mercurio
// @namespace https://reusrefugi.cat
// @version __VERSION__
// @description Cerca un cas d'Airtable Venus i omple el form EX-31/EX-32 de Mercurio amb 1 click, o puja els documents d'Airtable a la pantalla de documentació. NO submiteja.
// @author Reus Refugi
// @match https://mercurio.delegaciondelgobierno.gob.es/mercurio/seleccionModelo-*.html*
// @match https://mercurio.delegaciondelgobierno.gob.es/mercurio/nuevaSolicitud-EX31.html*
// @match https://mercurio.delegaciondelgobierno.gob.es/mercurio/nuevaSolicitud-EX32.html*
// @match https://mercurio.delegaciondelgobierno.gob.es/mercurio/presentacionTelematicaDocumentacion.html*
// @updateURL __WORKER_URL__/mercurio.user.js
// @downloadURL __WORKER_URL__/mercurio.user.js
// @grant none
// @run-at document-end
// ==/UserScript==
(function () {
'use strict';
const WORKER_URL = '__WORKER_URL__';
const SHARED_SECRET = '__SHARED_SECRET__';
// Versió visible al subtítol del modal — útil per confirmar amb voluntaris
// si han actualitzat (Tampermonkey re-comprova diàriament; també poden
// forçar-ho amb "Check for updates").
const VENUS_VERSION = '__VERSION__';
// ─── Límits de mida de Mercurio ─────────────────────────────────────
// Mercurio rebutja amb un HTTP 500 opac els fitxers grans (límit declarat
// 6 MB/fitxer, ~15 MB/expedient). Gate dur: comprovem la mida real del
// blob abans d'enviar res — el voluntari rep un missatge clar en lloc
// d'un 500 misteriós, i mai s'envia un fitxer que Mercurio rebutjaria.
const MAX_DOC_BYTES = 6 * 1024 * 1024;
const MAX_CASE_BYTES = 15 * 1024 * 1024;
// ─── Skip lists ─────────────────────────────────────────────────────
const CASCADE_FIELDS = new Set([
'extCodigoProvincia', 'extCodigoMunicipio', 'extCodigoLocalidad',
'reaCodigoProvinciaReagrupante', 'reaCodigoMunicipioReagrupante', 'reaCodigoLocalidadReagrupante',
'preCodigoProvinciaPresentador', 'preCodigoMunicipioPresentador', 'preCodigoLocalidadPresentador',
]);
const SKIP_FIELDS = new Set([
// reaCodigoProvinciaReagrupante: hidden/inaccessible in the UI for non-dependent
// cases. Skipping prevents Venus from firing the change event that triggers
// Mercurio's getMunicipios handler and resets extCodigoMunicipio/Localidad.
'reaCodigoProvinciaReagrupante',
'reaCodigoMunicipioReagrupante', 'reaCodigoLocalidadReagrupante',
'preCodigoMunicipioPresentador', 'preCodigoLocalidadPresentador',
'preNombrePresentador', 'preTipodocumentoPresentador', 'preNiePresentador',
'notNombreNotificacion', 'notTipodocumentoNotificacion', 'notNieNotificacion',
'_chkDecla1', '_chkDecla2', '_chkDecla3',
'_chkIncapacidad', '_chkConsientoConsultaDocumentos', '_chkConsentimientoNotificacion',
]);
// ─── Worker fetch helpers ───────────────────────────────────────────
async function workerGet(path) {
const r = await fetch(WORKER_URL + path, {
headers: { 'Authorization': 'Bearer ' + SHARED_SECRET },
});
if (!r.ok) throw new Error(\`\${r.status} \${await r.text()}\`);
return r.json();
}
// ─── Form fill primitives ───────────────────────────────────────────
function fireEvents(el, types) {
for (const t of types) el.dispatchEvent(new Event(t, { bubbles: true }));
if (window.jQuery) {
try { for (const t of types) window.jQuery(el).trigger(t); } catch (e) {}
}
}
function setField(name, value) {
if (SKIP_FIELDS.has(name)) return { name, status: 'skipped' };
const els = document.querySelectorAll('[name="' + CSS.escape(name) + '"]');
if (els.length === 0) return { name, status: 'not_found', value };
const el = els[0];
const tag = el.tagName.toLowerCase();
const type = (el.type || '').toLowerCase();
try {
if (tag === 'select') {
const opt = [...el.options].find(o => o.value === value);
if (!opt) return { name, status: 'invalid_option', value };
el.value = value;
fireEvents(el, ['change']);
return { name, status: 'ok', value };
}
if (type === 'checkbox') {
const want = value === 'true' || value === 'on';
if (el.checked !== want) { el.checked = want; fireEvents(el, ['change']); }
return { name, status: 'ok', value: want ? 'checked' : 'unchecked' };
}
if (type === 'radio') {
// Per radios, native click() és més segur: dispara el handler complet
// de Mercurio (que p.ex. mostra inputs dinàmics com expAsilo). 'change'
// event sol no els activa.
for (const r of els) if (r.value === value) { r.click(); break; }
return { name, status: 'ok', value };
}
// Respect the HTML maxlength — el.value assignment bypasses it, so we
// enforce it here and surface it as an error rather than silently
// submitting a value the server's DB column will reject.
if (el.maxLength > 0 && String(value).length > el.maxLength) {
return { name, status: 'invalid_maxlength', value, maxLength: el.maxLength };
}
el.value = value;
fireEvents(el, ['input', 'change']);
return { name, status: 'ok', value };
} catch (e) { return { name, status: 'error', value, error: String(e) }; }
}
async function waitForOption(name, value, timeoutMs = 3000) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
const el = document.querySelector('[name="' + CSS.escape(name) + '"]');
if (el && el.options) for (const o of el.options) if (o.value === value) return el;
await new Promise(r => setTimeout(r, 80));
}
return null;
}
async function fillCascade(payload) {
const results = [];
const blocks = [
['extCodigoProvincia', 'extCodigoMunicipio', 'extCodigoLocalidad'],
['reaCodigoProvinciaReagrupante', 'reaCodigoMunicipioReagrupante', 'reaCodigoLocalidadReagrupante'],
['preCodigoProvinciaPresentador', 'preCodigoMunicipioPresentador', 'preCodigoLocalidadPresentador'],
];
for (const block of blocks) {
const [, muniName, locName] = block;
// Skip TOT el bloc si muni+loc buits — un canvi a provincia "vague"
// pot disparar handlers globals de Mercurio que reseten ext.
if (!payload[muniName] && !payload[locName]) continue;
for (const fieldName of block) {
const value = payload[fieldName];
if (!value) continue;
const el = await waitForOption(fieldName, value);
if (!el) { results.push({ name: fieldName, status: 'cascade_timeout', value }); continue; }
el.value = value;
fireEvents(el, ['change']);
results.push({ name: fieldName, status: 'ok_cascade', value });
await new Promise(r => setTimeout(r, 200));
}
}
// VERIFICATION PASS: després de tot, esperem 600ms i revisem que
// muni/loc d'ext no s'hagin esborrat per side effects (CP handler,
// re-renderització, etc.). Si sí, retry.
await new Promise(r => setTimeout(r, 600));
for (const fieldName of ['extCodigoMunicipio', 'extCodigoLocalidad']) {
const wanted = payload[fieldName];
if (!wanted) continue;
const el = document.querySelector('[name="' + CSS.escape(fieldName) + '"]');
if (!el) continue;
if (el.value === wanted) continue;
// Reset detectat — retry
const opt = await waitForOption(fieldName, wanted, 1500);
if (!opt) {
results.push({ name: fieldName, status: 'cascade_timeout_retry', value: wanted });
continue;
}
el.value = wanted;
fireEvents(el, ['change']);
results.push({ name: fieldName, status: 'ok_cascade_retry', value: wanted, reason: 'reset detectat, re-setejat' });
await new Promise(r => setTimeout(r, 200));
}
return results;
}
/**
* Ordre de fillament:
* 1. datosForAut (radio) → click() per a que Mercurio renderitzi els
* camps dinàmics (p.ex. expAsilo per a EX-31 PI). Espera 400ms.
* 2. Tots els camps simples (text/checkbox/select estàtic) ABANS de
* les cascades, perquè Mercurio té handlers (p.ex. el del CP) que
* poden re-derivar municipi/localitat. Si setem CP ABANS, el handler
* ja s'ha disparat quan triem muni manualment.
* 3. Cascades adreça (provincia → municipi → localitat) AL FINAL.
*/
async function fillAll(payload) {
const results = [];
// Phase 1: radio supuesto + click()
if (payload.datosForAut) {
results.push(setField('datosForAut', String(payload.datosForAut)));
await new Promise(r => setTimeout(r, 400));
}
// Phase 2: tot menys cascades (incl. CP, expAsilo, etc.)
for (const [name, value] of Object.entries(payload)) {
if (name === 'datosForAut') continue;
if (CASCADE_FIELDS.has(name)) continue;
results.push(setField(name, String(value)));
}
// Phase 2-bis: retry de camps not_found. Alguns inputs es renderitzen
// condicionalment després d'un canvi de checkbox/radio (p.ex.
// descActividadDecla3 només apareix si chkDecla3 marcat). Si el
// handler de Mercurio és async i la primera iteració no troba el DOM,
// esperem 300ms i fem un re-fill només per als not_found.
const notFoundIdx = [];
for (let i = 0; i < results.length; i++) {
if (results[i].status === 'not_found') notFoundIdx.push(i);
}
if (notFoundIdx.length > 0) {
await new Promise(r => setTimeout(r, 300));
for (const idx of notFoundIdx) {
const orig = results[idx];
const v = payload[orig.name];
if (!v) continue;
const retry = setField(orig.name, String(v));
if (retry.status === 'ok') {
results[idx] = { name: orig.name, status: 'ok_retry', value: retry.value };
}
}
}
// Phase 3: cascades adreça AL FINAL (perquè handlers anteriors no les resetegin)
results.push(...await fillCascade(payload));
// Phase 4: late overwrite per camps que Mercurio omple ASÍNCRONAMENT
// des de la sessió Cl@ve/IDcat. Símptoma: el voluntari obre EX31/EX32,
// el userscript escriu notEmailNotificacion='regularitzacio@reusrefugi.cat',
// i segons després Mercurio fa un fetch al perfil del cert i sobreescriu
// amb el gmail personal del voluntari. Resultat: el camp acaba mostrant
// les dades personals d'Elena/Eduard en comptes de les de l'entitat.
//
// Estratègia:
// 1) Esperar 1500ms (típicament la crida cert-fill de Mercurio acaba
// abans). Re-escriure els 4 camps si no coincideixen amb el payload.
// 2) Polling fire-and-forget cada 500ms durant 3s addicionals — defensa
// contra cert-fills lents. NO bloqueja la UI; fillAll() retorna
// immediatament després del pas 1 perquè el voluntari no esperi.
//
// NOTA: només toquem email/mòbil (entitat), MAI nom/NIE/tipoDoc — aquests
// venen del cert del voluntari i han de coincidir amb qui signa.
const LATE_OVERWRITE_FIELDS = [
'notEmailNotificacion',
'notTelefonoMovilNotificacion',
'preEmailPresentador',
'preTelefonoMovilPresentador',
];
await new Promise(r => setTimeout(r, 1500));
for (const name of LATE_OVERWRITE_FIELDS) {
const want = payload[name];
if (!want) continue;
const el = document.querySelector('[name="' + CSS.escape(name) + '"]');
if (!el) continue;
if (el.value === want) continue;
el.value = want;
fireEvents(el, ['input', 'change']);
results.push({ name, status: 'ok_late_overwrite', value: want, reason: 'cert auto-fill sobreescrit' });
}
// Phase 4-bis (fire-and-forget): polling per 3s. Si Mercurio fa un cert-fill
// tardà (>1500ms) o el voluntari clica la pestanya DOMICILIO i Mercurio
// re-injecta dades, ho corregim silenciosament. No esperem el resultat
// per no bloquejar la UI.
(async () => {
const POLL_DURATION = 3000;
const POLL_INTERVAL = 500;
const startPoll = Date.now();
while (Date.now() - startPoll < POLL_DURATION) {
await new Promise(r => setTimeout(r, POLL_INTERVAL));
for (const name of LATE_OVERWRITE_FIELDS) {
const want = payload[name];
if (!want) continue;
const el = document.querySelector('[name="' + CSS.escape(name) + '"]');
if (!el || el.value === want) continue;
el.value = want;
fireEvents(el, ['input', 'change']);
}
}
})();
return results;
}
// ─── UI ─────────────────────────────────────────────────────────────
// Inline SVGs (heart, search, info, arrow). Stroke/fill colors set via CSS
// currentColor on parent so re-styling només requereix canviar 'color'.
const ICON_HEART = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/></svg>';
const ICON_SEARCH = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>';
const ICON_INFO = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>';
const ICON_ARROW = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>';
const ICON_CHECK = '<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/></svg>';
const ICON_ALERT = '<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><path d="M12 2C6.47 2 2 6.47 2 12s4.47 10 10 10 10-4.47 10-10S17.53 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/></svg>';
// ─── Styles (scoped) ───────────────────────────────────────────────
// Tot prefixat .venus-* + reset agressiu per aïllar de l'CSS legacy de
// Mercurio (jQuery UI, etc.). Inter via Google Fonts amb fallback system.
function injectStyles() {
if (document.getElementById('venus-fonts')) return;
const link = document.createElement('link');
link.id = 'venus-fonts';
link.rel = 'stylesheet';
link.href = 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap';
document.head.appendChild(link);
const style = document.createElement('style');
style.id = 'venus-styles';
// NB: tots els selectors interns prefixats amb .venus-modal per garantir
// (0,2,0) d'especificitat — supera el reset universal '.venus-modal *'
// (0,1,1) i evita que els resets de Mercurio (jQuery UI / antic) afectin.
// El reset universal NO toca 'color' per deixar que els SVG heretin del
// span pare via currentColor (cor púrpura, lupa gris, etc.).
style.textContent = \`
.venus-modal {
font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
font-size: 14px;
line-height: 1.4;
color: #1A1424;
position: fixed;
top: 16px;
right: 16px;
z-index: 99999;
width: 380px;
background: #FFFFFF;
border: 1px solid #E6E1EE;
border-radius: 12px;
box-shadow: 0 8px 24px rgba(26, 20, 36, 0.08);
overflow: hidden;
}
.venus-modal, .venus-modal *, .venus-modal *::before, .venus-modal *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.venus-modal .venus-header {
display: flex;
align-items: center;
gap: 8px;
padding: 14px 16px;
border-bottom: 1px solid #E6E1EE;
}
.venus-modal .venus-heart { color: #7C3AED; display: flex; }
.venus-modal .venus-title { font-size: 16px; font-weight: 700; color: #1A1424; }
.venus-modal .venus-sep { color: #B8B0C7; font-weight: 400; font-size: 16px; }
.venus-modal .venus-subtitle { font-size: 14px; font-weight: 400; color: #6B607E; flex: 1; }
.venus-modal .venus-count { font-size: 13px; color: #6B607E; font-variant-numeric: tabular-nums; }
.venus-modal .venus-search-wrap {
position: relative;
padding: 12px 16px 8px;
}
.venus-modal .venus-search-icon {
position: absolute;
left: 28px;
top: 50%;
transform: translateY(-50%);
color: #6B607E;
display: flex;
pointer-events: none;
}
.venus-modal .venus-search-input {
display: block;
width: 100%;
padding: 14px 14px 14px 42px;
font-size: 15px;
font-weight: 400;
font-family: inherit;
color: #1A1424;
background: #F6F3FB;
border: 1.5px solid #E6E1EE;
border-radius: 10px;
outline: none;
transition: border-color 120ms, box-shadow 120ms;
}
.venus-modal .venus-search-input::placeholder { color: #6B607E; opacity: 1; }
.venus-modal .venus-search-input:focus {
border-color: #7C3AED;
box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.15);
}
/* Container amb padding lateral per inset les files del modal edge.
* gap entre files (substitueix el border-top divider) perquè el
* border-radius dels rows es vegi sense clip. */
.venus-modal .venus-results {
max-height: 360px;
overflow-y: auto;
padding: 4px 8px 8px;
display: flex;
flex-direction: column;
gap: 2px;
}
/* Row base — idle. 1.5px transparent border reserva l'espai per
* evitar layout-shift quan saltem a estats colored. border-radius
* 10px perquè els borders colored (filling/done/error) tinguin
* cantonades suaus i no toquin el border del modal. */
.venus-modal .venus-row {
display: flex;
align-items: center;
gap: 14px;
padding: 14px 12px;
cursor: pointer;
background: #FFFFFF;
border: 1.5px solid transparent;
border-radius: 10px;
width: 100%;
text-align: left;
font-family: inherit;
font-size: 14px;
color: inherit;
transition: background 100ms, border-color 100ms;
}
.venus-modal .venus-row:hover,
.venus-modal .venus-row:focus,
.venus-modal .venus-row:active {
background: #F4EFFB;
outline: none;
}
.venus-modal .venus-row:focus-visible {
background: #F4EFFB;
border-color: #7C3AED;
outline: none;
}
/* Row state overrides — declarats després perquè la cadena hover/focus
* no els sobreescrigui. Tot l'estat manté la mateixa paleta. */
.venus-modal .venus-row[data-state="filling"],
.venus-modal .venus-row[data-state="filling"]:hover,
.venus-modal .venus-row[data-state="filling"]:focus,
.venus-modal .venus-row[data-state="filling"]:active,
.venus-modal .venus-row[data-state="filling"]:focus-visible {
background: #F0EAFB;
border-color: #7C3AED;
}
.venus-modal .venus-row[data-state="done"],
.venus-modal .venus-row[data-state="done"]:hover,
.venus-modal .venus-row[data-state="done"]:focus,
.venus-modal .venus-row[data-state="done"]:active,
.venus-modal .venus-row[data-state="done"]:focus-visible {
background: #E8F5EE;
border-color: #0E7A45;
}
.venus-modal .venus-row[data-state="done"] .venus-name { color: #0E3A22; }
.venus-modal .venus-row[data-state="done"] .venus-meta { color: #3D6B52; }
.venus-modal .venus-row[data-state="error"],
.venus-modal .venus-row[data-state="error"]:hover,
.venus-modal .venus-row[data-state="error"]:focus,
.venus-modal .venus-row[data-state="error"]:active,
.venus-modal .venus-row[data-state="error"]:focus-visible {
background: #FCEBEA;
border-color: #B42318;
}
.venus-modal .venus-row[data-state="error"] .venus-name { color: #5A1410; }
.venus-modal .venus-row[data-state="error"] .venus-meta { color: #8A3A33; }
.venus-modal .venus-badge {
flex: 0 0 auto;
width: 56px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.02em;
border-radius: 8px;
font-variant-numeric: tabular-nums;
}
.venus-modal .venus-badge-ex31 { background: #D6F0EE; color: #0E6F6F; }
.venus-modal .venus-badge-ex32 { background: #FCE8C9; color: #8A4B00; }
.venus-modal .venus-badge-warn { background: #FCEBEA; color: #B42318; }
.venus-modal .venus-row-content {
flex: 1 1 auto;
min-width: 0;
display: flex;
flex-direction: column;
gap: 2px;
}
.venus-modal .venus-name {
font-size: 16px;
font-weight: 600;
color: #1A1424;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.venus-modal .venus-meta {
font-size: 13px;
font-weight: 400;
color: #6B607E;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.venus-modal .venus-id { font-variant-numeric: tabular-nums; }
.venus-modal .venus-dot { margin: 0 6px; color: #B8B0C7; }
/* Row tail — slot dret (hint hover/focus, spinner, check, alert).
* Color base #6B607E gris; per estats canvia a la color del border. */
.venus-modal .venus-row-tail {
flex: 0 0 auto;
display: flex;
align-items: center;
gap: 6px;
font-size: 13px;
font-weight: 500;
white-space: nowrap;
color: #6B607E;
min-height: 18px;
}
.venus-modal .venus-row[data-state="filling"] .venus-row-tail { color: #7C3AED; }
.venus-modal .venus-row[data-state="done"] .venus-row-tail { color: #0E7A45; }
.venus-modal .venus-row[data-state="error"] .venus-row-tail { color: #B42318; }
.venus-modal .venus-row-hint-hover,
.venus-modal .venus-row-hint-focus {
display: none;
align-items: center;
}
.venus-modal .venus-row[data-state="idle"]:hover .venus-row-hint-hover {
display: flex;
color: #6B607E;
}
.venus-modal .venus-row[data-state="idle"]:focus-visible .venus-row-hint-hover {
display: none;
}
.venus-modal .venus-row[data-state="idle"]:focus-visible .venus-row-hint-focus {
display: flex;
color: #7C3AED;
font-weight: 600;
}
/* Spinner */
.venus-modal .venus-spinner {
width: 14px;
height: 14px;
border: 2px solid rgba(124, 58, 237, 0.25);
border-top-color: #7C3AED;
border-radius: 50%;
animation: venus-spin 0.8s linear infinite;
display: inline-block;
flex: 0 0 auto;
}
@keyframes venus-spin { to { transform: rotate(360deg); } }
.venus-modal .venus-empty {
padding: 24px 16px;
text-align: center;
font-size: 13px;
color: #6B607E;
}
/* Status (progress block / info card / errors) */
.venus-modal .venus-status:empty { display: none; }
.venus-modal .venus-status {
padding: 12px 16px;
font-size: 13px;
color: #1A1424;
border-top: 1px solid #E6E1EE;
}
.venus-modal .venus-progress-block {
display: flex;
flex-direction: column;
gap: 8px;
}
.venus-modal .venus-progress-header {
display: flex;
align-items: center;
justify-content: space-between;
font-size: 13px;
color: #1A1424;
font-weight: 500;
}
.venus-modal .venus-progress-detail-link {
font-size: 13px;
color: #7C3AED;
text-decoration: none;
font-weight: 500;
cursor: pointer;
background: none;
border: none;
font-family: inherit;
}
.venus-modal .venus-progress-detail-link:hover { text-decoration: underline; }
.venus-modal .venus-progress-bar {
display: flex;
height: 6px;
background: #F0ECF5;
border-radius: 3px;
overflow: hidden;
}
.venus-modal .venus-progress-seg-ok { background: #0E7A45; height: 100%; }
.venus-modal .venus-progress-seg-skip { background: #D58A1A; height: 100%; }
.venus-modal .venus-progress-seg-err { background: #B42318; height: 100%; }
.venus-modal .venus-progress-legend {
display: flex;
gap: 16px;
font-size: 12px;
color: #6B607E;
flex-wrap: wrap;
}
.venus-modal .venus-legend-item { display: flex; align-items: center; gap: 6px; }
.venus-modal .venus-legend-dot {
width: 8px;
height: 8px;
border-radius: 50%;
display: inline-block;
flex: 0 0 auto;
}
.venus-modal .venus-dot-ok { background: #0E7A45; }
.venus-modal .venus-dot-skip { background: #D58A1A; }
.venus-modal .venus-dot-err { background: #B42318; }
.venus-modal .venus-legend-item strong {
font-weight: 600;
color: #1A1424;
font-variant-numeric: tabular-nums;
}
.venus-modal .venus-progress-detail-panel {
display: none;
margin-top: 4px;
padding: 8px 10px;
background: #F6F3FB;
border-radius: 6px;
font-family: ui-monospace, SFMono-Regular, monospace;
font-size: 11px;
color: #1A1424;
white-space: pre-wrap;
max-height: 160px;
overflow-y: auto;
}
.venus-modal .venus-progress-detail-panel.open { display: block; }
.venus-modal .venus-merge-note {
margin-top: 4px;
padding: 8px 10px;
background: #F0EAFB;
border-radius: 6px;
font-size: 11px;
color: #4A3D63;
line-height: 1.6;
}
.venus-modal .venus-merge-note strong { color: #1A1424; font-weight: 600; }
.venus-modal .venus-info-card {
padding: 10px 12px;
background: #F4EFFB;
border: 1px solid #E6E1EE;
border-radius: 8px;
color: #1A1424;
}
.venus-modal .venus-info-title {
font-size: 14px;
font-weight: 700;
color: #7C3AED;
margin-bottom: 4px;
}
.venus-modal .venus-error-line { color: #B42318; font-weight: 600; }
.venus-modal .venus-footer {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 16px;
background: #FAF8FD;
border-top: 1px solid #E6E1EE;
font-size: 13px;
color: #6B607E;
}
.venus-modal .venus-footer-icon { color: #6B607E; display: flex; flex: 0 0 auto; }
\`;
document.head.appendChild(style);
}
function injectPanel() {
injectStyles();
const wrap = document.createElement('div');
wrap.className = 'venus-modal';
wrap.setAttribute('role', 'dialog');
wrap.setAttribute('aria-label', 'Venus — Auto-emplenar formulari Mercurio');
wrap.innerHTML = \`
<div class="venus-header">
<span class="venus-heart">\${ICON_HEART}</span>
<span class="venus-title">Venus</span>
<span class="venus-sep">—</span>
<span class="venus-subtitle">v\${VENUS_VERSION}</span>
<span class="venus-count" id="venus-count"></span>
</div>
<div class="venus-search-wrap">
<span class="venus-search-icon">\${ICON_SEARCH}</span>
<input id="venus-search" type="text" class="venus-search-input" placeholder="Cerca per nom o cognom…" autocomplete="off" spellcheck="false">
</div>
<div id="venus-results" class="venus-results" style="display:none"></div>
<div id="venus-status" class="venus-status"></div>
<div class="venus-footer">
<span class="venus-footer-icon">\${ICON_INFO}</span>
<span id="venus-footer-text">\${footerText()}</span>
</div>
\`;
document.body.appendChild(wrap);
const search = wrap.querySelector('#venus-search');
const results = wrap.querySelector('#venus-results');
const status = wrap.querySelector('#venus-status');
const count = wrap.querySelector('#venus-count');
let timer = null;
search.addEventListener('input', () => {
clearTimeout(timer);
// Llista oculta mentre la cerca és buida — així el panell no tapa la
// navegació de la pàgina. Només es renderitza en escriure.
if (!search.value.trim()) {
results.style.display = 'none';
results.innerHTML = '';
count.textContent = '';
return;
}
timer = setTimeout(() => doSearch(search.value, results, status, count), 250);
});
}
// ─── Mode (segons URL) ──────────────────────────────────────────────
// Quatre pantalles de Mercurio s'injecten al userscript. La lògica de
// click depèn de la pantalla, així com el copy del subtitle/footer.
function getMode() {
const p = location.pathname;
if (p.includes('presentacionTelematicaDocumentacion')) return 'upload';
if (p.includes('seleccionModelo')) return 'select';
return 'fill'; // nuevaSolicitud-EX31/EX32 (default)
}
const MODE_TEXTS = {
fill: {
subtitle: 'Auto-emplenar formulari',
idle: 'Clica un cas per emplenar el formulari automàticament',
busy: 'Emplenant camps… no tanquis aquesta finestra',
done: 'Formulari emplenat. Revisa abans d\\'enviar.',
},
upload: {
subtitle: 'Pujar documents',
idle: 'Clica un cas per pujar els seus documents a Mercurio',
busy: 'Pujant documents… no tanquis aquesta finestra',
done: 'Documents pujats. Revisa i clica CONTINUAR.',
},
select: {
subtitle: 'Seleccionar formulari',
idle: 'Clica un cas per saber si és EX-31 o EX-32',
busy: '',
done: '',
},
};
function modeText(key) { return (MODE_TEXTS[getMode()] || MODE_TEXTS.fill)[key]; }
function footerText() { return modeText('idle'); }
function setFooterText(t) {
const el = document.getElementById('venus-footer-text');
if (el) el.textContent = t;
}
function footerError(n) {
if (getMode() === 'upload') {
return n === 1
? 'Hi ha 1 document que no s\\'ha pogut pujar. Revisa\\'l.'
: \`Hi ha \${n} documents que no s'han pogut pujar. Revisa'ls.\`;
}
return n === 1
? 'Hi ha 1 camp que no s\\'ha pogut emplenar. Revisa\\'l.'
: \`Hi ha \${n} camps que no s'han pogut emplenar. Revisa'ls.\`;
}
// Estat visual de la fila (idle/filling/done/error). Només una fila pot
// estar en estat actiu (filling/done/error) alhora — quan posem estat a
// una nova, reseten les altres a idle.
function setRowState(row, state) {
if (!row) return;
if (state !== 'idle') {
const all = document.querySelectorAll('.venus-modal .venus-row');
for (const r of all) {
if (r !== row && r.dataset.state !== 'idle') {
r.dataset.state = 'idle';
const t = r.querySelector('.venus-row-tail');
if (t) t.innerHTML = '<span class="venus-row-hint-hover">' + ICON_ARROW + '</span><span class="venus-row-hint-focus">↵ Enter</span>';
}
}
}
row.dataset.state = state;
const tail = row.querySelector('.venus-row-tail');
if (!tail) return;
if (state === 'filling') {
tail.innerHTML = '<span class="venus-spinner"></span><span>Emplenant…</span>';
} else if (state === 'done') {
tail.innerHTML = ICON_CHECK + '<span>Emplenat</span>';
} else if (state === 'error') {
tail.innerHTML = ICON_ALERT + '<span>Error</span>';
} else {
tail.innerHTML = '<span class="venus-row-hint-hover">' + ICON_ARROW + '</span><span class="venus-row-hint-focus">↵ Enter</span>';
}
}
// Renderitza la barra de progrés segmentada + llegenda + "Veure detall".
// ok/skip/err són els counts; issues és l'array per al detall expandible.
function renderProgress(statusDiv, ok, skip, err, issues) {
const total = ok + skip + err;
const segs = [];
if (ok > 0) segs.push(\`<div class="venus-progress-seg-ok" style="flex:\${ok}"></div>\`);
if (skip > 0) segs.push(\`<div class="venus-progress-seg-skip" style="flex:\${skip}"></div>\`);
if (err > 0) segs.push(\`<div class="venus-progress-seg-err" style="flex:\${err}"></div>\`);
const detailLink = issues && issues.length
? '<button type="button" class="venus-progress-detail-link" id="venus-detail-link">Veure detall →</button>'
: '';
statusDiv.innerHTML = \`
<div class="venus-progress-block">
<div class="venus-progress-header">
<span>Emplenat <strong>\${ok}</strong> de <strong>\${total}</strong> camps</span>
\${detailLink}
</div>
<div class="venus-progress-bar">\${segs.join('')}</div>
<div class="venus-progress-legend">
<span class="venus-legend-item"><span class="venus-legend-dot venus-dot-ok"></span><strong>\${ok}</strong> emplenats</span>
<span class="venus-legend-item"><span class="venus-legend-dot venus-dot-skip"></span><strong>\${skip}</strong> omesos</span>
<span class="venus-legend-item"><span class="venus-legend-dot venus-dot-err"></span><strong>\${err}</strong> errors</span>
</div>
<div class="venus-progress-detail-panel" id="venus-detail-panel"></div>
</div>
\`;
if (issues && issues.length) {
const link = document.getElementById('venus-detail-link');
const panel = document.getElementById('venus-detail-panel');
let lines = '';
for (const r of issues) {
lines += escapeHtml(r.name) + ': ' + escapeHtml(r.status);
if (r.value) lines += ' ("' + escapeHtml(String(r.value)) + '")';
lines += '\\n';
}
panel.textContent = lines;
link.addEventListener('click', () => panel.classList.toggle('open'));
}
}
// Parseig: "RR-003-COGNOM--COGNOM" → "RR-003"
function shortIdCas(idCas) {
const m = String(idCas || '').match(/^(RR-\\d+)/);
return m ? m[1] : (idCas || '');
}
// Parseig: "DA 20ª – Sol·licitant PI" → ["DA 20ª", "Sol·licitant PI"]
function splitViaLegal(viaLegal) {
const s = String(viaLegal || '');
const idx = s.indexOf(' – ');
if (idx === -1) return [s, ''];
return [s.slice(0, idx), s.slice(idx + 3)];
}
async function doSearch(q, container, status, count) {
container.style.display = 'flex';
container.innerHTML = '<div class="venus-empty">Cercant…</div>';
count.textContent = '';
try {
const data = await workerGet('/mercurio/cases?q=' + encodeURIComponent(q || ''));
const cases = data.cases || [];
if (cases.length === 0) {
container.innerHTML = '<div class="venus-empty">Cap cas trobat.</div>';
count.textContent = '';
return;
}
count.textContent = cases.length + (cases.length === 1 ? ' cas' : ' casos');
container.innerHTML = '';
for (const c of cases) {
const row = document.createElement('button');
row.className = 'venus-row';
row.type = 'button';
row.dataset.state = 'idle';
// Si el cas no té "Via legal" definida a Airtable, no inventem
// EX31/EX32 — pintem badge d'avís i el click llançarà error explícit.
const missingVia = !c.viaLegal || !c.formulario;
const badgeClass = missingVia
? 'venus-badge-warn'
: (c.formulario === 'EX31' ? 'venus-badge-ex31' : 'venus-badge-ex32');
const badgeText = missingVia ? '⚠' : (c.formulario || '');
const [da, tag] = splitViaLegal(c.viaLegal);
const fullName = (escapeHtml(c.nom || '') + ' ' + escapeHtml(c.cognom1 || '')).trim() || '—';
const metaParts = [
\`<span class="venus-id">\${escapeHtml(shortIdCas(c.idCas))}</span>\`,
missingVia ? '<span class="venus-status-err">Sense via legal</span>' : escapeHtml(da || '?'),
(!missingVia && tag) ? escapeHtml(tag) : null,
].filter(Boolean);
const metaHtml = metaParts.join('<span class="venus-dot">·</span>');
row.innerHTML = \`
<span class="venus-badge \${badgeClass}">\${escapeHtml(badgeText)}</span>
<span class="venus-row-content">
<span class="venus-name">\${fullName}</span>
<span class="venus-meta">\${metaHtml}</span>
</span>
<span class="venus-row-tail">
<span class="venus-row-hint-hover">\${ICON_ARROW}</span>
<span class="venus-row-hint-focus">↵ Enter</span>
</span>
\`;
row.addEventListener('click', () => {
// En clicar un cas, plega la llista: deixa només la fila clicada
// visible (amb el seu estat) + la barra de progrés a sota.
for (const r of Array.from(container.children)) {
if (r !== row) r.remove();
}
count.textContent = '';
fillCase(c, status, row);
});
container.appendChild(row);
}
} catch (e) {
container.innerHTML = '<div class="venus-empty"><span class="venus-status-err">Error:</span> ' + escapeHtml(String(e)) + '</div>';
count.textContent = '';
}
}
// ─── MODE PUJAR (presentacionTelematicaDocumentacion.html) ──────────
// Match d'una categoria d'Airtable contra el select id=docAdjuntarAdjuntos
// del DOM. Estratègia en 3 passos:
// 1. EXACT match per label — la taxonomia Airtable usa els labels literals
// del dropdown Mercurio (5 de 6 opcions). Zero ambigüitat.
// 2. Heurística per substring — només cau aquí per al cas abstracte
// "Documentación vía legal" que cobreix 188 (vulnerabilitat, EX-32)
// i 189 (justificativa, EX-31). Mercurio renderitza un label diferent
// segons via legal, així que necessitem heurística per resoldre.
// 3. Fallback "Otros" — categoria desconeguda → puja com a Otros documentos.
function resolveMercurioCode(category, options) {
const cat = String(category || '').trim();
// 1. Exact match (case-insensitive, normalitzant whitespace)
const norm = s => String(s || '').trim().replace(/\\s+/g, ' ').toLowerCase();
const catNorm = norm(cat);
const exact = options.find(o => norm(o.label) === catNorm);
if (exact) return { code: exact.code, label: exact.label, matchedBy: 'exact' };
// 2. Heurística només per al label abstracte "Documentación vía legal..."
// — qualsevol altra categoria Airtable hauria de coincidir literalment.
if (/^documentaci[oó]n.*v[ií]a.*legal/i.test(cat)) {
const found = options.find(o => /vulnerabilidad|justificativa de presentaci[oó]n|entidad colaboradora/i.test(o.label));
if (found) return { code: found.code, label: found.label, matchedBy: 'heuristic:via-legal' };
}
// 3. Fallback Otros
const otros = options.find(o => /otros documentos/i.test(o.label));
if (otros) return { code: otros.code, label: otros.label, matchedBy: 'fallback:otros' };
return options[0] ? { code: options[0].code, label: options[0].label, matchedBy: 'fallback:first' } : null;
}
function readUploadOptions() {
const sel = document.getElementById('docAdjuntarAdjuntos');
if (!sel) return [];
return [...sel.options]
.filter(o => o.value)
.map(o => ({ code: o.value, label: (o.textContent || o.text || '').trim() }));
}
function readUploadedFilenames() {
// A Mercurio real, la classe clAdjunDes està al <span> dins del <td>:
// <td><span class="mf-table-responsive--pseudotd clAdjunDes">passaport.pdf</span></td>
// Selector amb només la classe (sense restriccio de tag) cobreix tant
// aquest cas com qualsevol mock que la posi al <td> directament.
const els = document.querySelectorAll('#tabla_datos_adj .clAdjunDes');
const set = new Set();
for (const el of els) set.add((el.textContent || '').trim().toLowerCase());
return set;
}
// Render progress block per al mode upload. Estructura idèntica al
// renderProgress() del mode fill, però amb llabels "pujats / duplicats /
// errors" i el detail panel mostra issues amb context (filename + raó).
function renderUploadProgress(statusDiv, ok, dup, issues, merged) {
merged = merged || [];
const total = ok + dup + issues.length;
const segs = [];
if (ok > 0) segs.push(\`<div class="venus-progress-seg-ok" style="flex:\${ok}"></div>\`);
if (dup > 0) segs.push(\`<div class="venus-progress-seg-skip" style="flex:\${dup}"></div>\`);
if (issues.length > 0) segs.push(\`<div class="venus-progress-seg-err" style="flex:\${issues.length}"></div>\`);
const detailLink = issues.length
? '<button type="button" class="venus-progress-detail-link" id="venus-detail-link">Veure detall →</button>'
: '';
let mergeBlock = '';
if (merged.length) {
const lines = merged.map(m =>
escapeHtml(String(m.label)) + ' — ' + m.attCount + ' fitxers → 1 PDF'
+ (m.pages ? ' (' + m.pages + ' pàg.)' : '')