Advertisement
bongzilla

Untitled

Jun 3rd, 2022
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. document.addEventListener("DOMContentLoaded", function(e) {
  3.     function applyObserverOnlyWhenNodeAvailable() {
  4.         const wishCountEl = document.querySelector('[data-content="wish-count"]');
  5.         const currentWishCount = parseWishCount(wishCountEl);
  6.  
  7.         if(currentWishCount === 0) {
  8.             toggleWishCount(currentWishCount, wishCountEl);
  9.         }
  10.  
  11.         const wishListObserver = new MutationObserver(function(mutations) {
  12.             mutations.forEach(function(mutation) {
  13.                 if (mutation.type === "childList") {
  14.                     const wishCount = parseWishCount(wishCountEl);
  15.  
  16.                     toggleWishCount(wishCount, wishCountEl);
  17.                 }
  18.             });
  19.         });
  20.  
  21.         if (!wishCountEl) {
  22.             window.setTimeout(applyObserverOnlyWhenNodeAvailable, 500);
  23.             return;
  24.         }
  25.  
  26.         const wishListObserverConfig = {
  27.             childList: true,
  28.             subtree: true
  29.         };
  30.  
  31.         wishListObserver.observe(wishCountEl, wishListObserverConfig);
  32.     }
  33.  
  34.     function parseWishCount(wishElement) {
  35.         return parseInt(wishElement.innerText);
  36.     }
  37.  
  38.     function toggleWishCount(wishCount, wishCountEl) {
  39.         if (wishCount === 0) {
  40.             wishCountEl.parentNode.style.display = "none";
  41.         } else {
  42.             wishCountEl.parentNode.style.display = "flex";
  43.         }
  44.     }
  45.  
  46.     applyObserverOnlyWhenNodeAvailable();
  47. });
  48. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement