Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Szukaj w Archiwach - AUTO Skany (Zawijanie wierszy)
- // @namespace http://tampermonkey.net/
- // @version 4.7
- // @description Jeśli skanów > 3, wyświetla je w wielu rzędach (zawijanie), aby mieściły się na ekranie.
- // @author marek7400
- // @match *://*.szukajwarchiwach.gov.pl/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- // --- 1. STYLE GLOBALNE ---
- function stretchPage() {
- if (document.getElementById('swa-custom-stretch-styles')) {
- return;
- }
- const style = document.createElement('style');
- style.id = 'swa-custom-stretch-styles';
- style.textContent = `
- body .container, body .container-fluid, body #wrapper, body #content, body #main-content {
- max-width: 100% !important; width: 100% !important; padding-left: 20px !important; padding-right: 20px !important;
- }
- table.table { width: 100% !important; max-width: 100% !important; }
- .swa-preview-container {
- display: flex;
- gap: 15px;
- padding: 15px;
- background: #f4f4f4;
- border: 1px solid #ccc;
- margin-top: 8px;
- margin-bottom: 20px;
- border-radius: 4px;
- box-shadow: inset 0 0 5px rgba(0,0,0,0.1);
- }
- .swa-mode-horizontal {
- overflow-x: auto;
- white-space: nowrap;
- }
- .swa-mode-wrapped {
- flex-wrap: wrap;
- white-space: normal;
- justify-content: flex-start;
- }
- .swa-preview-img {
- height: 400px;
- border: 1px solid #777;
- cursor: pointer;
- box-shadow: 3px 3px 8px rgba(0,0,0,0.3);
- transition: transform 0.2s;
- object-fit: contain;
- background: white;
- }
- .swa-preview-img:hover {
- transform: scale(1.03);
- z-index: 10;
- }
- `;
- document.head.appendChild(style);
- }
- stretchPage();
- const processedHrefs = new Set();
- function delay(ms) {
- return new Promise(function(resolve) {
- setTimeout(resolve, ms);
- });
- }
- function clearContainer(container) {
- while (container.firstChild) {
- container.removeChild(container.firstChild);
- }
- }
- // --- 2. FUNKCJA ŁADUJĄCA SKANY ---
- async function loadScansForUnit(link, limit) {
- if (limit <= 0) {
- return;
- }
- const previewContainer = document.createElement('div');
- previewContainer.className = 'swa-preview-container';
- if (limit > 3) {
- previewContainer.classList.add('swa-mode-wrapped');
- } else {
- previewContainer.classList.add('swa-mode-horizontal');
- }
- const loadingSpan = document.createElement('span');
- loadingSpan.style.color = '#0056b3';
- loadingSpan.style.fontSize = '14px';
- const loadingItalic = document.createElement('i');
- loadingItalic.textContent = `⏳ Pobieranie skanów (${limit})...`;
- loadingSpan.appendChild(loadingItalic);
- previewContainer.appendChild(loadingSpan);
- link.parentNode.insertBefore(previewContainer, link.nextSibling);
- try {
- const response = await fetch(link.href);
- const html = await response.text();
- const parser = new DOMParser();
- const doc = parser.parseFromString(html, 'text/html');
- const mainContent = doc.querySelector('#main-content') || doc.querySelector('.portlet-boundary') || doc.body;
- const allImages = mainContent.querySelectorAll('img');
- let validImages =[];
- for (let i = 0; i < allImages.length; i++) {
- const img = allImages[i];
- const src = img.getAttribute('data-src') || img.getAttribute('src');
- if (!src) {
- continue;
- }
- const lowerSrc = src.toLowerCase();
- if (lowerSrc.includes('logo') || lowerSrc.includes('icon') || lowerSrc.includes('theme') ||
- lowerSrc.includes('avatar') || lowerSrc.includes('spacer') || lowerSrc.includes('portlet') ||
- lowerSrc.includes('button')) {
- continue;
- }
- validImages.push(src);
- }
- validImages = [...new Set(validImages)];
- clearContainer(previewContainer);
- if (validImages.length === 0) {
- const emptySpan = document.createElement('span');
- emptySpan.style.color = '#d9534f';
- emptySpan.style.fontSize = '14px';
- const emptyBold = document.createElement('b');
- emptyBold.textContent = 'Brak zeskanowanych stron.';
- emptySpan.appendChild(emptyBold);
- previewContainer.appendChild(emptySpan);
- return;
- }
- const imagesToDisplay = validImages.slice(0, limit);
- for (let i = 0; i < imagesToDisplay.length; i++) {
- let imgSrc = imagesToDisplay[i];
- if (imgSrc.startsWith('/')) {
- imgSrc = window.location.origin + imgSrc;
- }
- const img = document.createElement('img');
- img.src = imgSrc;
- img.className = 'swa-preview-img';
- img.title = `Skan ${i + 1} z ${limit} (Kliknij, aby otworzyć jednostkę)`;
- img.addEventListener('click', function() {
- window.open(link.href, '_blank');
- });
- previewContainer.appendChild(img);
- }
- if (limit > 3) {
- const footer = document.createElement('div');
- footer.style.cssText = 'width: 100%; font-size: 12px; color: #777; padding-top: 10px; border-top: 1px solid #ddd; margin-top: 5px;';
- footer.textContent = `Wyświetlono ${imagesToDisplay.length} skanów zgodnie z informacją w tabeli.`;
- previewContainer.appendChild(footer);
- }
- } catch (error) {
- clearContainer(previewContainer);
- const errorSpan = document.createElement('span');
- errorSpan.style.color = 'red';
- errorSpan.style.fontSize = '14px';
- errorSpan.textContent = 'Błąd sieci.';
- previewContainer.appendChild(errorSpan);
- }
- }
- // --- 3. ANALIZA TABELI ---
- async function processAllUnitsAutomatically() {
- const unitLinks = document.querySelectorAll('a[href*="/jednostka/"]:not(.swa-processed)');
- for (let i = 0; i < unitLinks.length; i++) {
- const link = unitLinks[i];
- link.classList.add('swa-processed');
- const linkText = link.textContent.trim();
- if (link.querySelector('img') || !linkText) {
- continue;
- }
- const td = link.closest('td, div.table-cell, div[role="cell"], div.cell');
- const row = td ? td.closest('tr, div.table-row, div[role="row"], div.row') : link.closest('tr, div.table-row, div[role="row"], div.row');
- if (!row) {
- continue;
- }
- // Rozszerzono weryfikację o język angielski (dodano 'title' i 'name')
- let isNameCol = false;
- const dataTitle = (td && td.getAttribute('data-title') || '').toLowerCase();
- if (dataTitle.includes('name') || dataTitle.includes('title') || dataTitle.includes('tytu') || dataTitle.includes('nazw')) {
- isNameCol = true;
- }
- let cellIndex = -1;
- if (!isNameCol && td) {
- cellIndex = td.cellIndex;
- if (cellIndex === undefined) {
- const siblings = Array.from(row.children);
- cellIndex = siblings.indexOf(td);
- }
- const table = row.closest('table, div.table, div[role="table"]');
- const headers = table ? Array.from(table.querySelectorAll('thead th, th, div.th, div[role="columnheader"]')) : [];
- const th = headers[cellIndex];
- if (th) {
- const thText = th.textContent.toLowerCase();
- if (thText.includes('tytu') || thText.includes('nazw') || thText.includes('title') || thText.includes('name')) {
- isNameCol = true;
- }
- }
- }
- if (!isNameCol) {
- continue;
- }
- if (processedHrefs.has(link.href)) {
- continue;
- }
- processedHrefs.add(link.href);
- // Wyciąganie liczby skanów z rozszerzeniem na wariant angielski (słowo 'scan')
- let scanLimit = 0;
- const cells = row.cells ? Array.from(row.cells) : Array.from(row.children);
- const table = row.closest('table, div.table, div[role="table"]');
- const headers = table ? Array.from(table.querySelectorAll('thead th, th, div.th, div[role="columnheader"]')) :[];
- for (let j = 0; j < cells.length; j++) {
- const cell = cells[j];
- const headerText = headers[j] ? headers[j].textContent.toLowerCase() : '';
- const cellTitle = (cell.getAttribute('data-title') || '').toLowerCase();
- const cellContentStr = cell.textContent.toLowerCase();
- if (headerText.includes('skan') || headerText.includes('scan') ||
- cellTitle.includes('skan') || cellTitle.includes('scan') ||
- (cellContentStr.includes('skan') && /\d/.test(cellContentStr)) ||
- (cellContentStr.includes('scan') && /\d/.test(cellContentStr))) {
- scanLimit = parseInt(cell.textContent.replace(/\D/g, ''), 10) || 0;
- }
- }
- if (scanLimit > 0) {
- await loadScansForUnit(link, scanLimit);
- await delay(300);
- }
- }
- }
- setTimeout(processAllUnitsAutomatically, 1000);
- const observer = new MutationObserver(function(mutations) {
- let hasAddedNodes = false;
- for (let i = 0; i < mutations.length; i++) {
- if (mutations[i].addedNodes.length > 0) {
- hasAddedNodes = true;
- break;
- }
- }
- if (hasAddedNodes) {
- stretchPage();
- setTimeout(processAllUnitsAutomatically, 1000);
- }
- });
- observer.observe(document.body, { childList: true, subtree: true });
- })();
Advertisement
Add Comment
Please, Sign In to add comment