AvitoTool

Позволяет скрывать объявления на сайте avito.ru

  1. // ==UserScript==
  2. // @name AvitoTool
  3. // @version 0.0.3
  4. // @description Позволяет скрывать объявления на сайте avito.ru
  5. // @author pp
  6. // @include https://www.avito.ru/*
  7. // @grant none
  8. // @namespace https://greasyfork.org/users/814032
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var AvitoTool = {
  15. verbose: false,
  16. name: 'AvitoTool',
  17. keyHash: 'avitotool:hidden:elements',
  18. getHiddenElements: function() {
  19. return JSON.parse(localStorage.getItem(AvitoTool.keyHash));
  20. },
  21. setHiddenElements: function(arr, id) {
  22. AvitoTool.log('added new hidden element: ' + id);
  23. return localStorage.setItem(AvitoTool.keyHash, JSON.stringify(arr));
  24. },
  25. addHiddenElement: function(id) {
  26. var hiddenElementsArr = this.getHiddenElements();
  27. if (hiddenElementsArr !== null && hiddenElementsArr.length > 0) {
  28. hiddenElementsArr.push(id);
  29. } else {
  30. hiddenElementsArr = [id];
  31. }
  32. this.setHiddenElements(hiddenElementsArr, id);
  33. },
  34. addHideButton: function() {
  35. document.querySelectorAll('div[class^="items-items"] > div[data-marker^="item"]').forEach(function (item, index) {
  36. var hideButton = '<div class="item-hide"><button type="button" class="hide-button-' + item.dataset.itemId + ' button-button-eBrUW button-button-CmK9a button-size-s-r9SeD button-default-_Uj_C" aria-busy="false"><span class="button-textBox-_SF60"><div class="button-button__text_wrapper-AHKCO"><span class="text-text-LurtD text-size-s-BxGpL">Скрыть объявление</span></div></span></button></div>';
  37.  
  38. item.querySelector('div[class^="iva-item-actions"]').innerHTML += hideButton;
  39. document.querySelector('button.hide-button-' + item.dataset.itemId).addEventListener("click", function(e){
  40. var hiddenElementsArr = AvitoTool.getHiddenElements();
  41. if (hiddenElementsArr != null && !hiddenElementsArr.includes(item.dataset.itemId)) {
  42. AvitoTool.addHiddenElement(item.dataset.itemId);
  43. } else if (hiddenElementsArr == null) {
  44. AvitoTool.addHiddenElement(item.dataset.itemId);
  45. } else {
  46. AvitoTool.log('element id: ' + item.dataset.itemId + ' already added');
  47. }
  48. AvitoTool.hideByItemId(item.dataset.itemId);
  49. e.preventDefault();
  50. });
  51. });
  52. },
  53. hideByItemId: function(id) {
  54. var hideElem = document.querySelector('div[data-item-id="' + id + '"]');
  55. if (hideElem != null)
  56. hideElem.style.display = 'none';
  57.  
  58. },
  59. hideElements: function() {
  60. var hiddenElementsArr = this.getHiddenElements();
  61. if (hiddenElementsArr != null) {
  62. hiddenElementsArr.forEach(function(itemId, index) {
  63. AvitoTool.hideByItemId(itemId);
  64. });
  65. }
  66. },
  67. loaded: function() {
  68. AvitoTool.log("loaded");
  69. },
  70. run: function() {
  71. document.onreadystatechange = function () {
  72. if (document.readyState == "complete") {
  73. AvitoTool.addHideButton();
  74. AvitoTool.loaded();
  75. }
  76. }
  77.  
  78. AvitoTool.hideElements();
  79. },
  80. log: function(o) {
  81. if (AvitoTool.verbose) {
  82. console.log(AvitoTool.name + ' >> ' + o);
  83. }
  84. }
  85. };
  86. AvitoTool.run();
  87.  
  88. })();