Guest User

OLX - Usuń ofertę

a guest
Aug 12th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name OLX - Usuń ofertę
  3. // @namespace https://www.olx.pl/
  4. // @version 1.2
  5. // @description Dodaje przycisk "X" do każdej oferty na OLX, który umożliwia usunięcie oferty z widoku
  6. // @author BlackReven / ChatGPT
  7. // @match https://www.olx.pl/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=olx.pl
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Funkcja do generowania unikalnego ID dla oferty na podstawie treści HTML (w razie braku ID)
  16. function generateOfferId(offer) {
  17. return offer.querySelector('a').href || offer.innerHTML.hashCode();
  18. }
  19.  
  20. // Dodanie metody do prototypu Stringa do generowania hashCode (na wypadej braku ID)
  21. String.prototype.hashCode = function() {
  22. var hash = 0, i, chr;
  23. if (this.length === 0) return hash;
  24. for (i = 0; i < this.length; i++) {
  25. chr = this.charCodeAt(i);
  26. hash = ((hash << 5) - hash) + chr;
  27. hash |= 0; // Konwersja do 32-bitowego integera
  28. }
  29. return hash;
  30. };
  31.  
  32. // Pobieranie listy usuniętych ofert z localStorage
  33. function getRemovedOffers() {
  34. let removedOffers = localStorage.getItem('removedOffers');
  35. return removedOffers ? JSON.parse(removedOffers) : [];
  36. }
  37.  
  38. // Zapisywanie listy usuniętych ofert do localStorage
  39. function setRemovedOffers(offers) {
  40. localStorage.setItem('removedOffers', JSON.stringify(offers));
  41. }
  42.  
  43. // Usuwanie oferty i zapisywanie jej ID
  44. function removeOffer(offer) {
  45. const offerId = generateOfferId(offer);
  46. if (offerId) {
  47. let removedOffers = getRemovedOffers();
  48. removedOffers.push(offerId);
  49. setRemovedOffers(removedOffers);
  50. offer.style.display = 'none';
  51. }
  52. }
  53.  
  54. // Funkcja dodająca przycisk "X" do każdej oferty
  55. function addRemoveButtons() {
  56. let offers = document.querySelectorAll('[data-cy="l-card"]');
  57.  
  58. offers.forEach(function(offer) {
  59. const offerId = generateOfferId(offer);
  60.  
  61. // Jeśli oferta jest już usunięta, ukryj ją
  62. if (getRemovedOffers().includes(offerId)) {
  63. offer.style.display = 'none';
  64. return;
  65. }
  66.  
  67. // Dodaj przycisk "X", jeśli jeszcze nie istnieje
  68. if (!offer.querySelector('.remove-offer-btn')) {
  69. let removeButton = document.createElement('button');
  70. removeButton.innerText = 'X';
  71. removeButton.className = 'remove-offer-btn';
  72. removeButton.style.position = 'absolute';
  73. removeButton.style.top = '10px';
  74. removeButton.style.right = '10px';
  75. removeButton.style.backgroundColor = 'red';
  76. removeButton.style.color = 'white';
  77. removeButton.style.border = 'none';
  78. removeButton.style.borderRadius = '50%';
  79. removeButton.style.width = '30px';
  80. removeButton.style.height = '30px';
  81. removeButton.style.cursor = 'pointer';
  82. removeButton.style.zIndex = '9999';
  83.  
  84. removeButton.addEventListener('click', function(event) {
  85. event.preventDefault();
  86. event.stopPropagation();
  87. removeOffer(offer);
  88. });
  89.  
  90. offer.style.position = 'relative';
  91. offer.appendChild(removeButton);
  92. }
  93. });
  94. }
  95.  
  96. // Funkcja, która zostanie wywołana po załadowaniu strony
  97. function onLoad() {
  98. addRemoveButtons();
  99.  
  100. // Dodajemy event listener do scrolla strony
  101. window.addEventListener('scroll', addRemoveButtons);
  102. }
  103.  
  104. // Wywołujemy funkcję onLoad po załadowaniu strony
  105. window.addEventListener('load', onLoad);
  106. })();
Advertisement
Add Comment
Please, Sign In to add comment