🏠 Home 

Kleinanzeigen.de Grid Viewer

04/08/2023, 21:46:24


Install this script?
  1. // ==UserScript==
  2. // @name Kleinanzeigen.de Grid Viewer
  3. // @namespace Violentmonkey Scripts
  4. // @match https://www.kleinanzeigen.de/*
  5. // @grant none
  6. // @version 1.1
  7. // @author -
  8. // @description 04/08/2023, 21:46:24
  9. // @run-at Document-Start
  10. // @license MIT
  11. // ==/UserScript==
  12. var modalStyle = document.createElement('style');
  13. modalStyle.innerHTML = `
  14. /* Styles for the modal overlay */
  15. .modal-overlay {
  16. display: none;
  17. position: fixed;
  18. top: 0;
  19. left: 0;
  20. width: 100%; /* Make the overlay width 100% of the viewport */
  21. height: 100%;
  22. background-color: rgba(0, 0, 0, 0.7);
  23. justify-content: center;
  24. align-items: center;
  25. z-index: 9999; /* Adjust the z-index to ensure the overlay is on top */
  26. }
  27. /* Styles for the modal content */
  28. .modal-content {
  29. background-color: white;
  30. padding: 20px;
  31. border-radius: 5px;
  32. box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.2);
  33. width: 90%; /* Set the width of the content within the overlay */
  34. max-width: 1200px; /* Optionally, set a maximum width for the content */
  35. max-height: 90%;
  36. overflow: auto;
  37. display: grid;
  38. grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  39. gap: 20px;
  40. align-items: end;
  41. }
  42. `;
  43. document.head.appendChild(modalStyle);
  44. // Define a function to extract the product information from a listing element
  45. function getProductInfo(listing) {
  46. var imageBox = listing.querySelector(".imagebox.srpimagebox"); // Updated selector
  47. var image = imageBox.querySelector("img"); // Select the img element inside the imagebox
  48. var imageSrc = image ? image.getAttribute("src") : "";
  49. // Get the link element
  50. var link = listing.querySelector(".ellipsis");
  51. // Get the link href
  52. var linkHref = link ? link.href : "";
  53. // Get the link text
  54. var linkText = link ? link.textContent : "";
  55. // Get the price element
  56. var priceElement = listing.querySelector(".aditem-main--middle--price-shipping--price");
  57. // Get the price text
  58. var priceText = priceElement ? priceElement.textContent : "";
  59. // Return an object with the product information
  60. return {
  61. image: imageSrc,
  62. url: linkHref,
  63. title: linkText,
  64. price: priceText
  65. };
  66. }
  67. // Define a function to loop through all the listings on the page and add them to the products array
  68. function getProducts() {
  69. // Create a local products array
  70. var products = [];
  71. // Get all the listing elements
  72. var listings = document.querySelectorAll(".ad-listitem .aditem");
  73. // Loop through each listing element
  74. for (var i = 0; i < listings.length; i++) {
  75. // Get the current listing element
  76. var listing = listings[i];
  77. // Get the product information from the listing element
  78. var productInfo = getProductInfo(listing);
  79. // Add the product information to the products array
  80. products.push(productInfo);
  81. }
  82. // Return the products array
  83. return products;
  84. }
  85. // Define a function to create the modal overlay
  86. function createModalOverlay() {
  87. var modalOverlay = document.createElement('div');
  88. modalOverlay.className = 'modal-overlay';
  89. var modalContent = document.createElement('div');
  90. modalContent.className = 'modal-content';
  91. var closeButton = document.createElement('button');
  92. closeButton.innerHTML = 'X';
  93. closeButton.style.position = 'absolute';
  94. closeButton.style.right = '10px';
  95. closeButton.style.top = '10px';
  96. closeButton.onclick = function () {
  97. modalOverlay.style.display = 'none';
  98. };
  99. modalContent.appendChild(closeButton);
  100. modalOverlay.appendChild(modalContent);
  101. document.body.appendChild(modalOverlay);
  102. return modalOverlay;
  103. }
  104. // Define a function to add products to the modal content
  105. function addProductsToModal(products, modalContent) {
  106. for (var i = 0; i < products.length; i++) {
  107. var product = products[i];
  108. var productCard = document.createElement('div');
  109. productCard.className = 'product-card';
  110. productCard.innerHTML = `
  111. <a href="${product.url}" target="_blank">
  112. <img src="${product.image}" alt="Product Image"><br/>
  113. ${product.title}<br/>
  114. <span>${product.price}</span>
  115. </a>
  116. `;
  117. modalContent.appendChild(productCard);
  118. }
  119. }
  120. var isFetching = false;
  121. var currentPageURL = window.location.href;
  122. function loadNextPageProducts() {
  123. // If a fetch is already in progress, return early
  124. if (isFetching) {
  125. return;
  126. }
  127. isFetching = true;
  128. var currentPage = currentPageURL.includes('seite:') ? parseInt(currentPageURL.split('seite:')[1]) : 1;
  129. var nextPage = currentPage + 1;
  130. var nextPageURL;
  131. if (currentPageURL.includes('seite:')) {
  132. nextPageURL = currentPageURL.replace(/(seite:)\d+/, 'seite:' + nextPage);
  133. } else {
  134. var urlParts = currentPageURL.split('/');
  135. urlParts.splice(-1, 0, 'seite:' + nextPage);
  136. nextPageURL = urlParts.join('/');
  137. }
  138. fetch(nextPageURL)
  139. .then(response => response.text())
  140. .then(html => {
  141. var parser = new DOMParser();
  142. var nextPageDocument = parser.parseFromString(html, 'text/html');
  143. var nextPageListings = nextPageDocument.querySelectorAll(".ad-listitem .aditem");
  144. var products = [];
  145. nextPageListings.forEach(listing => {
  146. var productInfo = getProductInfo(listing);
  147. products.push(productInfo);
  148. });
  149. var modalContent = document.querySelector('.modal-content');
  150. addProductsToModal(products, modalContent);
  151. // When the fetch is complete, set isFetching back to false
  152. isFetching = false;
  153. // Update currentPageURL to the URL of the next page
  154. currentPageURL = nextPageURL;
  155. }).catch(error => {
  156. console.error('Error:', error);
  157. // If an error occurs, set isFetching back to false
  158. isFetching = false;
  159. });
  160. }
  161. function showModal() {
  162. var modalOverlay = document.querySelector('.modal-overlay');
  163. var modalContent;
  164. if (!modalOverlay) {
  165. modalOverlay = createModalOverlay();
  166. }
  167. modalContent = modalOverlay.querySelector('.modal-content');
  168. var products = getProducts();
  169. addProductsToModal(products, modalContent);
  170. function loadNextPageIfNearBottom() {
  171. var buffer = 100; // Number of pixels from the bottom to start loading next page
  172. if (
  173. modalContent.scrollTop + modalContent.clientHeight >=
  174. modalContent.scrollHeight - buffer
  175. ) {
  176. loadNextPageProducts();
  177. }
  178. }
  179. // Add a scroll event listener to the modal overlay
  180. modalContent.addEventListener('scroll', loadNextPageIfNearBottom);
  181. // Show the modal overlay
  182. modalOverlay.style.display = 'flex';
  183. }
  184. var targetDiv = document.querySelector('.l-container-row.contentbox-unpadded.no-bg');
  185. if (targetDiv !== null) {
  186. var triggerButton = document.createElement('button');
  187. triggerButton.innerHTML = 'Show Grid';
  188. triggerButton.style.display = 'block';
  189. triggerButton.style.margin = '0 auto';
  190. triggerButton.onclick = showModal;
  191. targetDiv.insertBefore(triggerButton, targetDiv.firstChild);
  192. }